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 Hash Tables with Double Hashing
Java Program to Perform Search in a BST
Giới thiệu Design Patterns
Class Loaders in Java
Java Program to Implement Hopcroft Algorithm
Introduction to Spring Method Security
Xây dựng ứng dụng Client-Server với Socket trong Java
Shuffling Collections In Java
Spring Boot: Customize the Jackson ObjectMapper
Spring MVC Content Negotiation
Java Program to Create a Random Linear Extension for a DAG
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java program to Implement Tree Set
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Spring Boot - Batch Service
How to Read a File in Java
Guide to the Java Clock Class
Java Program to Implement Ford–Fulkerson Algorithm
Spring RestTemplate Error Handling
Composition, Aggregation, and Association in Java
A Guide to JUnit 5
Java Program to Implement Jarvis Algorithm
Java Program to Implement Weight Balanced Tree
Java Program to Implement Segment Tree
Java Program to Implement Sieve Of Eratosthenes
Java – InputStream to Reader
Java Program to implement Bi Directional Map
Tổng quan về ngôn ngữ lập trình java
Entity To DTO Conversion for a Spring REST API
Hướng dẫn Java Design Pattern – Service Locator
Semaphore trong Java
Java Program to implement Bit Set