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 Find kth Largest Element in a Sequence
Deploy a Spring Boot App to Azure
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Randomized Binary Search Tree
Java – Rename or Move a File
Database Migrations with Flyway
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Implement Best-First Search
Java Program to Create a Random Graph Using Random Edge Generation
Java Program to Construct an Expression Tree for an Postfix Expression
Lập trình đa luồng với Callable và Future trong Java
Optional trong Java 8
More Jackson Annotations
Basic Authentication with the RestTemplate
Java Program to Permute All Letters of an Input String
TreeSet và sử dụng Comparable, Comparator trong java
Guide to the ConcurrentSkipListMap
Tính kế thừa (Inheritance) trong java
Spring Security Registration – Resend Verification Email
Hướng dẫn Java Design Pattern – State
Java Program to Implement Binomial Tree
Bootstrapping Hibernate 5 with Spring
Versioning a REST API
Java String Conversions
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Find the Registered Spring Security Filters
How to Remove the Last Character of a String?
Java Program to Implement Sorted List
Giới thiệu Json Web Token (JWT)
What is a POJO Class?
Spring REST API with Protocol Buffers
Java – Combine Multiple Collections