Spring aware相关接口 浅析
spring中 有一大堆Aware结尾接口, 例如BeanFactoryAware, ApplicationContextAware, ServletContextAware等
在程序中 我们自定义的bean实现了 Aware相关接口 spring 通过回调 就可以获得一些资源
比如说 我们的bean实现BeanFactoryAware spring会在bean实例化(getBean)阶段 回调该setBeanFactory方法 注入BeanFactory实例(也就是this)
spring BeanFactoryAware 接口定义
public interface BeanFactoryAware { void setBeanFactory(BeanFactory beanFactory) throws BeansException; }
spring容器中的bean 实例化 是在AbstractBeanFactory getBean中发生
public Object getBean(String name, Class requiredType, Object[] args) throws BeansException { return doGetBean(name, requiredType, args, false); }
调用的 doGetBean 方法比较复杂 简单看看 创建单例这段
protected Object doGetBean( final String name, final Class requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); .................略 // Create bean instance. //bean的定义是不是单例 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory() { public Object getObject() throws BeansException { try { // 是一个抽象方法 在子类AbstractAutowireCapableBeanFactory中实现 return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } .................略 return bean; }
AbstractAutowireCapableBeanFactory createBean方法
protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { AccessControlContext acc = AccessController.getContext(); return AccessController.doPrivileged(new PrivilegedAction() { public Object run() { .................略 //关注doCreateBean方法 Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } }, acc); }
doCreateBean方法
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { // Instantiate the bean. BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = (BeanWrapper) this.factoryBeanInstanceCache.remove(beanName); } .................略 // Initialize the bean instance. Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); //实现 *Aware部分接口, InitializingBean接口 方法调用在此发生 exposedObject = initializeBean(beanName, exposedObject, mbd); } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } .................略 return exposedObject; }
initializeBean方法
protected Object initializeBean(String beanName, Object bean, RootBeanDefinition mbd) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader()); } // 如果当前bean 是BeanFactoryAware的一个实例 if (bean instanceof BeanFactoryAware) { //回调当前实例注入资源 ((BeanFactoryAware) bean).setBeanFactory(this); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { //BeanPostProcessor 接口 postProcessBeforeInitialization方法回调 wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { // 实现InitializingBean接口 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; }
Spring aware相关接口
Andy_能力越到责任越大