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:
Array to String Conversions
Java Program to Implement Queue using Linked List
Jackson – Marshall String to JsonNode
Java Program to Solve Knapsack Problem Using Dynamic Programming
Java Program to Find the Edge Connectivity of a Graph
Java – Try with Resources
Cachable Static Assets with Spring MVC
Spring Boot - Eureka Server
Java Program to Perform Partial Key Search in a K-D Tree
Java Program to Implement Fermat Primality Test Algorithm
Converting String to Stream of chars
Mockito and JUnit 5 – Using ExtendWith
Giới thiệu JDBC Connection Pool
Hướng dẫn Java Design Pattern – Mediator
How to Define a Spring Boot Filter?
The Order of Tests in JUnit
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
OAuth2 Remember Me with Refresh Token
A Quick Guide to Spring Cloud Consul
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Hướng dẫn sử dụng Printing Service trong Java
Lập trình đa luồng trong Java (Java Multi-threading)
Hướng dẫn Java Design Pattern – Prototype
Java Program to Implement Quick sort
Documenting a Spring REST API Using OpenAPI 3.0
Find the Registered Spring Security Filters
Java Program to Implement Min Hash
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Java Program to Solve Tower of Hanoi Problem using Stacks
Collection trong java
Hướng dẫn Java Design Pattern – Service Locator