spring 3 解读 (笔记一期)

来源:互联网 发布:windows远程工具 编辑:程序博客网 时间:2024/05/21 08:00

 spring 3 -- 概念篇

 

笔者先用svn获取spring3的源代码:

其代码地址是:  https://src.springframework.org/svn/spring-framework/

 

Spring3源代码的组织结构:

build-spring-framework是整个源代码的构建目录,里面是项目的构建脚本,若要自己动手构建Spring,可以进入这个目录使用ant进行构建。

其他目录从名字上可以看得出Spring的各个组成部份,比如:

org.springframework.context  是Ioc容器源代码目录,

org.springframework.aop 是Aop实现的源代码目录,

 

Spring为我们提供的主要解决方案就是 IoC容器 AOP 支持 :

 

 IoC容器系列:

总接口类BeanFactory 是IoC容器的功能规范,提供IoC容器的最基本功能规范。

org.springframework.beans.factory
Interface BeanFactory

All Known Subinterfaces:
ApplicationContext,AutowireCapableBeanFactory,ConfigurableApplicationContext, ConfigurableBeanFactory, ConfigurableListableBeanFactory, ConfigurablePortletApplicationContext, ConfigurableWebApplicationContext, HierarchicalBeanFactory, ListableBeanFactory, WebApplicationContext
All Known Implementing Classes:
AbstractApplicationContext,AbstractAutowireCapableBeanFactory,AbstractBeanFactory, AbstractRefreshableApplicationContext, AbstractRefreshableConfigApplicationContext, AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AbstractXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, ClassPathXmlApplicationContext, DefaultListableBeanFactory, FileSystemXmlApplicationContext, GenericApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, SimpleJndiBeanFactory, StaticApplicationContext, StaticListableBeanFactory, StaticPortletApplicationContext, StaticWebApplicationContext, XmlBeanFactory, XmlPortletApplicationContext, XmlWebApplicationContext

在这些Spring提供的基本IoC容器中的接口和实现的基础上,Spring通过定义BeanDefinition来管理基于Spring的应用中的各种对象以及他们之间的依赖关系。

BeanDefinition抽象了我们对Bean的定义,是让容器起作用的主要数据类型,IoC的核心数据类型。

(用户使用容器时,可以使用转义字符 & 来得到FactoryBean本身,用来区分通过容器来获取FactoryBean产生的对象和获取FactoryBean本身。的Spring中所有的Bean都是由BeanFactory(IoC容器)来进行管理的,但对FactoryBean而言,这个Bean不是简单的Bean,他的实现与设计模式中的工厂模式装饰器模式 类似。)

BeanFactory方法:

booleancontainsBean(String name)
          Does this bean factory contain a bean with the given name? More specifically, isgetBean(java.lang.String) able to obtain a bean instance for the given name? String[]getAliases(String name)
          Return the aliases for the given bean name, if any.<T> TgetBean(Class<T> requiredType)
          Return the bean instance that uniquely matches the given object type, if any. ObjectgetBean(String name)
          Return an instance, which may be shared or independent, of the specified bean.<T> TgetBean(String name,Class<T> requiredType)
          Return an instance, which may be shared or independent, of the specified bean. ObjectgetBean(String name,Object... args)
          Return an instance, which may be shared or independent, of the specified bean. Class<?>getType(String name)
          Determine the type of the bean with the given name. booleanisPrototype(String name)
          Is this bean a prototype? That is, will getBean(java.lang.String) always return independent instances? booleanisSingleton(String name)
          Is this bean a shared singleton? That is, will getBean(java.lang.String) always return the same instance? booleanisTypeMatch(String name,Class targetType)
          Check whether the bean with the given name matches the specified type.

 

IoC容器XmlBeanFactroy的实现原理:

org.springframework.beans.factory.xml
Class XmlBeanFactory

java.lang.Object  extended by org.springframework.core.SimpleAliasRegistry      extended by org.springframework.beans.factory.support.DefaultSingletonBeanRegistry          extended by org.springframework.beans.factory.support.FactoryBeanRegistrySupport              extended by org.springframework.beans.factory.support.AbstractBeanFactory                  extended by org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory                      extended by org.springframework.beans.factory.support.DefaultListableBeanFactory                          extended by org.springframework.beans.factory.xml.XmlBeanFactory

 

此IoC容器可以读取以XML形式定义的BeanDefinition。在XmlBeanFactroy中,初始化中一个XmlBeanDefinitionReader对象,有了这个Reader对象,那些以XMl的方式定义的BeanDefinition就有了处理的地方,对这些XML形式的信息的处理是有这个XmlBeanDefinitionReader来完成的。

