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 Implement the Binary Counting Method to Generate Subsets of a Set
Spring @RequestParam Annotation
Java Program to Implement the Monoalphabetic Cypher
Hướng dẫn Java Design Pattern – Template Method
Java Program to Represent Linear Equations in Matrix Form
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Solve the Fractional Knapsack Problem
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement Gauss Seidel Method
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Java Program to Implement Graph Structured Stack
Java Program to Implement CopyOnWriteArrayList API
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
Java Program to Implement Binomial Tree
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Dynamic Proxies in Java
Validations for Enum Types
Converting a Stack Trace to a String in Java
Guide to Escaping Characters in Java RegExps
Java Program to Implement Circular Doubly Linked List
Hướng dẫn Java Design Pattern – Null Object
Java Program to Check whether Graph is Biconnected
Java Program to Find Inverse of a Matrix
Using Java Assertions
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Introduction to the Java NIO2 File API
Java Program to Implement Booth Algorithm
The Order of Tests in JUnit
Creating a Web Application with Spring 5
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement the String Search Algorithm for Short Text Sizes