spring源码阅读之BeanDefinition,解析文件,读取Bean,解析Bean,注册Bean

分为三个层次,核心、组件、应用

1 spring Framework(Core)核心,类似于操作系统的kernel,IOC容器和AOP模块。

用Ioc管理POJO对象,AOP动态和非侵入式的方式增强服务。

简化JavaEE体现在,为应用开发提供了许多即开即用的系统组件和服务,如事务处理、webMVC、jdbc、orm、远端调用,类似于操作系统的驱动。


* spring IOC  最基本的IOC容器BeanFactory的接口及实现,XMLBeanFactory,还有SimpleJndiBeanFactory、StaticListableBeanFactory,提供例如Resource访问资源的抽象和定位等外围支持。可利用上下文的容器ApplicationContext,实现FileSystemXmlApplicationContext等,对外提供更好的使用框架方式。

* spring AOP   集成了AspectJ作为一个特定实现,还在JVM动态代理/cglib基础上实现AOP框架。TransactionProxyFactory声明式事务处理就是通过AOP集成到spring中的。

* spring mvc

spring设计哲学

1 让javaEE开发变得更简单

2 让应用开发对接口编程,而不是对类编程。最大程度的降低开发者对SpringAPI的依赖。

3 POJO

依赖注入或者控制反转

将面向对象系统中的大部分用来处理数据的单件形式的有固定相互依赖关系的对象交于IOC容器管理,

Beanfactory是桶,BeanDefinition是水

Beanfactory所有的IOC容器都要竭诚这个接口

BeanDefinition对依赖反转模式中管理的对象及相互依赖关系的数据抽象,核心数据结构,



两条线

1  BeanFactory 定义了getName

HierarchicalBeanfactory定义了getParentBeanfactory具备双亲IOC容器的管理功能

ConfiturableBeanfactory定义了对BeanFactory的配置功能,如设置双亲IOC容器

2  ApplicationContext

ListableBeanFactory细化了Beanfactory的接口功能,比如定义了getBeanDefinitionNames(),另外MessageSource、 ResourceLoader、 ApplicationEventPublisher 接口,添加了高级特性

具体的IOC容器

XMLBeanfactory&DefaultListableBeanFactory,实现了ConfigurableBeanFactory,


FactoryBean能产生或者修饰 对象生成的工厂Bean它的实现与设计模式中的工厂模式和修饰器模式类似。 它是实现了FactoryBean<T>接口的Bean,根据该Bean的ID从BeanFactory中获取的实际上是FactoryBean的getObject()返回的对象,而不是FactoryBean本身,如果要获取FactoryBean对象,请在id前面加一个&符号来获取

Beanfactory主要定义了检索Bean的接口方法,如getBean,containsBean,isSinglton等

spring-beans目录下


在XmlBeanfactory中BeanDefinition由XmlBeanDefinitionReader读取信息来源ClassPathResource(xml文件),调用loadBeanDefinition方法完成。

编程使用IOC容器

ClassPathResource res = new ClassPathResource("beans.xml"); 
DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); 
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); r

reader.loadBeanDefinitions(res);

ApplicationContext 是一个高级形态意义的 IoC容器,

spring-Context目录下


1 支持不同的信息源,MessageSource,支持国际化的实现。2 访问资源,不同的IO源。

3 支持应用事件。ApplicationEventPublisher,支持在上下文中引入事件机制。这些 事件 和 Bean 的 生命 周期 的 结合 为 Bean 的 管理 提供 了 便利。

4 ApplicationContext本身提供的附加服务。

FileSystemXmlApplicationContext为例来说明。

在FileSystemXmlApplicationContext中,作为一个具体的应用上下文,只需要实现和它自身设计相关的两个功能。

一个功能是,如果应用直接使用FileSystemXmlApplicationContext,对于实例化这个应用上下文的支持,同时启动IoC容器的refresh()过程。这在FileSystemApplicationContext的代码实现中可以看到,代码如下:AbstractXMLApplicationContext实现了(我还没自己看上下文的具体实现)

 
public FileSystemXmlApplicationContext (
String[] configLocations , boolean refresh , @Nullable ApplicationContext parent)
throws BeansException {
super (parent) ;
setConfigLocations(configLocations) ;
if (refresh) {
refresh() ;
}
}

这个refresh()过程会牵涉IoC容器启动的一系列复杂操作,同时,对于不同的容器实现,这些操作都是类似的,因此在基类中将它们封装好。所以,我们在FileSystemXml的设计中看到的只是一个简单的调用。关于这个refresh()在IoC容器启动时的具体实现,是后面要分析。

另一个功能是与FileSystemXmlApplicationContext设计具体相关的功能,这部分与怎样从文件系统中加载XML的Bean定义资源有关。通过这个过程,可以为在文件系统中读取以XML形式存在的BeanDefinition做准备,因为不同的应用上下文实现对应着不同的读取BeanDefinition的方式,得到FileSystemResource的资源定位。

 
@Override
protected Resource getResourceByPath (String path) {
if (path.startsWith( "/" )) {
path = path.substring( 1 ) ;
}
return new FileSystemResource(path) ;
}

