Spring的手动注入,动态注入
参数说明:
Objectbean,就是一个已经被创建的实例;
ApplicationContext:Spring的Context;
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME:按照名字来注入,注入类型。
/** * 调用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); }
相关推荐
yupi0 2020-10-10
spring 2020-08-18
编程点滴 2020-07-29
幸运小侯子 2020-07-05
itjavashuai 2020-07-04
qingjiuquan 2020-06-29
shushan 2020-06-25
小鱿鱼 2020-06-22
咻pur慢 2020-06-18
melonjj 2020-06-17
qingjiuquan 2020-06-13
neweastsun 2020-06-05
小鱿鱼 2020-06-05
mxcsdn 2020-05-31
吾日五省我身 2020-05-27
牧场SZShepherd 2020-05-27
sweetgirl0 2020-05-14