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:
Quick Intro to Spring Cloud Configuration
Converting Iterator to List
Java Program to Implement Attribute API
Spring @RequestParam Annotation
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Implement Floyd-Warshall Algorithm
Mapping a Dynamic JSON Object with Jackson
Guide To CompletableFuture
Java Program to Find Transpose of a Graph Matrix
Query Entities by Dates and Times with Spring Data JPA
Custom HTTP Header with the HttpClient
Test a REST API with Java
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Sorted Doubly Linked List
Guide to ThreadLocalRandom in Java
Running Spring Boot Applications With Minikube
Feign – Tạo ứng dụng Java RESTful Client
Testing in Spring Boot
Hamcrest Collections Cookbook
Dynamic Proxies in Java
Spring REST API with Protocol Buffers
Hướng dẫn Java Design Pattern – Builder
Java Program to Implement Triply Linked List
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Java Collections Interview Questions
Map Serialization and Deserialization with Jackson
Java Program to Implement Sorted Circularly Singly Linked List
Java Program to Implement Stack
Java Program to Check Whether a Weak Link i.e. Articulation Vertex Exists in a Graph
Java Program to Implement Rolling Hash
Java Program to Perform Search in a BST
Function trong Java 8