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 the Synchronized Keyword in Java
LinkedHashSet trong Java hoạt động như thế nào?
How to Read a File in Java
Java Program to Optimize Wire Length in Electrical Circuit
Compact Strings in Java 9
Class Loaders in Java
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Spring Boot with Multiple SQL Import Files
Consumer trong Java 8
Tiêu chuẩn coding trong Java (Coding Standards)
Converting a Stack Trace to a String in Java
A Quick Guide to Spring Cloud Consul
Registration – Password Strength and Rules
Spring Boot - Hystrix
Chuyển đổi từ HashMap sang ArrayList
Using the Not Operator in If Conditions in Java
File Upload with Spring MVC
Các nguyên lý thiết kế hướng đối tượng – SOLID
Testing in Spring Boot
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Java Program to Solve the Fractional Knapsack Problem
Java Program to Implement Maximum Length Chain of Pairs
Function trong Java 8
Converting Iterator to List
Format ZonedDateTime to String
Guava Collections Cookbook
A Guide To UDP In Java
Java Program to Implement TreeSet API
Hướng dẫn Java Design Pattern – Adapter
Assert an Exception is Thrown in JUnit 4 and 5
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu