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:
Spring Security Login Page with React
Spring AMQP in Reactive Applications
Shuffling Collections In Java
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Implement Extended Euclid Algorithm
Spring Security 5 – OAuth2 Login
Từ khóa throw và throws trong Java
Java Program to Solve Tower of Hanoi Problem using Stacks
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Implement Nth Root Algorithm
How to Delay Code Execution in Java
Generating Random Numbers in a Range in Java
Extra Login Fields with Spring Security
Java Program to Implement Tarjan Algorithm
Unsatisfied Dependency in Spring
wait() and notify() Methods in Java
A Guide to BitSet in Java
Testing in Spring Boot
Java Program to Check whether Undirected Graph is Connected using BFS
The Spring @Controller and @RestController Annotations
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?
Jackson – Marshall String to JsonNode
Java Program to Implement the Program Used in grep/egrep/fgrep
Netflix Archaius with Various Database Configurations
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Implement Sorted Singly Linked List
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Guide to Spring @Autowired
Hướng dẫn sử dụng Java Reflection