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 Program to Implement Cartesian Tree
Send an email with an attachment
Java Program to Perform Right Rotation on a Binary Search Tree
Java Program to Encode a Message Using Playfair Cipher
Spring 5 Functional Bean Registration
String Initialization in Java
Spring Security 5 for Reactive Applications
Java Program to implement Dynamic Array
Using the Not Operator in If Conditions in Java
Java Program to Perform Insertion in a BST
Java Program to Implement Sorted Doubly Linked List
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement LinkedTransferQueue API
Hướng dẫn sử dụng Java Generics
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement Adjacency List
Hướng dẫn Java Design Pattern – Intercepting Filter
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Convert XML to JSON Using Jackson
ExecutorService – Waiting for Threads to Finish
Luồng Daemon (Daemon Thread) trong Java
Java – InputStream to Reader
Guide to Spring Cloud Kubernetes
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Construct an Expression Tree for an Infix Expression
More Jackson Annotations
Java Program to Implement Knight’s Tour Problem
Immutable Map Implementations in Java
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement the Hill Cypher
Constructor Injection in Spring with Lombok
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters