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:
Receive email using POP3
Serialize Only Fields that meet a Custom Criteria with Jackson
Comparing Dates in Java
Handling URL Encoded Form Data in Spring REST
Java Program to Implement Sorted Singly Linked List
Spring Boot Gradle Plugin
Converting Strings to Enums in Java
Spring Boot - File Handling
Java Program to Implement ArrayDeque API
Spring Boot - Tracing Micro Service Logs
A Quick Guide to Spring MVC Matrix Variables
Test a REST API with Java
Java Program to Check Cycle in a Graph using Graph traversal
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Working with Kotlin and JPA
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Red Black Tree
Creating a Web Application with Spring 5
Intro to Inversion of Control and Dependency Injection with Spring
Transactions with Spring and JPA
Java Program to Implement Flood Fill Algorithm
Join and Split Arrays and Collections in Java
Prevent Brute Force Authentication Attempts with Spring Security
Spring Cloud Series – The Gateway Pattern
Iterable to Stream in Java
Java Byte Array to InputStream
Jackson Ignore Properties on Marshalling
Giới thiệu Google Guice – Binding
Sending Emails with Java
Java Program to Find the Minimum value of Binary Search Tree
Java TreeMap vs HashMap
Send email with JavaMail