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:
Abstract class và Interface trong Java
@Order in Spring
Java NIO2 Path API
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Build a REST API with Spring and Java Config
Chuyển đổi từ HashMap sang ArrayList
Java Program to Find Nearest Neighbor Using Linear Search
Java Convenience Factory Methods for Collections
Java Program to Implement LinkedTransferQueue API
Spring Boot Configuration with Jasypt
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
CyclicBarrier in Java
Java Program to Implement Word Wrap Problem
Template Engines for Spring
Java Program to Implement ConcurrentSkipListMap API
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Remove All Occurrences of a Specific Value from a List
Java Program to Check Cycle in a Graph using Graph traversal
Registration – Activate a New Account by Email
Java 8 Predicate Chain
Spring Web Annotations
Java Program to Find the Vertex Connectivity of a Graph
Shuffling Collections In Java
Handle EML file with JavaMail
Java Program to Implement Caesar Cypher
Easy Ways to Write a Java InputStream to an OutputStream
Assertions in JUnit 4 and JUnit 5
Hướng dẫn Java Design Pattern – Observer
Base64 encoding và decoding trong Java 8
Java Program to Implement ConcurrentLinkedQueue API
Posting with HttpClient
Truyền giá trị và tham chiếu trong java