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 – Random Long, Float, Integer and Double
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Implement Circular Singly Linked List
New Features in Java 8
Java Program to Implement DelayQueue API
Java Program to Implement Trie
Introduction to Spring Data MongoDB
Java Program to Perform the Sorting Using Counting Sort
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java Program to Implement Find all Back Edges in a Graph
Guide to java.util.concurrent.BlockingQueue
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Interface trong Java 8 – Default method và Static method
Introduction to Spring Security Expressions
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Spring WebClient Filters
List Interface trong Java
Logging a Reactive Sequence
Returning Image/Media Data with Spring MVC
ArrayList trong java
Spring REST API + OAuth2 + Angular
Java Program to Implement Find all Forward Edges in a Graph
How to Iterate Over a Stream With Indices
New in Spring Security OAuth2 – Verify Claims
Cơ chế Upcasting và Downcasting trong java
HttpAsyncClient Tutorial
Guide to System.gc()
Enum trong java
Marker Interface trong Java
Java Program to Implement Heap Sort Using Library Functions