花了几天时间来学习Spring,忽然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另外一方面要阅读源代码,这种方式理解起来事半功倍。那看书有什么用呢?主要仍是扩展视野,毕竟书是别人总结出来的东西,看一遍能够发现本身的理解误差,并且还能够看到一些平时不太关注的内容,固然看也能够是一种学习技术的方式。html
最开始只是想了解一下AOP,没想到就陷的这么深,为了搞清楚spring是如何完成切面功能这两天仍是把Ioc部分的内容也给读了读。仍是看懂了大概,只不过这复杂的内部结构确实不易理解与阅读,我在想Spring确实是个好的开源软件,但代码可能真的少了点亲近感。一个BeanFactory和FactroyBean就能够写上好几页纸来讲明,毕竟这些名字没有多少Ioc的影子。java
对于Ioc的功能就再也不多说,这里主要是理解一下Ioc的关键代码,至于BeanFactory、ApplicationContent、Resource之类就不说了,直接从getBean开始吧。正则表达式
从最基本的容器开始:spring
public interface ISay { void say(); void noaop(); } public class SayImpl implements ISay{ public void say() { System.out.print("我是5207."); } public void noaop() { System.out.println("别aop我"); } } public class Client { @SuppressWarnings("resource") public static void main(String[] args) { XmlBeanFactory benBeanFactory = new XmlBeanFactory(new ClassPathResource("aop/demo/spring.xml")); ISay say1 = (ISay) benBeanFactory.getBean("sayImpl"); say1.say(); say1.noaop(); } }
这里面有一个sayImpl的java代码,下面是spring.xml文件:编程
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="sayImpl" class="aop.demo.SayImpl"></bean> </beans>
运行Client的会获得下面的结果:缓存
我是5207.别aop我app
这里的XmlBeanFactory只是用来解析Xml文件而建立的类,它自己只是传递了一个Resource而已,最终的容器功能是在AbstractBeanFactory中完成,那么咱们直接就看AbstractBeanFactory中getBean的实现,而getBean又调用了doGetBean方法,这才是本尊。ide
下面是doGetBean的主要代码,代码太多,有些不是很重要的就删除了。函数
protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { final String beanName = transformedBeanName(name); Object bean; //先从缓存里查找单例的对象 Object sharedInstance = getSingleton(beanName); if (sharedInstance != null && args == null) { //若是对象存在则判断是否要经过FactoryBean来审批 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { //判断是否须要从父ParentBeanFactory得到对象 BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } } //先把当前bean依赖的bean给加载起来,经过getBean递归来完成 String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dependsOnBean : dependsOn) { getBean(dependsOnBean); registerDependentBean(dependsOnBean, beanName); } } if (mbd.isSingleton()) { //建立单实例的对象 sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { public Object getObject() throws BeansException { try { //这里是真正的实例化一个对象的地方 return createBean(beanName, mbd, args); } catch (BeansException ex) { destroySingleton(beanName); throw ex; } } }); //看看是否是FactoryBean,若是是的话要使用FactoryBean.getObject来返回对象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { // 若是是prototype,建立新对象 Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'"); } try { Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() { public Object getObject() throws BeansException { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; " + "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } return (T) bean; }
其实上面的代码中能够找出一段代码中完成核心调用的是createBean和getObjectForBeanInstance这两个方法。getObjectForBeanInstance的主要逻辑是post
看到这里就明白了FactoryBean的用处了吧,这个也是Spring Aop与Ioc进行交互的一个重要逻辑。
再来看看createBean
会发现createBean在AbstractBeanFactory中只是一个虚方法,说明是在派生类中实现的,前面是从XmlBeanFactory开始的,那么顺着它的继承关系看看,最终是在AbstractAutowireCapableBeanFactory中找到了createBean的实现:
@Override protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { ....省略. Object beanInstance = doCreateBean(beanName, mbd, args); ....省略. return beanInstance; } protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { BeanWrapper instanceWrapper = null; if (mbd.isSingleton()) { instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); Class<?> beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); ....省略. // Initialize the bean instance. Object exposedObject = bean; try { populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { ....省略. } ....省略. return exposedObject; }
代码太多了,找关键部分吧。createBean的过程主要是
首先是建立好bean,而后将新建立的bean实例并放在BeanWrapper中返回。
一、先是执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation,这里会有实例建立后的一个回调,若是是false会退出整个过程。
二、完成对象的注入autowireByName/autowireByType,这里就看到很眼熟的东西了吧“autowire”。
三、调用InstantiationAwareBeanPostProcessor的postProcessPropertyValues
四、最后是applyPropertyValues方法,把属性值都写入
一、先是invokeAwareMethods,完成BeanNameAware、BeanClassLoaderAware、BeanFactoryAware的注入
二、而后是BeanPostProcessor的postProcessBeforeInitialization调用
三、完成InitializingBean的调用
四、而后是BeanPostProcessor的postProcessAfterInitialization调用,这里就是后面Aop中Advistor接管对象的地方啦
能够看到initializeBean方法最后返回的是wrappedBean有两种可能,一种是通过BeanPostProcessor生成的对象,若是当前bean没有注册BeanPostProcessor的话就直接返回bean本身。aop中就运用这个方法来完成代码的切面编程。
小结
写到这Ioc建立一个对象的过程就说完了,过程当中发现了不少Ioc容器对一个对象生成过程的技巧与奥秘,也理解了为何能够在spring中灵活的完成对对象建立的各类“干预”。
好了,接下来开始进入spring aop部分,前面的例子作一些补充加入aop的功能,spring.xml以下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 声明被代理的目标对象 --> <bean id="sayImpl" class="aop.demo.SayImpl"></bean> <!-- 声明用于加强的拦截器对象 --> <bean id="sayImplAroundAdvice" class="aop.demo.SayImplAroundAdvice"></bean> <!-- 配置一个切面 --> <bean id="sayAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="sayImplAroundAdvice"/> <!-- 加强 --> <property name="pattern" value="aop.demo.SayImpl.s.*"/> <!-- 切点(正则表达式) --> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <property name="optimize" value="true"/> </bean> </beans>
这里有一个环绕加强及一个切面,最后使用DefaultAdvisorAutoProxyCreator来完成自动代理。前面在学习Ioc时使用的是XmlBeanFactory,是一个基本的基于xml的ioc容器,那么咱们再使用ClassPathXmlApplicationContext吧,看看会发生什么变化:
public class Client { @SuppressWarnings("resource") public static void main(String[] args) { XmlBeanFactory benBeanFactory = new XmlBeanFactory(new ClassPathResource("aop/demo/spring.xml")); ISay say1 = (ISay) benBeanFactory.getBean("sayImpl"); say1.say(); say1.noaop(); ApplicationContext context = new ClassPathXmlApplicationContext("aop/demo/spring.xml"); ISay say = (ISay)context.getBean("sayImpl"); say.say(); say.noaop(); } }
执行结果:
我是5207.别aop我
你们好:我是5207.但愿你们多多点赞.
别aop我
能够发现使用ClassPathXmlApplicationContext容器时aop的功能才有用,有点奇怪啊,都是读取的配置为啥结果不一样呢?这里主要是BeanFactory和ApplicationContext的主要区别之一。查看XmlBeanFactory的加载过程发现其只是调用了一个XmlBeanDefinitionReader读取了spring.xml并解析为BeanDefinition,而后具体的bean都要到getBean时才会去实例化,并且bean的依赖也要本身去处理。
那applicationContext呢?从ClassPathXmlApplicationContext的代码发现就复杂一些,在构造函数中有一个调用比较特别:
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }
看到这里发现多了一个refresh()的过程,进去一看一堆的处理。其实主要过程就是初始化Ioc容器,完成容器的配置,加载好并注册好各类回调。好比前面提到的BeanPostProcessor就是在这个refresh里就完成了注册。
正由于refresh这个过程咱们用起spring来才会以为简单好用。
有了上面的基础再来看DefaultAdvisorAutoProxyCreator就会简单的多。咱们能够看一下DefaultAdvisorAutoProxyCreator的类继承关系,其中有一个AbstractAutoProxyCreator须要特别关注。这个AbstractAutoProxyCreator实现了一堆的接口:
public abstract class AbstractAutoProxyCreator extends ProxyConfig implements SmartInstantiationAwareBeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, Ordered, AopInfrastructureBean {
其中SmartInstantiationAwareBeanPostProcessor继承关系中最底层的是BeanPostProcessor,前面说明Ioc的时候提到过,BeanPostProcessor能够在getBean时返回BeanPostProcessor加工过的对象。好了,奥秘就在这里啦,看一下AbstractAutoProxyCreator中如何实现的吧:
public Object postProcessBeforeInitialization(Object bean, String beanName) { return bean; } /** * Create a proxy with the configured interceptors if the bean is * identified as one to proxy by the subclass. * @see #getAdvicesAndAdvisorsForBean */ public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean != null) { Object cacheKey = getCacheKey(bean.getClass(), beanName); if (!this.earlyProxyReferences.containsKey(cacheKey)) { return wrapIfNecessary(bean, beanName, cacheKey); } } return bean; }
其中Before方法啥也没作,都在After中完成的。在After中有一个wrapIfNecessary方法,这个方法的做用是若是须要进行代理则生成代理并返回代理对象实例。
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) { if (beanName != null && this.targetSourcedBeans.containsKey(beanName)) { return bean; } if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) { return bean; } if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) { this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; } // Create proxy if we have advice. Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null); if (specificInterceptors != DO_NOT_PROXY) { this.advisedBeans.put(cacheKey, Boolean.TRUE); Object proxy = createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean)); this.proxyTypes.put(cacheKey, proxy.getClass()); return proxy; } this.advisedBeans.put(cacheKey, Boolean.FALSE); return bean; }
方法中使用了一个缓存advisedBeans存取bean是否须要advise,若是是须要的话就会存TRUE,不然就是FALSE,这样下次再来取时不用再判断直接返回结果就行。固然最重要的是若是是须要为bean生成代理时则会建立代理啦。至于如何判断是否须要代理是经过一个getAdvicesAndAdvisorsForBean方法来完成的,若是getAdvicesAndAdvisorsForBean返回的是DO_NOT_PROXY则表示不须要代理。为了扩展getAdvicesAndAdvisorsForBean是抽象方法。从DefaultAdvisorAutoProxyCreator的父类AbstractAdvisorAutoProxyCreator中找到了getAdvicesAndAdvisorsForBean的实现:
@Override protected Object[] getAdvicesAndAdvisorsForBean(Class beanClass, String beanName, TargetSource targetSource) { List advisors = findEligibleAdvisors(beanClass, beanName); if (advisors.isEmpty()) { return DO_NOT_PROXY; } return advisors.toArray(); }
这里面会查找全部的Advisors并返回,固然这个过程当中由于Advisor是包含了Advice的,因此Aop的基本要素就到齐了。而后回到wrapIfNecessary中,接着就会调用createProxy来生成代理对象啦。
protected Object createProxy( Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) { ProxyFactory proxyFactory = new ProxyFactory(); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); if (!shouldProxyTargetClass(beanClass, beanName)) { // Must allow for introductions; can't just set interfaces to // the target's interfaces only. Class<?>[] targetInterfaces = ClassUtils.getAllInterfacesForClass(beanClass, this.proxyClassLoader); for (Class<?> targetInterface : targetInterfaces) { proxyFactory.addInterface(targetInterface); } } Advisor[] advisors = buildAdvisors(beanName, specificInterceptors); for (Advisor advisor : advisors) { proxyFactory.addAdvisor(advisor); } proxyFactory.setTargetSource(targetSource); customizeProxyFactory(proxyFactory); proxyFactory.setFrozen(this.freezeProxy); if (advisorsPreFiltered()) { proxyFactory.setPreFiltered(true); } return proxyFactory.getProxy(this.proxyClassLoader); }
createProxy过程主要是经过ProxyFactory来生成代理对象,在以前的《学习AOP之深刻一点Spring Aop》里已经讲过。最后将生成的代理对象放入缓存并返回给Ioc容器。
从些以后,getBean时得到的已经不是beanName的实际对象,而是代理对象。