IOC容器的初始化过程

refresh()方法,启动IOC容器,包括BeanDefinition的资源定位、载入和注册。

编程式获取bean

  1. ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
  2. ac.getBean("userService");//比如:<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>

Resource定位

1 BeanDefinition 的 资源 定位, 它 由 ResourceLoader 通过 统一 的 Resource 接口 来,就 像用 水桶 装水 先 要把 水 找到

2 BeanDefinition的载入

3 向IOC容器中注册BeanDefinition,调用BeanDefinitionRegistry接口的实现完成,实际上是注入到一个HashMap中去的。

实际的依赖注入是getBean方法,但可以配置实现预实例化



这个FileSystemXmlApplicationContext已经通过继承Abstract-ApplicationContext具备了ResourceLoader读入以Resource定义的BeanDefinition的能力,因为AbstractApplicationContext的基类是DefaultResourceLoader。

对ApplicationContext进行初始化的模板和提纲

@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      prepareRefresh();
      // Tell the subclass to refresh the internal bean factory.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();//启动refreshBeanfactory
      // Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);
      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);Beanfactory后处理
         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);//调用BeanFactory的后处理器,这些后处理器是在Bean定义中向容器注册的
         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);注册Bean后处理器,在Bean创建过程中调用
         // Initialize message source for this context.
         initMessageSource();对上下文中的消息源进行初始化
         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();初始化上下文中的事件机制
         // Initialize other special beans in specific context subclasses.
         onRefresh();初始化其他特殊bean
         // Check for listener beans and register them.
         registerListeners();检查监听bean并向容器注册
         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);实例化所有no-lazyinit的单件
         // Last step: publish corresponding, event.
         finishRefresh();发布容器事件,结束refresh过程
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }
         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();//为防止Bean资源占用,在异常处理中,销毁已经在前面过程中生成的单件Bean
         // Reset 'active' flag.
         cancelRefresh(ex);重置active标志
         // Propagate exception to caller.
         throw ex;
      }
      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}

1   过程中, BeanDefinition 资源 的 定位原理,比较难找,自己去找吧。


protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   refreshBeanFactory();//构造Beanfactory ,DefaultListableBeanFactory,同时调用loadBeanDefinitions

   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (logger.isDebugEnabled()) {
      logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
   }
   return beanFactory;
}

在初始化FileSystmXmlApplicationContext的过程中,通过IoC容器的初始化的refresh来启动整个调用,使用的IoC容器是DefultListableBeanFactory。具体的资源载入在XmlBeanDefinitionReader读入BeanDefinition时完成,在XmlBeanDefinitionReader的基类AbstractBeanDefinitionReader中可以看到这个载入过程的具体实现。对载入过程的启动,可以在AbstractRefreshableApplicationContext的loadBeanDefinitions方法中看到,如代码清单2-5所示。

@Override
protected final void refreshBeanFactory() throws BeansException {
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
      DefaultListableBeanFactory beanFactory = createBeanFactory();
      beanFactory.setSerializationId(getId());
      customizeBeanFactory(beanFactory);
      loadBeanDefinitions(beanFactory);这里是使用BeanDefinitionReader载入Bean定义的地方,因为允许有多种载入方式,虽然用得//最多的是XML定义的形式,这里通过一个抽象函数把具体的实现委托给子类来完成
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}

AbstractXmlApplicationContext.java
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
   // Create a new XmlBeanDefinitionReader for the given BeanFactory.
   XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

   // Configure the bean definition reader with this context's
   // resource loading environment.
   beanDefinitionReader.setEnvironment(this.getEnvironment());
   beanDefinitionReader.setResourceLoader(this);
   beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

   // Allow a subclass to provide custom initialization of the reader,
   // then proceed with actually loading the bean definitions.
   initBeanDefinitionReader(beanDefinitionReader);
   loadBeanDefinitions(beanDefinitionReader);
}

