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:
HttpClient Timeout
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Constructor Injection in Spring with Lombok
“Stream has already been operated upon or closed” Exception in Java
Java Program to Generate Random Numbers Using Middle Square Method
HttpClient 4 – Send Custom Cookie
Spring Boot - Tomcat Port Number
wait() and notify() Methods in Java
Java Program to Implement vector
A Comparison Between Spring and Spring Boot
Refactoring Design Pattern với tính năng mới trong Java 8
Java Program to Implement Sparse Matrix
Spring Data – CrudRepository save() Method
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Apache Camel with Spring Boot
Java – Convert File to InputStream
Simultaneous Spring WebClient Calls
Simple Single Sign-On with Spring Security OAuth2
Hướng dẫn Java Design Pattern – Template Method
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
New Features in Java 13
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java Program to Implement Interpolation Search Algorithm
Sort a HashMap in Java
Spring Boot - Tomcat Deployment
Connect through a Proxy
Java Program to Perform Partition of an Integer in All Possible Ways
Recommended Package Structure of a Spring Boot Project
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Perform Partial Key Search in a K-D Tree
Map Serialization and Deserialization with Jackson
Format ZonedDateTime to String