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 Merge Sort Algorithm on Linked List
Convert Character Array to String in Java
Java Program to Implement Fenwick Tree
The Thread.join() Method in Java
LinkedList trong java
Java Program to Use rand and srand Functions
Java Program to Implement Euler Circuit Problem
Spring Boot - Admin Client
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Solve any Linear Equation in One Variable
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to Perform the Shaker Sort
Java Program to Solve the 0-1 Knapsack Problem
The Guide to RestTemplate
Các kiểu dữ liệu trong java
Functional Interfaces in Java 8
Marker Interface trong Java
Introduction to the Functional Web Framework in Spring 5
Java Program to implement Bit Set
Java Program to Implement Johnson’s Algorithm
Cơ chế Upcasting và Downcasting trong java
Hướng dẫn Java Design Pattern – Prototype
REST Web service: Basic Authentication trong Jersey 2.x
ExecutorService – Waiting for Threads to Finish
Hashtable trong java
Mockito and JUnit 5 – Using ExtendWith
Spring Boot - Tomcat Deployment
Giới thiệu JDBC Connection Pool
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement Hash Tables with Double Hashing
Java Program to Implement Shell Sort