2 BeanDefinition的载入解析


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans>
  3. <!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 -->
  4. <context:component-scan base-package="com.eduoinfo.finances.bank.web"> </context:component-scan>
  5. <!-- 引入jdbc配置文件 -->
  6. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  7. <property name="locations">
  8. <list>
  9. <value>classpath*:jdbc.properties </value>
  10. </list>
  11. </property>
  12. </bean>
  13. <!-- dataSource 配置 -->
  14. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  15. <!-- 基本属性 url、user、password -->
  16. <property name="url" value="${jdbc.url}" />
  17. <property name="username" value="${jdbc.username}" />
  18. <property name="password" value="${jdbc.password}" />
  19. <!-- 配置初始化大小、最小、最大 -->
  20. <property name="initialSize" value="1" />
  21. <property name="minIdle" value="1" />
  22. <property name="maxActive" value="20" />
  23. <!-- 配置获取连接等待超时的时间 -->
  24. <property name="maxWait" value="60000" />
  25. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
  26. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  27. <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
  28. <property name="minEvictableIdleTimeMillis" value="300000" />
  29. <property name="validationQuery" value="SELECT 'x'" />
  30. <property name="testWhileIdle" value="true" />
  31. <property name="testOnBorrow" value="false" />
  32. <property name="testOnReturn" value="false" />
  33. <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
  34. <property name="poolPreparedStatements" value="false" />
  35. <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
  36. <!-- 配置监控统计拦截的filters -->
  37. <property name="filters" value="stat" />
  38. </bean>
  39. <!-- mybatis文件配置,扫描所有mapper文件 -->
  40. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" />
  41. <!-- spring与mybatis整合配置,扫描所有dao -->
  42. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
  43. <!-- 对dataSource 数据源进行事务管理 -->
  44. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
  45. <!-- 配置使Spring采用CGLIB代理 -->
  46. <aop:aspectj-autoproxy proxy-target-class="true" />
  47. <!-- 启用对事务注解的支持 -->
  48. <tx:annotation-driven transaction-manager="transactionManager" />
  49. <!-- Cache配置 -->
  50. <cache:annotation-driven cache-manager="cacheManager" />
  51. <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
  52. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
  53. </bean

核心流程:Spring中对于applicationcontext.xml文件的解析流程大致如下


核心调用关系类图:入口是AbstractXmlApplicationContext类,调用关系图如下:


这时候的容器还没有完全起作用,要完全发挥容器的作用,还需完成数据向容器的注册。
经过这样逐层地解析,我们在XML文件中定义的BeanDefinition就被整个载入到了IoC容器中,并在容器中建立了数据映射。
这时候的容器还没有完全起作用,要完全发挥容器的作用,还需完成数据向容器的注册。


BeanDefinition在容器中的注册


图registerBeanDefinition 的 调用 关系

@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
      throws BeanDefinitionStoreException {

   Assert.hasText(beanName, "Bean name must not be empty");
   Assert.notNull(beanDefinition, "BeanDefinition must not be null");

   if (beanDefinition instanceof AbstractBeanDefinition) {
      try {
         ((AbstractBeanDefinition) beanDefinition).validate();
      }
      catch (BeanDefinitionValidationException ex) {
         throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
               "Validation of bean definition failed", ex);
      }
   }

   BeanDefinition oldBeanDefinition;

   oldBeanDefinition = this.beanDefinitionMap.get(beanName);
   if (oldBeanDefinition != null) {
      if (!isAllowBeanDefinitionOverriding()) {
         throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
               "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
               "': There is already [" + oldBeanDefinition + "] bound.");
      }
      else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
         // e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
         if (this.logger.isWarnEnabled()) {
            this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
                  "' with a framework-generated bean definition: replacing [" +
                  oldBeanDefinition + "] with [" + beanDefinition + "]");
         }
      }
      else if (!beanDefinition.equals(oldBeanDefinition)) {
         if (this.logger.isInfoEnabled()) {
            this.logger.info("Overriding bean definition for bean '" + beanName +
                  "' with a different definition: replacing [" + oldBeanDefinition +
                  "] with [" + beanDefinition + "]");
         }
      }
      else {
         if (this.logger.isDebugEnabled()) {
            this.logger.debug("Overriding bean definition for bean '" + beanName +
                  "' with an equivalent definition: replacing [" + oldBeanDefinition +
                  "] with [" + beanDefinition + "]");
         }
      }
      this.beanDefinitionMap.put(beanName, beanDefinition);
   }
   else {
      if (hasBeanCreationStarted()) {
         // Cannot modify startup-time collection elements anymore (for stable iteration)
         synchronized (this.beanDefinitionMap) {
            this.beanDefinitionMap.put(beanName, beanDefinition);
            List<String> updatedDefinitions = new ArrayList<>(this.beanDefinitionNames.size() + 1);
            updatedDefinitions.addAll(this.beanDefinitionNames);
            updatedDefinitions.add(beanName);
            this.beanDefinitionNames = updatedDefinitions;
            if (this.manualSingletonNames.contains(beanName)) {
               Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
               updatedSingletons.remove(beanName);
               this.manualSingletonNames = updatedSingletons;
            }
         }
      }
      else {
         // Still in startup registration phase
         this.beanDefinitionMap.put(beanName, beanDefinition);
         this.beanDefinitionNames.add(beanName);
         this.manualSingletonNames.remove(beanName);
      }
      this.frozenBeanDefinitionNames = null;
   }

   if (oldBeanDefinition != null || containsSingleton(beanName)) {
      resetBeanDefinition(beanName);
   }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值