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:
Tạo chương trình Java đầu tiên sử dụng Eclipse
Instance Profile Credentials using Spring Cloud
Mix plain text and HTML content in a mail
Java Program to Implement Stack
Sort a HashMap in Java
Java Map With Case-Insensitive Keys
Show Hibernate/JPA SQL Statements from Spring Boot
Dynamic Proxies in Java
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Giới thiệu Design Patterns
Spring Boot - Hystrix
Spring Cloud – Securing Services
Java Program to Implement LinkedHashSet API
Set Interface trong Java
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
JWT – Token-based Authentication trong Jersey 2.x
Understanding Memory Leaks in Java
Updating your Password
Generic Constructors in Java
Spring Security – security none, filters none, access permitAll
Automatic Property Expansion with Spring Boot
Java Program to Implement AA Tree
Spring REST API + OAuth2 + Angular (using the Spring Security OAuth legacy stack)
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Quick Guide to Spring MVC with Velocity
Create a Custom Exception in Java
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Hướng dẫn Java Design Pattern – Object Pool
Spring Security with Maven
HashMap trong Java hoạt động như thế nào?
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon