In Spring Boot, we can use Spring Framework to define our beans and their dependency injection. The @ComponentScan annotation is used to find beans and the corresponding injected with @Autowired annotation.
If you followed the Spring Boot typical layout, no need to specify any arguments for @ComponentScan annotation. All component class files are automatically registered with Spring Beans.
The following example provides an idea about Auto wiring the Rest Template object and creating a Bean for the same −
@Bean public RestTemplate getRestTemplate() { return new RestTemplate(); }
The following code shows the code for auto wired Rest Template object and Bean creation object in main Spring Boot Application class file −
package com.maixuanviet.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class DemoApplication { @Autowired RestTemplate restTemplate; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
Related posts:
New Stream Collectors in Java 9
Spring @Primary Annotation
Tạo chương trình Java đầu tiên sử dụng Eclipse
Converting a List to String in Java
Spring 5 WebClient
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Auditing with JPA, Hibernate, and Spring Data JPA
Java Program to Implement Queue
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Describe the Representation of Graph using Incidence Matrix
Lập trình mạng với java
Java Program to Check Whether Graph is DAG
List Interface trong Java
Java Program to Implement Flood Fill Algorithm
Xây dựng ứng dụng Client-Server với Socket trong Java
Java Program to Implement HashMap API
Logging in Spring Boot
Java Program to Implement Cubic convergence 1/pi Algorithm
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Adding Shutdown Hooks for JVM Applications
ArrayList trong java
How to Get All Spring-Managed Beans?
A Guide to EnumMap
Hướng dẫn Java Design Pattern – Object Pool
Serverless Functions with Spring Cloud Function
Java Program to Perform Partial Key Search in a K-D Tree
Guide to DelayQueue
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
OAuth2.0 and Dynamic Client Registration
Introduction to Thread Pools in Java
Annotation trong Java 8