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 Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Spring Security Form Login
Custom Thread Pools In Java 8 Parallel Streams
Java Program to Implement Queue
Hướng dẫn Java Design Pattern – MVC
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Hướng dẫn Java Design Pattern – Builder
Spring Boot - Admin Server
Guide to java.util.concurrent.Locks
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Java Program to Check Multiplicability of Two Matrices
Guide to CountDownLatch in Java
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement Hash Tables Chaining with Binary Trees
Java Program to Implement Quick Sort with Given Complexity Constraint
A Guide To UDP In Java
Java Program to Check whether Graph is Biconnected
A Guide to Iterator in Java
Format ZonedDateTime to String
Java Program to Implement Ternary Tree
Java Program to find the peak element of an array using Binary Search approach
The Guide to RestTemplate
Java 8 Predicate Chain
Java Program to Implement Range Tree
Java Program to Implement D-ary-Heap
Queue và PriorityQueue trong Java
Java Program to Implement PriorityQueue API
Java Program to Implement the Hill Cypher
Debugging Reactive Streams in Java
Java Program to Implement ArrayBlockingQueue API
A Custom Media Type for a Spring REST API
Dockerizing a Spring Boot Application