参数说明:
Object bean,就是一个已经被创建的实例;
ApplicationContext:Spring的Context;
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME:按照名字来注入,注入类型。
/**标签:autowireBean,Object,Spring,手动,appContext,param,bean,注入 From: https://blog.51cto.com/u_3871599/6139642
* 调用spring注入新创建对象的相关属性(根据属性名进行注入)
*
* @param bean
* @param appContext
*/
public static void autowireBean(Object bean, ApplicationContext appContext) {
autowireBean(bean, appContext, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);
}
/**
* 调用spring注入新创建对象的相关属性
*
* @param bean
* @param appContext
* @param autowireMode
*/
public static void autowireBean(Object bean, ApplicationContext appContext, int autowireMode) {
if (bean == null || appContext == null) {
return;
}
AutowireCapableBeanFactory factory = appContext.getAutowireCapableBeanFactory();
factory.autowireBeanProperties(bean, autowireMode, false);
String beanName = ClassUtils.getUserClass(bean).getName();
factory.initializeBean(bean, beanName);
}