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:
How to Manually Authenticate User with Spring Security
Guide to DelayQueue
Java Program to Create a Random Graph Using Random Edge Generation
Java Program to Implement Double Order Traversal of a Binary Tree
Getting Started with Stream Processing with Spring Cloud Data Flow
JWT – Token-based Authentication trong Jersey 2.x
An Intro to Spring Cloud Task
Anonymous Classes in Java
Java Program to Implement Skew Heap
Spring Boot Application as a Service
How to Set TLS Version in Apache HttpClient
HttpClient Connection Management
Java Program to Implement Hash Tables Chaining with List Heads
Spring Boot - Twilio
Send email with JavaMail
HttpClient 4 – Follow Redirects for POST
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Spring Boot: Customize the Jackson ObjectMapper
Collection trong java
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Introduction to Thread Pools in Java
Getting the Size of an Iterable in Java
HttpClient Timeout
Java Program to Implement Graph Coloring Algorithm
Testing in Spring Boot
Lập trình hướng đối tượng (OOPs) trong java
Introduction to Spring Method Security
Java Program to Implement Bubble Sort
How to Remove the Last Character of a String?
Java Program to Find Maximum Element in an Array using Binary Search
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph