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 the Nearest Neighbor Using K-D Tree Search
Quick Intro to Spring Cloud Configuration
Java 8 StringJoiner
Java Program to Implement Regular Falsi Algorithm
@DynamicUpdate with Spring Data JPA
Java Program to Generate Date Between Given Range
Java List UnsupportedOperationException
Spring Cloud – Adding Angular
Collect a Java Stream to an Immutable Collection
Guava CharMatcher
Java Program to Represent Linear Equations in Matrix Form
Read an Outlook MSG file
Handling URL Encoded Form Data in Spring REST
HandlerAdapters in Spring MVC
Hướng dẫn Java Design Pattern – Bridge
Xây dựng ứng dụng Client-Server với Socket trong Java
HttpClient 4 – Send Custom Cookie
Request Method Not Supported (405) in Spring
Wrapper Classes in Java
Netflix Archaius with Various Database Configurations
Lập trình mạng với java
Spring Boot - Rest Controller Unit Test
Guide to CopyOnWriteArrayList
Java 8 Predicate Chain
How to Convert List to Map in Java
Spring MVC Setup with Kotlin
JUnit 5 @Test Annotation
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Tổng quan về ngôn ngữ lập trình java
Introduction to Spring Method Security
Spring Security and OpenID Connect
REST Web service: Basic Authentication trong Jersey 2.x