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 Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Hướng dẫn Java Design Pattern – Decorator
Understanding Memory Leaks in Java
Java Program to implement Priority Queue
Adding Shutdown Hooks for JVM Applications
Java Program to Create a Random Linear Extension for a DAG
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Spring Security 5 – OAuth2 Login
Java Multi-line String
Java Program to Compute Determinant of a Matrix
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Tarjan Algorithm
“Stream has already been operated upon or closed” Exception in Java
Java – File to Reader
Hướng dẫn Java Design Pattern – Proxy
Java 8 StringJoiner
Convert Time to Milliseconds in Java
Java Program to Use rand and srand Functions
Java Program to Perform Searching Based on Locality of Reference
Java Program to Find the Connected Components of an UnDirected Graph
Java Program to Implement vector
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement the RSA Algorithm
Hướng dẫn sử dụng Lớp FilePermission trong java
Working With Maps Using Streams
Java Program to Implement Hash Tables with Linear Probing
Introduction to PCollections
Guide to the Synchronized Keyword in Java
Java 14 Record Keyword
Java Program to Find Nearest Neighbor Using Linear Search
An Intro to Spring Cloud Vault