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:
Spring Security – security none, filters none, access permitAll
How to Read a Large File Efficiently with Java
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Java Program to Generate Random Numbers Using Multiply with Carry Method
Tính đóng gói (Encapsulation) trong java
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Java Program to Implement SynchronosQueue API
Java Program to Perform the Unique Factorization of a Given Number
Setting Up Swagger 2 with a Spring REST API
Converting Between an Array and a Set in Java
Java Program to Implement Iterative Deepening
Immutable ArrayList in Java
Java Program to Implement Ternary Heap
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Exception Handling in Java
How to Get the Last Element of a Stream in Java?
The Modulo Operator in Java
@Order in Spring
Java Program to Create a Random Linear Extension for a DAG
Java 8 Streams peek() API
Introduction to Spring Data MongoDB
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Program to Implement Hash Tables with Quadratic Probing
Hướng dẫn Java Design Pattern – Command
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement Circular Singly Linked List
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement Levenshtein Distance Computing Algorithm
Spring Boot - Enabling Swagger2
Java Program to implement Dynamic Array
@DynamicUpdate with Spring Data JPA