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:
Apache Tiles Integration with Spring MVC
Spring WebFlux Filters
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Java Program to Check whether Graph is a Bipartite using DFS
Debugging Reactive Streams in Java
Spring Boot Integration Testing with Embedded MongoDB
Java Program to find the peak element of an array using Binary Search approach
Java Program to Implement Naor-Reingold Pseudo Random Function
Java Program to Implement Adjacency Matrix
Java – Reader to InputStream
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Initialize a HashMap in Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Assert an Exception is Thrown in JUnit 4 and 5
A Guide to EnumMap
Java 14 Record Keyword
Java Program to Represent Linear Equations in Matrix Form
Java Stream Filter with Lambda Expression
The Registration Process With Spring Security
Java Program to Implement Circular Singly Linked List
Java – InputStream to Reader
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Java Program to Implement Direct Addressing Tables
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Java Program to Implement Hash Trie
Hướng dẫn Java Design Pattern – DAO
Creating a Generic Array in Java
Java Program to Implement Stein GCD Algorithm
A Guide to Java HashMap
Java Program to Implement Self organizing List
Hướng dẫn Java Design Pattern – Intercepting Filter