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 14 Record Keyword
Spring’s RequestBody and ResponseBody Annotations
Uploading MultipartFile with Spring RestTemplate
Spring Boot - Eureka Server
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Java Program to Implement AVL Tree
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Guide to WeakHashMap in Java
Tìm hiểu về Web Service
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Getting a File’s Mime Type in Java
Remove the First Element from a List
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement WeakHashMap API
Java Program to implement Circular Buffer
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Find Minimum Element in an Array using Linear Search
Generic Constructors in Java
Java Program to Implement the MD5 Algorithm
Java Program to Implement Shunting Yard Algorithm
Documenting a Spring REST API Using OpenAPI 3.0
Spring Boot - Twilio
Guide to the Synchronized Keyword in Java
Call Methods at Runtime Using Java Reflection
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Implement Segment Tree
Registration – Password Strength and Rules
Base64 encoding và decoding trong Java 8
Java Program to Implement CopyOnWriteArraySet API
So sánh Array và ArrayList trong Java
Using JWT with Spring Security OAuth (legacy stack)
Giới thiệu HATEOAS