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:
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Custom Thread Pools In Java 8 Parallel Streams
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Form Validation with AngularJS and Spring MVC
Guide to Mustache with Spring Boot
Concurrent Test Execution in Spring 5
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Implement Tarjan Algorithm
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Find Basis and Dimension of a Matrix
Giới thiệu Aspect Oriented Programming (AOP)
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Implement VList
Build a REST API with Spring and Java Config
Guide to Escaping Characters in Java RegExps
Java Program to Implement Stack
Java Program to Implement the Hill Cypher
An Intro to Spring Cloud Contract
Java 8 and Infinite Streams
Lớp Arrarys trong Java (Arrays Utility Class)
Spring Boot - Cloud Configuration Client
Spring Data JPA @Modifying Annotation
A Guide to the ViewResolver in Spring MVC
Java – Generate Random String
Spring JDBC
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
A Quick Guide to Spring Cloud Consul
Phân biệt JVM, JRE, JDK
Guide to System.gc()
Spring Boot - Twilio
Java Program to Implement AttributeList API
How to Remove the Last Character of a String?