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 Karatsuba Multiplication Algorithm
How to Round a Number to N Decimal Places in Java
Đồng bộ hóa các luồng trong Java
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Working With Maps Using Streams
Spring MVC + Thymeleaf 3.0: New Features
Java – Rename or Move a File
Set Interface trong Java
Sorting in Java
Deque và ArrayDeque trong Java
Java Program to Use rand and srand Functions
Testing an OAuth Secured API with Spring MVC
Period and Duration in Java
Java Program to Implement Hash Tree
Java Program to Implement JobStateReasons API
Java Program to Implement AA Tree
CharSequence vs. String in Java
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Spring Data Reactive Repositories with MongoDB
Java Program to Implement ArrayDeque API
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Collect a Java Stream to an Immutable Collection
Spring Boot - Scheduling
How to Get All Dates Between Two Dates?
Java Program to Solve a Matching Problem for a Given Specific Case
Spring Boot - Admin Server
Explain about URL and HTTPS protocol
Java 14 Record Keyword
Retrieve User Information in Spring Security
Send email with authentication
Java – Combine Multiple Collections
Java Program to Perform Quick Sort on Large Number of Elements