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:
Removing all duplicates from a List in Java
Java Program to Compute DFT Coefficients Directly
A Guide to the Java LinkedList
Format ZonedDateTime to String
A Guide to Spring Cloud Netflix – Hystrix
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Mockito and JUnit 5 – Using ExtendWith
Immutable Objects in Java
Java Program to Implement Stack using Two Queues
Control the Session with Spring Security
Java 8 StringJoiner
Request Method Not Supported (405) in Spring
Java CyclicBarrier vs CountDownLatch
Handle EML file with JavaMail
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Vector API
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Class Loaders in Java
Spring Boot - Zuul Proxy Server and Routing
ETags for REST with Spring
Why String is Immutable in Java?
Java Program to implement Bi Directional Map
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Implement Knapsack Algorithm
Functional Interface trong Java 8
A Guide to Queries in Spring Data MongoDB
Java Program to Implement Uniform-Cost Search
New Features in Java 11
Serialize Only Fields that meet a Custom Criteria with Jackson
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Java – String to Reader