How the SpringBoot project injects Bean into a common class
- 2021-12-12 04:21:32
- OfStack
How to Inject Bean into Common Classes
Class managed by Spring gets 1 injected Bean mode
@ Autowired is a kind of annotation, which can annotate member variables, methods and constructors to complete automatic assembly and automatically execute the current method. If the method has parameters, it will automatically find parameters of the same type in the IOC container to pass values to it.
For example, injecting Bean into Controller can be written as follows:
@RestController
public class TestController {
@Autowired
public TestBean bean;// Configuration file bean
}
Non-Spring managed classes get 1 injected Bean mode
The above method of annotation injection through @ Autowired can only be used in classes managed by Spring, while Bean obtained in this way in ordinary classes is null.
Bean can be injected into a common class in the same way that Bean can be obtained by Spring context ApplicationContext
Get Bean through ApplicationContext context in ordinary class
public class Test{
// Declare the Bean Variable
private static TestBean bean;
// Pass applicationContext Context acquisition Bean
public static void setApplicationContext(ApplicationContext applicationContext) {
bean = applicationContext.getBean(TestBean.class);
}
}
Modify the SpringBoot startup class
Pass ApplicationContext into a common class
@SpringBootApplication
public class TestrApplication {
public static void main(String[] args) {
final ApplicationContext applicationContext = SpringApplication.run(TestApplication.class, args);
// Pass the context into Test Class, used to detect the tcp Is the connection normal
Test.setApplicationContext(applicationContext);
}
}
In this way, an Bean of Spring can be injected into ordinary classes for use.
How to get an Bean node in a common class
/*
* Filename: SpringContextUtil.java
* Description: Gets bean Instance tool class
* Modified by: Wang Chinda
* Modification time: 2018/10/30
* Modification: New
*/
package com.chinda.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Get bean Instance tool class
* @author Wang Chinda
* @date 2018/10/30
* @see
* @since 1.0
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
/**
* Context object instance
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* Get applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* Pass name Get Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* Pass class Get Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* Pass name As well as Class Returns the specified Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}