本期开始,我们来聊聊spring,到底什么是spring那
spring是一个轻量级的j2ee框架,目的就是降低开发难度,提高开发效率,本章内容,我们就来一起研究下spring的加载过程,从实例化到销毁的整个过程,我用xmind画了张图,加深理解

第一步:实例化bean对象,就是new一个对象的过程
第二步:将配置文件中的属性set到刚刚new的对象中
第三步:检查bean是否实现了aware接口,如果实现了,就把相应的依赖设置到bean对象中,
第四步;调用beanPostprocessor的前置处理方法,postProcessBeforeInitialization(Object bean, String beanName)
第五步:校验bean对象是否实现了 InitializingBean 接口,如果实现了,调用afterPropertiesSet方法,完成初始化的一些操作,或者配置文件中是否配置init-method属性,如果配置了,调用init-method属性配置的方法
第六步:调用BeanPostProcessor后置处理方法postProcessAfterInitialization(Object bean, String beanName),这里最重要的就是AOP毁在这里将advice逻辑织入到bean中
第七部:调用Destruction 方法
第八步:Bean对象这回已经加载完毕,处理就绪状态,可以调用了
第九步:销毁,应用上下文销毁,如果bean实现了disableBean接口,spring会调用destroy方法,如果在配置文件配置了destroy属性,spring也会调用destroy属性配置的方法.
这里涉及到一个比较重要的概念,我们在第二步中,说将属性设置到对象中,如果是基本属性,直接就可以设置,我们设想一个问题,如果设置的属性是对象那,比如A中依赖B,B依赖C,C依赖A,这样的话,spring是怎样处理的那,这几涉及到spring的循环依赖问题,这几简单说明下,
根据依赖注入方式的不同,Spring 处理方式也不同。如果依赖靠构造器方式注入,则无法处理,Spring 直接会报循环依赖异常。这个理解起来也不复杂,构造 A 时需要 B 作为构造器参数,此时 Spring 容器会先实例化 B。构造 B 时,B 又需要 C 作为构造器参数,Spring 容器又不得不先去构造 C。最后构造 C 时,C 又依赖 A 才能完成构造。此时,A 还没构造完成,A 要等 BeanB 实例化好才能完成构造,B 又要等 C,C 等 A。这样就形成了死循环,所以对于以构造器注入方式的循环依赖是无解的,Spring 容器会直接报异常。对于 setter 类型注入的循环依赖则可以顺利完成实例化并依次注入具体spring是怎样做到的,我们后面专门分析下
还有一个比较重要的接口BeanPostProcessor 接口,此接口有两个非常重要的方法,前置处理方法和后置处理方法,通过实现这个接口,我们就可以在bean实例化过程中,插入我们的逻辑,AOP就是在在这里完成织入逻辑的,我们一起来分析下AOP的织入流程
1:检查实现了PointcutAdvisor接口的实现类
2:当前的pointCut中的表达式是否能匹配到bean对象,如果匹配到了,就将此对象织入advice
3:将bean对象,bean实现的接口数组,advice对象传递给代理工厂,ProxyFactory,代理工厂创建AopProxy实现类,AopProxy实现类创建bean代理类,并最终返回这个代理类.返回的 bean 此时就不是原来的 bean 了,而是 bean 的代理类
public interface BeanPostProcessor {
/**
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
* initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
* or a custom init-method). The bean will already be populated with property values.
* The returned bean instance may be a wrapper around the original.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding {@code bean instanceof FactoryBean} checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other {@code BeanPostProcessor} callbacks.
* <p>The default implementation returns the given {@code bean} as-is.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one;
* if {@code null}, no subsequent BeanPostProcessors will be invoked
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
接下来,再说说AOP是怎样个IOC整合的
AOP生成代理类的逻辑其实是在AbstractAutoProxyCreator 这个抽象类的子类中实现的,DefaultAdvisorAutoProxyCreator、AspectJAwareAdvisorAutoProxyCreator 这两个子类都是,AbstractAutoProxyCreator 实现了 BeanPostProcessor 接口
这样 AbstractAutoProxyCreator 可以在 bean 初始化时就可以搞事情了,继承完这个之后我们是可以拿到bean对象了,但是还得需要切面对象,所以得实现BeanFactoryAware接口,这样的话,就可以获取到BeanFactory了,有了 BeanFactory,就可以获取 BeanFactory 中所有的切面对象了。有了目标对象 bean,所有的切面类,此时就可以为 bean 生成代理对象了
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
本期就是简单的叙述了下spring的生命周期,
Thanks!