initMethod 和 afterPropertiesSet 以及 AwareMethod方法的执行时机
来源:互联网 发布:高中数学视频教学软件 编辑:程序博客网 时间:2024/06/11 13:08
initMethod 和 afterPropertiesSet 以及 AwareMethod方法的执行时机
在spring开发中,我们定义bean
经常会需要用到beanFactory对象,这就需要实现BeanFactoryAware这种类型的接口,它有一个setBeanFactory方法
在xml中配置bean 的时候,我们也可以指定initMethod方法
在bean类定义的时候可以实现InitializingBean,提供一个afterPropertiesSet方法的实现
以上者3中情况我们经常用到,下面来分析一下spring是如何处理这3种情况的,他们的调用时机是怎么样的?
在AbstractAutowireCapableBeanFactory类中
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } //...省略很多代码 下面开始初始化 关键就是两个步骤 // Initialize the bean instance. Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); //这里执行了注入属性和依赖的操作 if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd);//这里执行了initMethod 和 afterPropertiesSet } } //...省略很多代码 return exposedObject; }
重点看initializeBean(beanName, exposedObject, mbd)
protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { invokeAwareMethods(beanName, bean); return null; } }, getAccessControlContext()); } else { //这里判断是BeanFactoryAware, ServletContextAware之类的aware类型,如果是的话就执行对于的Aware方法 //把beanFactory啊 servletContext啊之类的依赖set进去 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //如果实现了BeanPostProcessor接口 这里会执行postProcessBeforeInitialization方法 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //这里就开始执行initMethod 和 afterPropertiesSet方法 invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; }
进入invokeInitMethods(beanName, wrappedBean, mbd);
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean);//先判断是否实现了InitializingBean接口 if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { //.....省略很多代码 else { //执行afterPropertiesSet()方法 ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { //如果配置了initMethod 就执行initMethod方法 这里只是取到了方法名,显然是要通过反射调用了 invokeCustomInitMethod(beanName, bean, mbd); } } }
经过这些源码的分析 可以得出结论
首先这些过程都在很重要的抽象类AbstractAutowireCapableBeanFactory抽象类中 这个类就是常用的DefaultListableBeanFactory的父类
1.spring先根据beanDefinition创建出了bean 的实例
2.执行了populateBean方法 把属性和依赖都注入了
3.执行了 initializeBean(beanName, exposedObject, mbd);方法 这里面才进行了Aware相关方法,afterPropertiesSet 和 initMethod 方法的调用
可见这3种方法调用又都是在bean实例已经创建好,且属性值和依赖的其他bean实例都已经注入以后 才得到调用的
4.后面的代码可以看出 Aware相关方法最先执行,afterPropertiesSet 第二执行 ,initMethod 方法最后执行
另外多说一句 afterPropertiesSet方法是很有用的,比如 AOP事务管理用到的类,TransactionProxyFactoryBean 就是利用afterPropertiesSet方法事先把事务管理器
TransactionManager的代理类对象给生成好了,后面调用FactoryBean对象的getObject方法的时候,就直接把这个代理对象返回出去了。
0 0
- initMethod 和 afterPropertiesSet 以及 AwareMethod方法的执行时机
- Spring InitializingBean的afterPropertiesSet方法
- Spring InitializingBean的afterPropertiesSet方法
- AppDelegate中方法执行的时机
- viewController中方法执行的时机
- AppConext 的onCreate方法执行时机
- handler.post()方法的执行时机
- Spring BeanPostProcessor执行时机和Bean的初始化时机总结
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
- viewDidUnload和dealloc的执行时机
- viewDidUnload和dealloc的执行时机
- Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机
- Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- onMeasure、onLayout以及onSizeChanged方法的回调时机和回调顺序
- onMeasure、onLayout以及onSizeChanged方法的回调时机和回调顺序
- AR学习笔记
- platform按键驱动学习之测试实例
- A+B==C
- package.json文件配置详解
- python实现网站内部视频批量下载
- initMethod 和 afterPropertiesSet 以及 AwareMethod方法的执行时机
- c#菜鸟学完
- request for member 'getid’ in ‘e1’, which is of non-class type ‘Employee()’
- 近似回文词
- 替换空格
- Android中实现StickyNavLayout自己的小demo
- gcc命令常用选项
- SQL Server数据库锁机制及类型
- C# Json.net Self referencing loop detected for property