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 Stack using Linked List
Java 8 and Infinite Streams
Java Program to Create a Balanced Binary Tree of the Incoming Data
Spring Data – CrudRepository save() Method
Spring Boot - Introduction
Check if a String is a Palindrome in Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Tránh lỗi NullPointerException trong Java như thế nào?
Converting String to Stream of chars
Java Program to Implement the Bin Packing Algorithm
Java Program to Represent Graph Using Linked List
@Order in Spring
HttpClient with SSL
Java Program to Perform Addition Operation Using Bitwise Operators
Service Registration with Eureka
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Implement Efficient O(log n) Fibonacci generator
Hướng dẫn sử dụng Java Annotation
The DAO with JPA and Spring
Java Program to Implement ArrayList API
Spring Boot - Sending Email
Introduction to the Java ArrayDeque
Returning Custom Status Codes from Spring Controllers
Java 8 StringJoiner
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Fixing 401s with CORS Preflights and Spring Security
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Abstract class và Interface trong Java
Transaction Propagation and Isolation in Spring @Transactional
Java Program to Implement Sieve Of Sundaram
Hướng dẫn Java Design Pattern – Object Pool
Lớp lồng nhau trong java (Java inner class)