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:
Guide to Guava Table
Thao tác với tập tin và thư mục trong Java
Build a REST API with Spring and Java Config
String Processing with Apache Commons Lang 3
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Java Program to Perform Partition of an Integer in All Possible Ways
StringBuilder vs StringBuffer in Java
Integer Constant Pool trong Java
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Java Program for Douglas-Peucker Algorithm Implementation
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
String Operations with Java Streams
Java Program to Implement TreeMap API
Hướng dẫn Java Design Pattern – Transfer Object
A Quick JUnit vs TestNG Comparison
Prevent Cross-Site Scripting (XSS) in a Spring Application
Setting Up Swagger 2 with a Spring REST API
Hướng dẫn Java Design Pattern – State
Spring Boot - Sending Email
Java Program to Perform the Shaker Sort
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement Dijkstra’s Algorithm using Set
Mapping Nested Values with Jackson
Summing Numbers with Java Streams
String Joiner trong Java 8
Sử dụng CountDownLatch trong Java
Setting the Java Version in Maven
Tổng quan về ngôn ngữ lập trình java
Generic Constructors in Java
JUnit 5 for Kotlin Developers
How to Use if/else Logic in Java 8 Streams