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:
Jackson – JsonMappingException (No serializer found for class)
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
A Custom Media Type for a Spring REST API
HttpAsyncClient Tutorial
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Represent Graph Using Adjacency Matrix
The Registration API becomes RESTful
HttpClient Basic Authentication
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Spring REST API + OAuth2 + Angular
HTTP Authentification and CGI/Servlet
Spring WebClient Filters
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Treap
Get the workstation name or IP
Guide to Spring Cloud Kubernetes
Java Program to Generate Random Numbers Using Middle Square Method
Cơ chế Upcasting và Downcasting trong java
Recommended Package Structure of a Spring Boot Project
Java Program to Implement the MD5 Algorithm
How to Find an Element in a List with Java
Rate Limiting in Spring Cloud Netflix Zuul
Java Program to Implement Hash Tables
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Guide to PriorityBlockingQueue in Java
Spring Boot - Securing Web Applications
Composition, Aggregation, and Association in Java
Thao tác với tập tin và thư mục trong Java
Guide to CopyOnWriteArrayList
The Thread.join() Method in Java
Guide to Java Instrumentation