XmlBeanFactory的功能是建立在DfaultListableBeanFactory这个基本容器的基础上的,在这个容器的基础实现上实现了其他诸如XMl读取的附加功能。

XmlBeanFactory的实现源代码:

 private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
 /**
  * Create a new XmlBeanFactory with the given resource,
  * which must be parsable using DOM.
  * @param resource XML resource to load bean definitions from
  * @throws BeansException in case of loading or parsing errors
  */
 public XmlBeanFactory(Resource resource) throws BeansException {
  this(resource, null);
 }

 /**
  * Create a new XmlBeanFactory with the given input stream,
  * which must be parsable using DOM.
  * @param resource XML resource to load bean definitions from
  * @param parentBeanFactory parent bean factory
  * @throws BeansException in case of loading or parsing errors
  */
 public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
  super(parentBeanFactory);
  this.reader.loadBeanDefinitions(resource);
 }


 

举例 编程式 使用IoC容器:

ClassPathResource res = new ClassPathResource("beans.xml");

DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

XmlBeanFactroyReader reader = new XmlBeanFactroyReader(factory);

reader.loadBeanfinitions(res);

这样,我们就可以通过factory对象来使用DefaultListableBeanFactory这个IoC容器。在使用IoC容器,需要以下步骤:

1)创建IoC配置文件的抽象资源,此抽象资源包含了BeanDefinition的定义信息;

2)创建一个BeanFactory;

3)创建一个载入BeanDefinition的读取器;

4)成定义号的资源位置读入配置信息

 

 

ApplicaitonContext

扩展BeanFactory的 高级容器

org.springframework.context
Interface ApplicationContext

All Superinterfaces:
ApplicationEventPublisher,BeanFactory,HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader, ResourcePatternResolver
All Known Subinterfaces:
ConfigurableApplicationContext,ConfigurablePortletApplicationContext,ConfigurableWebApplicationContext, WebApplicationContext
All Known Implementing Classes:
AbstractApplicationContext,AbstractRefreshableApplicationContext,AbstractRefreshableConfigApplicationContext, AbstractRefreshablePortletApplicationContext, AbstractRefreshableWebApplicationContext, AbstractXmlApplicationContext, AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext, ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, GenericApplicationContext, GenericWebApplicationContext, GenericXmlApplicationContext, ResourceAdapterApplicationContext, StaticApplicationContext, StaticPortletApplicationContext, StaticWebApplicationContext, XmlPortletApplicationContext, XmlWebApplicationContext

ApplicaitonContext 提供了BeanFactory不具备的功能

 

 

 

IoC容器初次化:

 IoC容器的初始化包括BeanDefinition的Resource定位载入注册三个基本的过程。

Spring在实现中把这三个过程分开并使用不同的模块来完成,这样可以让用户更加灵活地对这三个过程进行减少或者扩展,定义出适合自己的IoC容器。


BeanDefinition的资料定位由ResourceLoader通过统一的Resource接口来完成,这个Resource对种种形式的BeanDefinition的使用提供了统一接口。对于这些BeanDefinition的存在形式,比如,在文件系统中的Bean定义信息可以使用FileSystemResource来进行抽象;在类路径中可以使用ClassPathResource来进行抽象,等等。这个过程类似于容器寻找数据的过程,就像用水桶装水先要把水找到一样。


第二个关键的部分是BeanDefinition的载入,此载入过程把用户定义好的Bean表示为IoC容器内部的数据结构,而这个容器内部的数据结构就是BeanDefinition。总的来说,这个BeanDefinition实际上就是POJO对象在IoC容器中的抽象,这个BeanDefinition定义了一系列的数据来使用IoC容器能够方便地对POJO对象也就是Spring的Bean进行管理 。即BeanDefinition就是Spring的领域对象。


第三个过程是向IoC容器注册这些BeanDefinition的过程。这个过程是通过调用BeanDefinition接口的实现来完成的,这个注册过程把载入过程中解析得到的BeanDefinition向IoC容器进行注册。可以看到在IoC容器内部,是通过使用一个HashMap来持有这些BeanDefinition数据的。


IoC容器的上下文的初始化一般不包含Bean依赖注入的实现。一般而言,依赖注入发生在应用第一次向容器通过getBean索取Bean时。但有一个例外,在使用IoC容器时有一个预实例化的配置,这个预实例化是可以配置的,具体来说可以通过在Bean定义信息中的lazyinit属性来设定;有了这个预实例化的特性,用户可以对容器的初始化过程作一个控制;从而改变这个被设置了lazyinit属性的Bean的依赖注入的发生,使得这个Bean的依赖注入在IoC容器初始化时就预先完成了。





 

 

 

 

 

 

 

原创粉丝点击