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:
Marker Interface trong Java
Stack Memory and Heap Space in Java
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Introduction to Spring Method Security
Java – InputStream to Reader
Java CyclicBarrier vs CountDownLatch
Java Program to Check whether Undirected Graph is Connected using BFS
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Build a REST API with Spring and Java Config
Introduction to Spring MVC HandlerInterceptor
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Introduction to Apache Commons Text
A Guide to EnumMap
Java Program to Generate a Random Subset by Coin Flipping
Consumer trong Java 8
Java Program to Represent Graph Using Linked List
Send an email using the SMTP protocol
Jackson – Marshall String to JsonNode
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Introduction to Java 8 Streams
Java Program to Implement Stack using Two Queues
Java – Reader to String
Java Program to Perform Sorting Using B-Tree
Documenting a Spring REST API Using OpenAPI 3.0
Java Program to Implement Sorted Vector
Java toString() Method
Spring WebClient Requests with Parameters
Reactive WebSockets with Spring 5
Java Program to Search for an Element in a Binary Search Tree
A Quick Guide to Spring Cloud Consul
Hướng dẫn sử dụng String Format trong Java
Cơ chế Upcasting và Downcasting trong java