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:
Reactive WebSockets with Spring 5
Java Program to Implement the Vigenere Cypher
Java Program to Implement Pollard Rho Algorithm
Spring Security Custom AuthenticationFailureHandler
Spring Boot - Unit Test Cases
Prevent Brute Force Authentication Attempts with Spring Security
Introduction to Java Serialization
Java 8 StringJoiner
Java Program to Implement DelayQueue API
Servlet 3 Async Support with Spring MVC and Spring Security
New Features in Java 8
Find the Registered Spring Security Filters
Java Program to Implement vector
Java Program to Implement Depth-limited Search
Immutable Map Implementations in Java
StringBuilder vs StringBuffer in Java
Simultaneous Spring WebClient Calls
Java Program to Implement Bresenham Line Algorithm
A Guide to HashSet in Java
Java Program to Implement Adjacency List
Spring Boot - Interceptor
How to Use if/else Logic in Java 8 Streams
Java Program to Implement K Way Merge Algorithm
Java InputStream to String
Spring Boot - Servlet Filter
Tìm hiểu về xác thực và phân quyền trong ứng dụng
A Guide To UDP In Java
Java String Conversions
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Comparing Arrays in Java
Spring Webflux with Kotlin
Java – Write to File