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 a Directed Graph Contains a Eulerian Path
Spring Boot - Hystrix
Summing Numbers with Java Streams
Java Program to Check the Connectivity of Graph Using DFS
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement Repeated Squaring Algorithm
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Implement Circular Singly Linked List
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Implement JobStateReasons API
Java Program to Implement Graph Structured Stack
The Difference Between map() and flatMap()
Jackson JSON Views
Java Program to Implement LinkedBlockingDeque API
Java Program to Implement Best-First Search
Java Program to Implement Weight Balanced Tree
Java Program to Implement the Hill Cypher
Java Program to Solve any Linear Equation in One Variable
Guide to Guava Multimap
Debug a JavaMail Program
Java Program to Check if it is a Sparse Matrix
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Compute Cross Product of Two Vectors
So sánh HashMap và Hashtable trong Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Spring Boot - Cloud Configuration Server
How to Count Duplicate Elements in Arraylist
Hướng dẫn Java Design Pattern – Abstract Factory
Login For a Spring Web App – Error Handling and Localization
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Configuring a DataSource Programmatically in Spring Boot