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 Median of two Sorted Arrays using Binary Search Approach
Serialize Only Fields that meet a Custom Criteria with Jackson
Java Program to Implement D-ary-Heap
A Guide to Java SynchronousQueue
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Describe the Representation of Graph using Adjacency List
Mix plain text and HTML content in a mail
Tips for dealing with HTTP-related problems
Java Program to Implement Flood Fill Algorithm
Service Registration with Eureka
Java Program to Perform Right Rotation on a Binary Search Tree
Introduction to Project Reactor Bus
Hướng dẫn Java Design Pattern – Interpreter
Convert Hex to ASCII in Java
Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
How to Read HTTP Headers in Spring REST Controllers
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Runnable vs. Callable in Java
Entity To DTO Conversion for a Spring REST API
Creating a Custom Starter with Spring Boot
Count Occurrences of a Char in a String
Java Program to Implement Segment Tree
Java Program to Implement Attribute API
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Guide to Java 8’s Collectors
Java – Write an InputStream to a File
Java InputStream to String
Hướng dẫn sử dụng Java Generics
Returning Custom Status Codes from Spring Controllers
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Implement the String Search Algorithm for Short Text Sizes