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:
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Implement Quick sort
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Hướng dẫn Java Design Pattern – Bridge
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Form Validation with AngularJS and Spring MVC
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Implement the Checksum Method for Small String Messages and Detect
TreeSet và sử dụng Comparable, Comparator trong java
Refactoring Design Pattern với tính năng mới trong Java 8
Jackson – Bidirectional Relationships
XML Serialization and Deserialization with Jackson
Introduction to Spring Cloud CLI
Java Program to Implement Heap Sort Using Library Functions
Java Copy Constructor
Java Program to Implement Affine Cipher
Java Program to Implement Gabow Algorithm
Spring Boot - Building RESTful Web Services
Java Program to implement Sparse Vector
Java Program to Represent Linear Equations in Matrix Form
Sorting Query Results with Spring Data
HttpClient 4 Cookbook
Java Program to Implement Tarjan Algorithm
Java Stream Filter with Lambda Expression
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Properties with Spring and Spring Boot
StringBuilder vs StringBuffer in Java
Java Program to Implement Dijkstra’s Algorithm using Queue
Tính kế thừa (Inheritance) trong java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Case-Insensitive String Matching in Java
Hướng dẫn sử dụng Printing Service trong Java