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 IdentityHashMap API
Lập trình đa luồng với Callable và Future trong Java
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Rate Limiting in Spring Cloud Netflix Zuul
Spring Data MongoDB Transactions
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Hopcroft Algorithm
Initialize a HashMap in Java
Assertions in JUnit 4 and JUnit 5
Guide to CountDownLatch in Java
Spring Boot - Build Systems
How to Read a Large File Efficiently with Java
So sánh HashSet, LinkedHashSet và TreeSet trong Java
4 tính chất của lập trình hướng đối tượng trong Java
HashMap trong Java hoạt động như thế nào?
Java Program to Implement Fermat Factorization Algorithm
Static Content in Spring WebFlux
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Overflow and Underflow in Java
Guide to Escaping Characters in Java RegExps
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Merging Streams in Java
Guide to Guava Multimap
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Binary Tree
Checked and Unchecked Exceptions in Java
Quick Guide to the Java StringTokenizer
Spring Boot - CORS Support
A Guide to EnumMap
Chuyển đổi từ HashMap sang ArrayList
Using Optional with Jackson
Biểu thức Lambda trong Java 8 – Lambda Expressions