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:
Spring Boot - Twilio
Check If a File or Directory Exists in Java
Configure a Spring Boot Web Application
Java Program to Implement Pairing Heap
Truyền giá trị và tham chiếu trong java
Java Program to Implement Borwein Algorithm
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
Injecting Prototype Beans into a Singleton Instance in Spring
TreeSet và sử dụng Comparable, Comparator trong java
Java Program to Implement Naor-Reingold Pseudo Random Function
Comparing Two HashMaps in Java
Marker Interface trong Java
Spring Boot - Interceptor
Zipping Collections in Java
A Guide to JUnit 5 Extensions
Java Program to Implement HashMap API
Java Program to Implement the Program Used in grep/egrep/fgrep
Tính trừu tượng (Abstraction) trong Java
CyclicBarrier in Java
Java Program to Implement RoleList API
StringBuilder vs StringBuffer in Java
Converting Between Byte Arrays and Hexadecimal Strings in Java
Comparing Dates in Java
Copy a List to Another List in Java
Spring Boot Tutorial – Bootstrap a Simple Application
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Introduction to Spring Boot CLI
Java Program to Implement Graph Structured Stack
The XOR Operator in Java
Send an email using the SMTP protocol
Changing Annotation Parameters At Runtime