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:
Programmatic Transaction Management in Spring
HashSet trong java
Build a REST API with Spring and Java Config
Iterable to Stream in Java
Java Program to Implement Skew Heap
Java 14 Record Keyword
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Implement Insertion Sort
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Perform String Matching Using String Library
Java Program to Implement Miller Rabin Primality Test Algorithm
Error Handling for REST with Spring
Limiting Query Results with JPA and Spring Data JPA
Shuffling Collections In Java
Java Program to Implement the One Time Pad Algorithm
Getting Started with Custom Deserialization in Jackson
Hướng dẫn Java Design Pattern – Decorator
Java Program to Implement Segment Tree
Show Hibernate/JPA SQL Statements from Spring Boot
New Features in Java 12
@Lookup Annotation in Spring
Overview of the java.util.concurrent
Guide to Java 8’s Collectors
Introduction to the Java ArrayDeque
A Guide to the Java LinkedList
Spring MVC + Thymeleaf 3.0: New Features
Instance Profile Credentials using Spring Cloud
DynamoDB in a Spring Boot Application Using Spring Data
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Threaded Binary Tree
The StackOverflowError in Java
Simultaneous Spring WebClient Calls