spring源码解读

来源:互联网 发布:exp数据库表结构导出 编辑:程序博客网 时间:2024/06/07 22:24

Spring技术内幕

深入解析Spring架构与设计原理(一)引子

我打算用这个帖子,把自己在这个过程中的一些心得,特别是对Spring新的理解,记录下来。使用这个帖子的标题,持续下来。

简单来说,自己的软件产品是一个基于互联网的SaaS协同软件平台,操作简单,支持流程定义,管理和多种客户端 -像短信,MSN,智能手机什么的(我这里就不多做什么广告了),也有一个企业版的版本,使用的技术框架是Hibernate + Spring + Wicket,下面是Linux和MySQL,还有云计算的平台的使用,以支持其扩展性,虽然现在还没有可扩展性的需求,但似乎不难从SaaS上,就会想 到云计算, 其实,它们真的是天生的一对!

关于云计算,自己对这个技术很感兴趣,觉得和开源软件的结合,是很有意思的,因为它们都有基于服务的基因,在云计算平台的使用上,也有一些初步的 实践。云计算是一个很有意思的话题,但在这里主要是想谈Spring,所以对云计算,这里就先不多说了,但非常欢迎有兴趣的朋友和一起另外找地方讨论!

回到正题,在我自己的产品中,其中除了Wicket和云计算外,其他都是大家非常熟知的了,像Hibernate, Spring, MySQL什么的。在这个过程中,发现自己对一些技术点也有了新的认识,最有体会的是Spring。当然,在这个过程中,更大的收获是对产品开发整个过程 的认识,在这点上,真是一言难尽........

回到自己还算了解的Spring, 这次我使用的是3.0的代码,所以,有机会也把这些代码读了几遍,比原来的理解要加深了许多,也发现了不少和2.0代码不同的地方,以及自己一些对 Spring的新的理解,这些,就让我就用这个帖子系列,给自己总结一下,也算是对自己以前的那个代码分析的帖子做一个新的交代吧。

自己对Spring一点小小的见解

简化Java企业应用的开发,是Spring框架的目标.就是我们熟知的当年的那个interface21,也亦非吴下阿蒙了,由它演进出来的 Spring,以及由它带来的崭新开发理念,也早已伴随着这个开源框架的广泛应用,而飞入寻常百姓家。与此同时,伴随着Spring的成熟,开源社区的成 长,在Rod.Johnson的领导下,以Spring为核心的一系列开源软件的产品组合,其脉络也逐渐的清晰和丰富起来;现在,已经发展成为一个包括软 件运行,构建,部署运营,从而涵盖整个软件服务生命周期的产品族群;同时也成为,在当今主流的软件业态中,一个不可或缺的重要组成。

在最近完成的VMware公司对Spring的运营者SpringSource公司的收购中,也让我们又看到了一个,在开源软件中,蕴含着的巨大 商业价值,以及又一次基于开源模式的商业成功;也让我们看到,Spring为自己设计的未来定位,它与云计算的融合趋势,以及,努力成为在云计算业态 中,PaaS(Platform As a Service)服务有力竞争者的战略设想;由此,可以想象,在云计算这个全新的计算时代中,如何秉承Spring的一贯风格,为云计算应用的开发,提供 高可靠,高可用,高可扩展,高性能的应用平台,对Spring团队来说,是一个面临的全新挑战;在这个领域中的雄心和今后的作为,那就让我们一起拭目以待 吧。这里也有点凑巧了,正好Spring和云计算都是自己喜欢的东西,说不定以后,我还能够在这两者的结合上再写些东西呢。

作为一个庞大的体系,Spring在Java企业应用中, 和我们熟悉的企业应用服务器一样,比如我们熟知的其他产品,像Weblogic,Websphere,JBoss,.NET这些等等,其定位和目的,都在 于希望能够起到一个企业应用资源的集成管理,以及为应用开发提供平台支持的作用,这和我们熟知的,像UNIX和Windows这样传统意义上的操作系统, 在传统的计算系统中,起到的作用非常的类似。只不过,按照个人的理解,它们不同在于,我们熟知的传统操作系统关心的是存储,计算,通信,外围设备这些物理 资源的管理,并在管理这些资源的基础上,为应用程序提供一个统一平台和服务接口;而像Spring这样的应用平台,它们关心的是在Java企业应用中,对 包括那些像Web应用,数据持久化,事务处理,消息中间件,分布式计算等等这些,为企业应用服务的抽象资源的统一管理,并在此基础上,为应用提供一个基于 POJO的开发环境。尽管各自面向的资源,管理的对象,支持的应用以及使用的场景不同,但这两者在整个系统中的定位,却依然有着可以类比和相互参考的地 方,从某种意义上看,它们都起到一个资源协调,平台支持,以及服务集成的作用。

所以我觉得可以使用,我们看待传统操作系统的方法和一些基本观念,来对Spring进行系统分析,以及对Spring进行层次划分,这样可能更加 容易理解,同时,所以,个人感觉,仿照传统操作系统的眼光,把对Spring框架的实现,划分为核心,组件和应用这三个基本的层次,来理解Spring框 架是不错的一个方法,就算是众所周知的“三段论”的应用吧。不知道这种分析方法,是不是太庸俗,但我自己还是觉得挺受用的,呵呵,谁叫我是个俗人呢!

今天先写一些,就算是起个头吧,明天继续! 写写IOC/AOP的一些具体东西。

 

 

深入解析Spring架构与设计原理(一)IOC实现原理

IOC的基础
下面我们从IOC/AOP开始,它们是Spring平台实现的核心部分;虽然,我们一开始大多只是在这个层面上,做一些配置和外部特性的使用工 作,但对这两个核心模块工作原理和运作机制的理解,对深入理解Spring平台,却是至关重要的;因为,它们同时也是Spring其他模块实现的基础。从 Spring要做到的目标,也就是从简化Java EE开发的出发点来看,简单的来说,它是通过对POJO开发的支持,来具体实现的;具体的说,Spring通过为应用开发提供基于POJO的开发模式,把 应用开发和复杂的Java EE服务,实现解耦,并通过提高单元测试的覆盖率,从而有效的提高整个应用的开发质量。这样一来,实际上,就需要把为POJO提供支持的,各种Java EE服务支持抽象到应用平台中去,去封装起来;而这种封装功能的实现,在Spring中,就是由IOC容器以及AOP来具体提供的,这两个模块,在很大程 度上,体现了Spring作为应用开发平台的核心价值。它们的实现,是Rod.Johnson在他的另一本著作《Expert One-on-One J2EE Development without EJB》 中,所提到Without EJB设计思想的体现;同时也深刻的体现了Spring背后的设计理念。

从更深一点的技术层面上来看,因为Spring是一个基于Java语言的应用平台,如果我们能够对Java计算模型,比如像JVM虚拟机实现技术 的基本原理有一些了解,会让我们对Spring实现的理解,更加的深入,这些JVM虚拟机的特性使用,包括像反射机制,代理类,字节码技术等等。它们都是 在Spring实现中,涉及到的一些Java计算环境的底层技术;尽管对应用开发人员来说,可能不会直接去涉及这些JVM虚拟机底层实现的工作,但是了解 这些背景知识,或多或少,对我们了解整个Spring平台的应用背景有很大的帮助;打个比方来说,就像我们在大学中,学习的那些关于计算机组织和系统方面 的基本知识,比如像数字电路,计算机组成原理,汇编语言,操作系统等等这些基本课程的学习。虽然,坦率的来说,对我们这些大多数课程的学习者,在以后的工 作中,可能并没有太多的机会,直接从事这么如此底层的技术开发工作;但具备这些知识背景,为我们深入理解基于这些基础技术构架起来的应用系统,毫无疑问, 是不可缺少的。随着JVM虚拟机技术的发展,可以设想到的是,更多虚拟机级别的基本特性,将会持续的被应用平台开发者所关注和采用,这也是我们在学习平台 实现的过程中,非常值得注意的一点,因为这些底层技术实现,毫无疑问,会对Spring应用平台的开发路线,产品策略产生重大的影响。同时,在使用 Spring作为应用平台的时候,如果需要更深层次的开发和性能调优,这些底层的知识,也是我们知识库中不可缺少的部分。有了这些底层知识,理解整个系 统,想来就应该障碍不大了。

IOC的一点认识
对Spring IOC的理解离不开对依赖反转模式的理解,我们知道,关于如何反转对依赖的控制,把控制权从具体业务对象手中转交到平台或者框架中,是解决面向对象系统设 计复杂性和提高面向对象系统可测试性的一个有效的解决方案。这个问题触发了IoC设计模式的发展,是IoC容器要解决的核心问题。同时,也是产品化的 IoC容器出现的推动力。而我觉得Spring的IoC容器,就是一个开源的实现依赖反转模式的产品。

那具体什么是IoC容器呢?它在Spring框架中到底长什么样?说了这么多,其实对IoC容器的使用者来说,我们常常接触到的 BeanFactory和ApplicationContext都可以看成是容器的具体表现形式。这些就是IoC容器,或者说在Spring中提IoC容 器,从实现来说,指的是一个容器系列。这也就是说,我们通常所说的IoC容器,如果深入到Spring的实现去看,会发现IoC容器实际上代表着一系列功 能各异的容器产品。只是容器的功能有大有小,有各自的特点。打个比方来说,就像是百货商店里出售的商品,我们举水桶为例子,在商店中出售的水桶有大有小; 制作材料也各不相同,有金属的,有塑料的等等,总之是各式各样,但只要能装水,具备水桶的基本特性,那就可以作为水桶来出售来让用户使用。这在 Spring中也是一样,它有各式各样的IoC容器的实现供用户选择和使用;使用什么样的容器完全取决于用户的需要,但在使用之前如果能够了解容器的基本 情况,那会对容器的使用是非常有帮助的;就像我们在购买商品时进行的对商品的考察和挑选那样。

我们从最基本的XmlBeanFactory看起,它是容器系列的最底层实现,这个容器的实现与我们在Spring应用中用到的那些上下文相比, 有一个非常明显的特点,它只提供了最基本的IoC容器的功能。从它的名字中可以看出,这个IoC容器可以读取以XML形式定义的 BeanDefinition。理解这一点有助于我们理解ApplicationContext与基本的BeanFactory之间的区别和联系。我们可 以认为直接的BeanFactory实现是IoC容器的基本形式,而各种ApplicationContext的实现是IoC容器的高级表现形式。

仔细阅读XmlBeanFactory的源码,在一开始的注释里面已经对 XmlBeanFactory的功能做了简要的说明,从代码的注释还可以看到,这是Rod Johnson在2001年就写下的代码,可见这个类应该是Spring的元老类了。它是继承DefaultListableBeanFactory这个 类的,这个DefaultListableBeanFactory就是一个很值得注意的容器!

Java代码

1. public class XmlBeanFactory extends DefaultListableBeanFactory {  

2.     private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);  

3.     public XmlBeanFactory(Resource resource) throws BeansException {  

4.         this(resource, null);  

5.     }  

6.     public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {  

7.         super(parentBeanFactory);  

8.         this.reader.loadBeanDefinitions(resource);  

9.     }  

10. }  

public class XmlBeanFactory extends DefaultListableBeanFactory {

private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

public XmlBeanFactory(Resource resource) throws BeansException {

this(resource, null);

}

public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {

super(parentBeanFactory);

this.reader.loadBeanDefinitions(resource);

}

}




XmlBeanFactory的功能是建立在DefaultListableBeanFactory这个基本容器的基础上的,在这个基本容器的基 础上实现了其他诸如XML读取的附加功能。对于这些功能的实现原理,看一看XmlBeanFactory的代码实现就能很容易地理解。在如下的代码中可以 看到,在XmlBeanFactory构造方法中需要得到Resource对象。对XmlBeanDefinitionReader对象的初始化,以及使 用这个这个对象来完成loadBeanDefinitions的调用,就是这个调用启动了从Resource中载入BeanDefinitions的过 程,这个loadBeanDefinitions同时也是IoC容器初始化的重要组成部分。

简单来说,IoC容器的初始化包括BeanDefinition的Resouce定位、载入和注册这三个基本的过程。我觉得重点是在载入和对 BeanDefinition做解析的这个过程。可以从DefaultListableBeanFactory来入手看看IoC容器是怎样完成 BeanDefinition载入的。在refresh调用完成以后,可以看到loadDefinition的调用:

Java代码

1. public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {  

2.     public AbstractXmlApplicationContext() {  

3.     }  

4.     public AbstractXmlApplicationContext(ApplicationContext parent) {  

5.         super(parent);  

6.     }  

7.     //这里是实现loadBeanDefinitions的地方  

8.     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {  

9.         // Create a new XmlBeanDefinitionReader for the given BeanFactory.  

10.         // 创建 XmlBeanDefinitionReader,并通过回调设置到 BeanFactory中去,创建BeanFactory的使用的也是 DefaultListableBeanFactory。  

11.         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  

12.   

13.         // Configure the bean definition reader with this context's  

14.         // resource loading environment.  

15.         // 这里设置 XmlBeanDefinitionReader, 为XmlBeanDefinitionReader 配置ResourceLoader,因为DefaultResourceLoader是父类,所以this可以直接被使用  

16.         beanDefinitionReader.setResourceLoader(this);  

17.         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  

18.   

19.         // Allow a subclass to provide custom initialization of the reader,  

20.         // then proceed with actually loading the bean definitions.  

21.     // 这是启动Bean定义信息载入的过程  

22.         initBeanDefinitionReader(beanDefinitionReader);  

23.         loadBeanDefinitions(beanDefinitionReader);  

24.     }  

25.   

26.     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {  

27.     }  

public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {

public AbstractXmlApplicationContext() {

}

public AbstractXmlApplicationContext(ApplicationContext parent) {

super(parent);

}

//这里是实现loadBeanDefinitions的地方

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {

// Create a new XmlBeanDefinitionReader for the given BeanFactory.

// 创建 XmlBeanDefinitionReader,并通过回调设置到 BeanFactory中去,创建BeanFactory的使用的也是 DefaultListableBeanFactory。

XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

 

// Configure the bean definition reader with this context's

// resource loading environment.

// 这里设置 XmlBeanDefinitionReader, 为XmlBeanDefinitionReader 配置ResourceLoader,因为DefaultResourceLoader是父类,所以this可以直接被使用

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.

// 这是启动Bean定义信息载入的过程

initBeanDefinitionReader(beanDefinitionReader);

loadBeanDefinitions(beanDefinitionReader);

}

 

protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

}


这里使用 XmlBeanDefinitionReader来载入BeanDefinition到容器中,如以下代码清单所示:

Java代码

1.     //这里是调用的入口。  

2.     public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {  

3.         return loadBeanDefinitions(new EncodedResource(resource));  

4.     }  

5.     //这里是载入XML形式的BeanDefinition的地方。  

6.     public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {  

7.         Assert.notNull(encodedResource, "EncodedResource must not be null");  

8.         if (logger.isInfoEnabled()) {  

9.             logger.info("Loading XML bean definitions from " + encodedResource.getResource());  

10.         }  

11.   

12.         Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();  

13.         if (currentResources == null) {  

14.             currentResources = new HashSet<EncodedResource>(4);  

15.             this.resourcesCurrentlyBeingLoaded.set(currentResources);  

16.         }  

17.         if (!currentResources.add(encodedResource)) {  

18.             throw new BeanDefinitionStoreException(  

19.                     "Detected recursive loading of " + encodedResource + " - check your import definitions!");  

20.         }  

21.         //这里得到XML文件,并得到IO的InputSource准备进行读取。  

22.         try {  

23.             InputStream inputStream = encodedResource.getResource().getInputStream();  

24.             try {  

25.                 InputSource inputSource = new InputSource(inputStream);  

26.                 if (encodedResource.getEncoding() != null) {  

27.                     inputSource.setEncoding(encodedResource.getEncoding());  

28.                 }  

29.                 return doLoadBeanDefinitions(inputSource, encodedResource.getResource());  

30.             }  

31.             finally {  

32.                 inputStream.close();  

33.             }  

34.         }  

35.         catch (IOException ex) {  

36.             throw new BeanDefinitionStoreException(  

37.                     "IOException parsing XML document from " + encodedResource.getResource(), ex);  

38.         }  

39.         finally {  

40.             currentResources.remove(encodedResource);  

41.             if (currentResources.isEmpty()) {  

42.                 this.resourcesCurrentlyBeingLoaded.set(null);  

43.             }  

44.         }  

45.     }  

46. //具体的读取过程可以在doLoadBeanDefinitions方法中找到:  

47.     //这是从特定的XML文件中实际载入BeanDefinition的地方  

48.     protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)  

49.             throws BeanDefinitionStoreException {  

50.         try {  

51.             int validationMode = getValidationModeForResource(resource);  

52.             //这里取得XML文件的Document对象,这个解析过程是由 documentLoader完成的,这个documentLoader是DefaultDocumentLoader,在定义documentLoader的地方创建  

53.             Document doc = this.documentLoader.loadDocument(  

54.                     inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());  

55.             //这里启动的是对BeanDefinition解析的详细过程,这个解析会使用到Spring的Bean配置规则,是我们下面需要详细关注的地方。  

56.             return registerBeanDefinitions(doc, resource);  

57.         }  

58.         catch (BeanDefinitionStoreException ex) {  

59.             throw ex;  

60.         }  

61.         catch (SAXParseException ex) {  

62.             throw new XmlBeanDefinitionStoreException(resource.getDescription(),  

63.                     "Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);  

64.         }  

65.         catch (SAXException ex) {  

66.             throw new XmlBeanDefinitionStoreException(resource.getDescription(),  

67.                     "XML document from " + resource + " is invalid", ex);  

68.         }  

69.         catch (ParserConfigurationException ex) {  

70.             throw new BeanDefinitionStoreException(resource.getDescription(),  

71.                     "Parser configuration exception parsing XML from " + resource, ex);  

72.         }  

73.         catch (IOException ex) {  

74.             throw new BeanDefinitionStoreException(resource.getDescription(),  

75.                     "IOException parsing XML document from " + resource, ex);  

76.         }  

77.         catch (Throwable ex) {  

78.             throw new BeanDefinitionStoreException(resource.getDescription(),  

79.                     "Unexpected exception parsing XML document from " + resource, ex);  

80.         }  

81.     }  

//这里是调用的入口。

public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {

return loadBeanDefinitions(new EncodedResource(resource));

}

//这里是载入XML形式的BeanDefinition的地方。

public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {

Assert.notNull(encodedResource, "EncodedResource must not be null");

if (logger.isInfoEnabled()) {

logger.info("Loading XML bean definitions from " + encodedResource.getResource());

}

 

Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();

if (currentResources == null) {

currentResources = new HashSet<EncodedResource>(4);

this.resourcesCurrentlyBeingLoaded.set(currentResources);

}

if (!currentResources.add(encodedResource)) {

throw new BeanDefinitionStoreException(

"Detected recursive loading of " + encodedResource + " - check your import definitions!");

}

//这里得到XML文件,并得到IO的InputSource准备进行读取。

try {

InputStream inputStream = encodedResource.getResource().getInputStream();

try {

InputSource inputSource = new InputSource(inputStream);

if (encodedResource.getEncoding() != null) {

inputSource.setEncoding(encodedResource.getEncoding());

}

return doLoadBeanDefinitions(inputSource, encodedResource.getResource());

}

finally {

inputStream.close();

}

}

catch (IOException ex) {

throw new BeanDefinitionStoreException(

"IOException parsing XML document from " + encodedResource.getResource(), ex);

}

finally {

currentResources.remove(encodedResource);

if (currentResources.isEmpty()) {

this.resourcesCurrentlyBeingLoaded.set(null);

}

}

}

//具体的读取过程可以在doLoadBeanDefinitions方法中找到:

//这是从特定的XML文件中实际载入BeanDefinition的地方

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)

throws BeanDefinitionStoreException {

try {

int validationMode = getValidationModeForResource(resource);

//这里取得XML文件的Document对象,这个解析过程是由 documentLoader完成的,这个documentLoader是DefaultDocumentLoader,在定义documentLoader的地方创建

Document doc = this.documentLoader.loadDocument(

inputSource, getEntityResolver(), this.errorHandler, validationMode, isNamespaceAware());

//这里启动的是对BeanDefinition解析的详细过程,这个解析会使用到Spring的Bean配置规则,是我们下面需要详细关注的地方。

return registerBeanDefinitions(doc, resource);

}

catch (BeanDefinitionStoreException ex) {

throw ex;

}

catch (SAXParseException ex) {

throw new XmlBeanDefinitionStoreException(resource.getDescription(),

"Line " + ex.getLineNumber() + " in XML document from " + resource + " is invalid", ex);

}

catch (SAXException ex) {

throw new XmlBeanDefinitionStoreException(resource.getDescription(),

"XML document from " + resource + " is invalid", ex);

}

catch (ParserConfigurationException ex) {

throw new BeanDefinitionStoreException(resource.getDescription(),

"Parser configuration exception parsing XML from " + resource, ex);

}

catch (IOException ex) {

throw new BeanDefinitionStoreException(resource.getDescription(),

"IOException parsing XML document from " + resource, ex);

}

catch (Throwable ex) {

throw new BeanDefinitionStoreException(resource.getDescription(),

"Unexpected exception parsing XML document from " + resource, ex);

}

}


关于具体的Spring BeanDefinition的解析,是在BeanDefinitionParserDelegate中完成的。这个类里包含了各种Spring Bean定义规则的处理,感兴趣的同学可以仔细研究。我们举一个例子来分析这个处理过程,比如我们最熟悉的对Bean元素的处理是怎样完成的,也就是我们 在XML定义文件中出现的<bean></bean>这个最常见的元素信息是怎样被处理的。在这里,我们会看到那些熟悉的 BeanDefinition定义的处理,比如id、name、aliase等属性元素。把这些元素的值从XML文件相应的元素的属性中读取出来以后,会 被设置到生成的BeanDefinitionHolder中去。这些属性的解析还是比较简单的。对于其他元素配置的解析,比如各种Bean的属性配置,通 过一个较为复杂的解析过程,这个过程是由parseBeanDefinitionElement来完成的。解析完成以后,会把解析结果放到 BeanDefinition对象中并设置到BeanDefinitionHolder中去,如以下清单所示:

Java代码

1. public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {  

2.         //这里取得在<bean>元素中定义的id、name和aliase属性的值  

3.         String id = ele.getAttribute(ID_ATTRIBUTE);  

4.         String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);  

5.   

6.         List<String> aliases = new ArrayList<String>();  

7.         if (StringUtils.hasLength(nameAttr)) {  

8.             String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, BEAN_NAME_DELIMITERS);  

9.             aliases.addAll(Arrays.asList(nameArr));  

10.         }  

11.   

12.         String beanName = id;  

13.         if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {  

14.             beanName = aliases.remove(0);  

15.             if (logger.isDebugEnabled()) {  

16.                 logger.debug("No XML 'id' specified - using '" + beanName +  

17.                         "' as bean name and " + aliases + " as aliases");  

18.             }  

19.         }  

20.   

21.         if (containingBean == null) {  

22.             checkNameUniqueness(beanName, aliases, ele);  

23.         }  

24.   

25.         //这个方法会引发对bean元素的详细解析  

26. AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);  

27.         if (beanDefinition != null) {  

28.             if (!StringUtils.hasText(beanName)) {  

29.                 try {  

30.                     if (containingBean != null) {  

31.                         beanName = BeanDefinitionReaderUtils.generateBeanName(  

32.                                 beanDefinition, this.readerContext.getRegistry(), true);  

33.                     }  

34.                     else {  

35.                         beanName = this.readerContext.generateBeanName(beanDefinition);  

36.                         // Register an alias for the plain bean class name, if still possible,  

37.                         // if the generator returned the class name plus a suffix.  

38.                         // This is expected for Spring 1.2/2.0 backwards compatibility.  

39.                         String beanClassName = beanDefinition.getBeanClassName();  

40.                         if (beanClassName != null &&  

41.                                 beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&  

42.                                 !this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {  

43.                             aliases.add(beanClassName);  

44.                         }  

45.                     }  

46.                     if (logger.isDebugEnabled()) {  

47.                         logger.debug("Neither XML 'id' nor 'name' specified - " +  

48.                                 "using generated bean name [" + beanName + "]");  

49.                     }  

50.                 }  

51.                 catch (Exception ex) {  

52.                     error(ex.getMessage(), ele);  

53.                     return null;  

54.                 }  

55.             }  

56.             String[] aliasesArray = StringUtils.toStringArray(aliases);  

57.             return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);  

58.         }  

59.   

60.         return null;  

61.     }  

public BeanDefinitionHolder parseBeanDefinitionElement(Element ele, BeanDefinition containingBean) {

//这里取得在<bean>元素中定义的id、name和aliase属性的值

String id = ele.getAttribute(ID_ATTRIBUTE);

String nameAttr = ele.getAttribute(NAME_ATTRIBUTE);

 

List<String> aliases = new ArrayList<String>();

if (StringUtils.hasLength(nameAttr)) {

String[] nameArr = StringUtils.tokenizeToStringArray(nameAttr, BEAN_NAME_DELIMITERS);

aliases.addAll(Arrays.asList(nameArr));

}

 

String beanName = id;

if (!StringUtils.hasText(beanName) && !aliases.isEmpty()) {

beanName = aliases.remove(0);

if (logger.isDebugEnabled()) {

logger.debug("No XML 'id' specified - using '" + beanName +

"' as bean name and " + aliases + " as aliases");

}

}

 

if (containingBean == null) {

checkNameUniqueness(beanName, aliases, ele);

}

 

//这个方法会引发对bean元素的详细解析

AbstractBeanDefinition beanDefinition = parseBeanDefinitionElement(ele, beanName, containingBean);

if (beanDefinition != null) {

if (!StringUtils.hasText(beanName)) {

try {

if (containingBean != null) {

beanName = BeanDefinitionReaderUtils.generateBeanName(

beanDefinition, this.readerContext.getRegistry(), true);

}

else {

beanName = this.readerContext.generateBeanName(beanDefinition);

// Register an alias for the plain bean class name, if still possible,

// if the generator returned the class name plus a suffix.

// This is expected for Spring 1.2/2.0 backwards compatibility.

String beanClassName = beanDefinition.getBeanClassName();

if (beanClassName != null &&

beanName.startsWith(beanClassName) && beanName.length() > beanClassName.length() &&

!this.readerContext.getRegistry().isBeanNameInUse(beanClassName)) {

aliases.add(beanClassName);

}

}

if (logger.isDebugEnabled()) {

logger.debug("Neither XML 'id' nor 'name' specified - " +

"using generated bean name [" + beanName + "]");

}

}

catch (Exception ex) {

error(ex.getMessage(), ele);

return null;

}

}

String[] aliasesArray = StringUtils.toStringArray(aliases);

return new BeanDefinitionHolder(beanDefinition, beanName, aliasesArray);

}

 

return null;

}


在具体生成BeanDefinition以后。我们举一个对property进行解析的例子来完成对整个BeanDefinition载入过程的 分析,还是在类BeanDefinitionParserDelegate的代码中,它对BeanDefinition中的定义一层一层地进行解析,比如 从属性元素集合到具体的每一个属性元素,然后才是对具体的属性值的处理。根据解析结果,对这些属性值的处理会封装成PropertyValue对象并设置 到BeanDefinition对象中去,如以下代码清单所示。

Java代码

1. /** 

2.  * 这里对指定bean元素的property子元素集合进行解析。 

3.  */  

4. public void parsePropertyElements(Element beanEle, BeanDefinition bd) {  

5.     //遍历所有bean元素下定义的property元素  

6.     NodeList nl = beanEle.getChildNodes();  

7.     for (int i = 0; i < nl.getLength(); i++) {  

8.         Node node = nl.item(i);  

9.         if (node instanceof Element && DomUtils.nodeNameEquals(node, PROPERTY_ELEMENT)) {  

10.             //在判断是property元素后对该property元素进行解析的过程  

11.             parsePropertyElement((Element) node, bd);  

12.         }  

13.     }  

14. }  

15. public void parsePropertyElement(Element ele, BeanDefinition bd) {  

16.     //这里取得property的名字  

17.     String propertyName = ele.getAttribute(NAME_ATTRIBUTE);  

18.     if (!StringUtils.hasLength(propertyName)) {  

19.         error("Tag 'property' must have a 'name' attribute", ele);  

20.         return;  

21.     }  

22.     this.parseState.push(new PropertyEntry(propertyName));  

23.     try {  

24.         //如果同一个bean中已经有同名的存在,则不进行解析,直接返回。也就是说,如果在同一个bean中有同名的property设置,那么起作用的只是第一个。  

25.         if (bd.getPropertyValues().contains(propertyName)) {  

26.             error("Multiple 'property' definitions for property '" + propertyName + "'", ele);  

27.             return;  

28.         }  

29.         //这里是解析property值的地方,返回的对象对应对Bean定义的property属性设置的解析结果,这个解析结果会封装到PropertyValue对象中,然后设置到BeanDefinitionHolder中去。  

30.         Object val = parsePropertyValue(ele, bd, propertyName);  

31.         PropertyValue pv = new PropertyValue(propertyName, val);  

32.         parseMetaElements(ele, pv);  

33.         pv.setSource(extractSource(ele));  

34.         bd.getPropertyValues().addPropertyValue(pv);  

35.     }  

36.     finally {  

37.         this.parseState.pop();  

38.     }  

39. }  

40. /** 

41.  * 这里取得property元素的值,也许是一个list或其他。 

42.  */  

43. public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {  

44.     String elementName = (propertyName != null) ?  

45.                     "<property> element for property '" + propertyName + "'" :  

46.                     "<constructor-arg> element";  

47.   

48.     // Should only have one child element: ref, value, list, etc.  

49.     NodeList nl = ele.getChildNodes();  

50.     Element subElement = null;  

51.     for (int i = 0; i < nl.getLength(); i++) {  

52.         Node node = nl.item(i);  

53.         if (node instanceof Element && !DomUtils.nodeNameEquals(node, DESCRIPTION_ELEMENT) &&  

54.                 !DomUtils.nodeNameEquals(node, META_ELEMENT)) {  

55.             // Child element is what we're looking for.  

56.             if (subElement != null) {  

57.                 error(elementName + " must not contain more than one sub-element", ele);  

58.             }  

59.             else {  

60.                 subElement = (Element) node;  

61.             }  

62.         }  

63.     }  

64.     //这里判断property的属性,是ref还是value,不允许同时是ref和value。  

65.     boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);  

66.     boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);  

67.     if ((hasRefAttribute && hasValueAttribute) ||  

68.             ((hasRefAttribute || hasValueAttribute) && subElement != null)) {  

69.         error(elementName +  

70.                 " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);  

71.     }  

72.     //如果是ref,创建一个ref的数据对象RuntimeBeanReference,这个对象封装了ref的信息。  

73.     if (hasRefAttribute) {  

74.         String refName = ele.getAttribute(REF_ATTRIBUTE);  

75.         if (!StringUtils.hasText(refName)) {  

76.             error(elementName + " contains empty 'ref' attribute", ele);  

77.         }  

78.         RuntimeBeanReference ref = new RuntimeBeanReference(refName);  

79.         ref.setSource(extractSource(ele));  

80.         return ref;  

81.     } //如果是value,创建一个value的数据对象TypedStringValue ,这个对象封装了value的信息。  

82.     else if (hasValueAttribute) {  

83.         TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));  

84.         valueHolder.setSource(extractSource(ele));  

85.         return valueHolder;  

86.     } //如果还有子元素,触发对子元素的解析  

87.     else if (subElement != null) {  

88.         return parsePropertySubElement(subElement, bd);  

89.     }  

90.     else {  

91.         // Neither child element nor "ref" or "value" attribute found.  

92.         error(elementName + " must specify a ref or value", ele);  

93.         return null;  

94.     }  

95. }  

/**

 * 这里对指定bean元素的property子元素集合进行解析。

 */

public void parsePropertyElements(Element beanEle, BeanDefinition bd) {

//遍历所有bean元素下定义的property元素

NodeList nl = beanEle.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {

Node node = nl.item(i);

if (node instanceof Element && DomUtils.nodeNameEquals(node, PROPERTY_ELEMENT)) {

//在判断是property元素后对该property元素进行解析的过程

parsePropertyElement((Element) node, bd);

}

}

}

public void parsePropertyElement(Element ele, BeanDefinition bd) {

//这里取得property的名字

String propertyName = ele.getAttribute(NAME_ATTRIBUTE);

if (!StringUtils.hasLength(propertyName)) {

error("Tag 'property' must have a 'name' attribute", ele);

return;

}

this.parseState.push(new PropertyEntry(propertyName));

try {

//如果同一个bean中已经有同名的存在,则不进行解析,直接返回。也就是说,如果在同一个bean中有同名的property设置,那么起作用的只是第一个。

if (bd.getPropertyValues().contains(propertyName)) {

error("Multiple 'property' definitions for property '" + propertyName + "'", ele);

return;

}

//这里是解析property值的地方,返回的对象对应对Bean定义的property属性设置的解析结果,这个解析结果会封装到PropertyValue对象中,然后设置到BeanDefinitionHolder中去。

Object val = parsePropertyValue(ele, bd, propertyName);

PropertyValue pv = new PropertyValue(propertyName, val);

parseMetaElements(ele, pv);

pv.setSource(extractSource(ele));

bd.getPropertyValues().addPropertyValue(pv);

}

finally {

this.parseState.pop();

}

}

/**

 * 这里取得property元素的值,也许是一个list或其他。

 */

public Object parsePropertyValue(Element ele, BeanDefinition bd, String propertyName) {

String elementName = (propertyName != null) ?

"<property> element for property '" + propertyName + "'" :

"<constructor-arg> element";

 

// Should only have one child element: ref, value, list, etc.

NodeList nl = ele.getChildNodes();

Element subElement = null;

for (int i = 0; i < nl.getLength(); i++) {

Node node = nl.item(i);

if (node instanceof Element && !DomUtils.nodeNameEquals(node, DESCRIPTION_ELEMENT) &&

!DomUtils.nodeNameEquals(node, META_ELEMENT)) {

// Child element is what we're looking for.

if (subElement != null) {

error(elementName + " must not contain more than one sub-element", ele);

}

else {

subElement = (Element) node;

}

}

}

//这里判断property的属性,是ref还是value,不允许同时是ref和value。

boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE);

boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE);

if ((hasRefAttribute && hasValueAttribute) ||

((hasRefAttribute || hasValueAttribute) && subElement != null)) {

error(elementName +

" is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element", ele);

}

//如果是ref,创建一个ref的数据对象RuntimeBeanReference,这个对象封装了ref的信息。

if (hasRefAttribute) {

String refName = ele.getAttribute(REF_ATTRIBUTE);

if (!StringUtils.hasText(refName)) {

error(elementName + " contains empty 'ref' attribute", ele);

}

RuntimeBeanReference ref = new RuntimeBeanReference(refName);

ref.setSource(extractSource(ele));

return ref;

} //如果是value,创建一个value的数据对象TypedStringValue ,这个对象封装了value的信息。

else if (hasValueAttribute) {

TypedStringValue valueHolder = new TypedStringValue(ele.getAttribute(VALUE_ATTRIBUTE));

valueHolder.setSource(extractSource(ele));

return valueHolder;

} //如果还有子元素,触发对子元素的解析

else if (subElement != null) {

return parsePropertySubElement(subElement, bd);

}

else {

// Neither child element nor "ref" or "value" attribute found.

error(elementName + " must specify a ref or value", ele);

return null;

}

}


比如,再往下看,我们看到像List这样的属性配置是怎样被解析的,依然在BeanDefinitionParserDelegate中:返回的 是一个List对象,这个List是Spring定义的ManagedList,作为封装List这类配置定义的数据封装,如以下代码清单所示。

Java代码

1. public List parseListElement(Element collectionEle, BeanDefinition bd) {  

2.     String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);  

3.     NodeList nl = collectionEle.getChildNodes();  

4.     ManagedList<Object> target = new ManagedList<Object>(nl.getLength());  

5.     target.setSource(extractSource(collectionEle));  

6.     target.setElementTypeName(defaultElementType);  

7.     target.setMergeEnabled(parseMergeAttribute(collectionEle));  

8.     //具体的List元素的解析过程。  

9.     parseCollectionElements(nl, target, bd, defaultElementType);  

10.     return target;  

11. }  

12. protected void parseCollectionElements(  

13.         NodeList elementNodes, Collection<Object> target, BeanDefinition bd, String defaultElementType) {  

14.     //遍历所有的元素节点,并判断其类型是否为Element。  

15.     for (int i = 0; i < elementNodes.getLength(); i++) {  

16.         Node node = elementNodes.item(i);  

17.         if (node instanceof Element && !DomUtils.nodeNameEquals(node, DESCRIPTION_ELEMENT)) {  

18.     //加入到target中去,target是一个ManagedList,同时触发对下一层子元素的解析过程,这是一个递归的调用。  

19.             target.add(parsePropertySubElement((Element) node, bd, defaultElementType));  

20.         }  

21.     }  

22. }  

public List parseListElement(Element collectionEle, BeanDefinition bd) {

String defaultElementType = collectionEle.getAttribute(VALUE_TYPE_ATTRIBUTE);

NodeList nl = collectionEle.getChildNodes();

ManagedList<Object> target = new ManagedList<Object>(nl.getLength());

target.setSource(extractSource(collectionEle));

target.setElementTypeName(defaultElementType);

target.setMergeEnabled(parseMergeAttribute(collectionEle));

//具体的List元素的解析过程。

parseCollectionElements(nl, target, bd, defaultElementType);

return target;

}

protected void parseCollectionElements(

NodeList elementNodes, Collection<Object> target, BeanDefinition bd, String defaultElementType) {

//遍历所有的元素节点,并判断其类型是否为Element。

for (int i = 0; i < elementNodes.getLength(); i++) {

Node node = elementNodes.item(i);

if (node instanceof Element && !DomUtils.nodeNameEquals(node, DESCRIPTION_ELEMENT)) {

//加入到target中去,target是一个ManagedList,同时触发对下一层子元素的解析过程,这是一个递归的调用。

target.add(parsePropertySubElement((Element) node, bd, defaultElementType));

}

}

}


经过这样一层一层的解析,我们在XML文件中定义的BeanDefinition就被整个给载入到了IoC容器中,并在容器中建立了数据映射。在 IoC容器中建立了对应的数据结构,或者说可以看成是POJO对象在IoC容器中的映像,这些数据结构可以以 AbstractBeanDefinition为入口,让IoC容器执行索引、查询和操作。

在我的感觉中,对核心数据结构的定义和处理应该可以看成是一个软件的核心部分了。所以,这里的BeanDefinition的载入可以说是IoC容器的核心,如果说IoC容器是Spring的核心,那么这些BeanDefinition就是Spring的核心的核心了!

呵呵,这部分代码数量不小,但如果掌握这条主线,其他都可以举一反三吧,就像我们掌握了操作系统启动的过程,以及在操作系统设计中的核心数据结构 像进程数据结构,文件系统数据结构,网络协议数据结构的设计和处理一样,对整个系统的设计原理,包括移植,驱动开发和应用开发,是非常有帮助的!

 

 

深入解析Spring架构与设计原理(二)AOP

关于AOP的个人理解

AOP联盟定义的AOP体系结构把与AOP相关的概念大致分为了由高到低、从使用到实现的三个层次。关于这个体系结构,个人的理解是这样的,从上 往下,最高层是语言和开发环境,在这个环境中可以看到几个重要的概念:base可以视为待增强对象,或者说目标对象;aspect指切面,通常包含对于 base的增强应用;configuration可以看成是一种编织或者说配置,通过在AOP体系中提供这个configuration配置环境,可以把 base和aspect结合起来,从而完成切面对目标对象的编织实现。

对Spring平台或者说生态系统来说,AOP是Spring框架的核心功能模块之一。AOP与IOC容器的结合使用, 为应用开发或者Spring自身功能的扩展都提供了许多便利。Spring AOP的实现和其他特性的实现一样,非常丰富,除了可以使用Spring本身提供的AOP实现之外,还封装了业界优秀的AOP解决方案AspectJ来让 应用使用。在这里,主要对Spring自身的AOP实现原理做一些解析;在这个AOP实现中,Spring充分利用了IOC容器Proxy代理对象以及 AOP拦截器的功能特性,通过这些对AOP基本功能的封装机制,为用户提供了AOP的实现框架。所以,要了解这些AOP的基本实现,需要我们对Java 的Proxy机制有一些基本了解。

AOP实现的基本线索

AOP实现中,可以看到三个主要的步骤,一个是代理对象的生成,然后是拦截器的作用,然后是Aspect编织的实现。AOP框架的丰富,很大程度 体现在这三个具体实现中,所具有的丰富的技术选择,以及如何实现与IOC容器的无缝结合。毕竟这也是一个非常核心的模块,需要满足不同的应用需求带来的解 决方案需求。
在Spring AOP的实现原理中,我们主要举ProxyFactoryBean的实现作为例子和实现的基本线索进行分析;很大一个原因,是因为 ProxyFactoryBean是在Spring IoC环境中,创建AOP应用的最底层方法,从中,可以看到一条实现AOP的基本线索。在ProxyFactoryBean中,它的AOP实现需要依赖 JDK或者CGLIB提供的Proxy特性。从FactoryBean中获取对象,是从getObject()方法作为入口完成的。然后为proxy代理 对象配置advisor链,这个配置是在initializeAdvisorChain方法中完成的;然后就为生成AOP代理对象做好了准备,生成代理对 象如下所示:

Java代码

1. private synchronized Object getSingletonInstance() {  

2.     if (this.singletonInstance == null) {  

3.         this.targetSource = freshTargetSource();  

4.         if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {  

5.             // Rely on AOP infrastructure to tell us what interfaces to proxy.  

6.             Class targetClass = getTargetClass();  

7.             if (targetClass == null) {  

8.                 throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");  

9.             }  

10.  // 这里设置代理对象的接口     setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));  

11.         }  

12.         // Initialize the shared singleton instance.  

13.         super.setFrozen(this.freezeProxy);  

14.         // 注意这里的方法会使用ProxyFactory来生成我们需要的Proxy  

15.         this.singletonInstance = getProxy(createAopProxy());  

16.     }  

17.     return this.singletonInstance;  

18. }  

19. //使用createAopProxy返回的AopProxy来得到代理对象  

20. protected Object getProxy(AopProxy aopProxy) {  

21.     return aopProxy.getProxy(this.proxyClassLoader);  

22. }  

private synchronized Object getSingletonInstance() {

if (this.singletonInstance == null) {

this.targetSource = freshTargetSource();

if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {

// Rely on AOP infrastructure to tell us what interfaces to proxy.

Class targetClass = getTargetClass();

if (targetClass == null) {

throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");

}

 // 这里设置代理对象的接口 setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));

}

// Initialize the shared singleton instance.

super.setFrozen(this.freezeProxy);

// 注意这里的方法会使用ProxyFactory来生成我们需要的Proxy

this.singletonInstance = getProxy(createAopProxy());

}

return this.singletonInstance;

}

//使用createAopProxy返回的AopProxy来得到代理对象

protected Object getProxy(AopProxy aopProxy) {

return aopProxy.getProxy(this.proxyClassLoader);

}

 



上面我们看到了在Spring中通过ProxyFactoryBean实现AOP功能的第一步,得到AopProxy代理对象的基本过程,下面我 们看看AopProxy代理对象的拦截机制是怎样发挥作用,是怎样实现AOP功能的。我们知道,对代理对象的生成,有CGLIB和JDK两种生成方式,在 CGLIB中,对拦截器设计是通过在Cglib2AopProxy的AopProxy代理对象生成的时候,在回调 DynamicAdvisedInterceptor对象中实现的,这个回调的实现在intercept方法中完成。对于AOP是怎样完成对目标对象的增 强的,这些实现是封装在AOP拦截器链中,由一个个具体的拦截器来完成的。具体拦截器的运行是在以下的代码实现中完成的,这些调用在 ReflectiveMethodInvocation中。

Java代码

1. public Object proceed() throws Throwable {  

2.     //  We start with an index of -1 and increment early.  

3.     //如果拦截器链中的拦截器迭代调用完毕,这里开始调用target的函数,这个函数是通过反射机制完成的,具体实现在:AopUtils.invokeJoinpointUsingReflection方法里面。  

4.     if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {  

5.         return invokeJoinpoint();  

6.     }  

7.     //这里沿着定义好的 interceptorOrInterceptionAdvice链进行处理。  

8.     Object interceptorOrInterceptionAdvice =  

9.         this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);  

10.     if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {  

11.         // Evaluate dynamic method matcher here: static part will already have  

12.         // been evaluated and found to match.  

13.         //这里对拦截器进行动态匹配的的判断,还记得我们前面分析的pointcut吗?这里是触发进行匹配的地方,如果和定义的pointcut匹配,那么这个advice将会得到执行。  

14.         InterceptorAndDynamicMethodMatcher dm =  

15.             (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;  

16.         if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {  

17.             return dm.interceptor.invoke(this);  

18.         }  

19.         else {  

20.             // Dynamic matching failed.  

21.             // Skip this interceptor and invoke the next in the chain.  

22.             // //如果不匹配,那么这个proceed会被递归调用,直到所有的拦截器都被运行过为止。  

23.             return proceed();  

24.         }  

25.     }  

26.     else {  

27.         // It's an interceptor, so we just invoke it: The pointcut will have  

28.         // been evaluated statically before this object was constructed.  

29.         //如果是一个interceptor,直接调用这个interceptor对应的方法  

30.         return((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);  

31.     }  

32. }  

public Object proceed() throws Throwable {

// We start with an index of -1 and increment early.

//如果拦截器链中的拦截器迭代调用完毕,这里开始调用target的函数,这个函数是通过反射机制完成的,具体实现在:AopUtils.invokeJoinpointUsingReflection方法里面。

if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {

return invokeJoinpoint();

}

//这里沿着定义好的 interceptorOrInterceptionAdvice链进行处理。

Object interceptorOrInterceptionAdvice =

    this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);

if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {

// Evaluate dynamic method matcher here: static part will already have

// been evaluated and found to match.

//这里对拦截器进行动态匹配的的判断,还记得我们前面分析的pointcut吗?这里是触发进行匹配的地方,如果和定义的pointcut匹配,那么这个advice将会得到执行。

InterceptorAndDynamicMethodMatcher dm =

    (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;

if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {

return dm.interceptor.invoke(this);

}

else {

// Dynamic matching failed.

// Skip this interceptor and invoke the next in the chain.

// //如果不匹配,那么这个proceed会被递归调用,直到所有的拦截器都被运行过为止。

return proceed();

}

}

else {

// It's an interceptor, so we just invoke it: The pointcut will have

// been evaluated statically before this object was constructed.

//如果是一个interceptor,直接调用这个interceptor对应的方法

return((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);

}

}


在调用拦截器的时候,我们接下去就可以看到对advice的通知的调用。而经过一系列的注册,适配的过程以后,拦截器在拦截的时候,会调用到预置 好的一个通知适配器,设置通知拦截器,这是一系列Spring设计好为通知服务的类的一个,是最终完成通知拦截和实现的地方,非常的关键。比如,对 MethodBeforeAdviceInterceptor的实现是这样的:

Java代码

1. public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {  

2.   

3.     private MethodBeforeAdvice advice;  

4.   

5.   

6.     /** 

7.      * Create a new MethodBeforeAdviceInterceptor for the given advice. 

8.      * @param advice the MethodBeforeAdvice to wrap 

9.      */  

10.     public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {  

11.         Assert.notNull(advice, "Advice must not be null");  

12.         this.advice = advice;  

13.     }  

14.     //这个invoke方法是拦截器的回调方法,会在代理对象的方法被调用的时候触发回调。  

15.     public Object invoke(MethodInvocation mi) throws Throwable {  

16.         this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );  

17.         return mi.proceed();  

18.     }  

19. }  

public class MethodBeforeAdviceInterceptor implements MethodInterceptor, Serializable {

 

private MethodBeforeAdvice advice;

 

 

/**

 * Create a new MethodBeforeAdviceInterceptor for the given advice.

 * @param advice the MethodBeforeAdvice to wrap

 */

public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {

Assert.notNull(advice, "Advice must not be null");

this.advice = advice;

}

//这个invoke方法是拦截器的回调方法,会在代理对象的方法被调用的时候触发回调。

public Object invoke(MethodInvocation mi) throws Throwable {

this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis() );

return mi.proceed();

}

}



在代码中,可以看到,就是这里,会调用advice的before方法!这样就成功的完成了before通知的编织!

因为Spring AOP本身并不打算成为一个一统天下的AOP框架,秉持Spring的一贯设计理念,设想中的Spring设计目标应该是,致力于AOP框架与IOC容器 的紧密集成,通过集成AOP技术为JavaEE应用开发中遇到的普遍问题提供解决方案,从而为AOP用户使用AOP技术提供最大的便利,从这个角度上为 Java EE的应用开发人员服务。在没有使用第三方AOP解决方案的时候,Spring通过虚拟机的Proxy特性和CGLIB实现了AOP的基本功能,我想,如 果有了Spring AOP实现原理的知识背景,再加上我们对源代码实现的认真解读,可以为我们了解其他AOP框架与IOC容器的集成原理,也打下了很好的基础,并真正了解一 个AOP框架是在怎样实现的。

这还真是就是我们喜欢开源软件一个原因,有了源代码,软件就没有什么神秘的面纱了!本立而道生,多读源代码吧,或者找一本从源代码出发讲解软件实现的书来看看,就像以前我们学习操作系统,学习TCP/IP那样!一定会有长进的。

 

 

深入解析Spring架构与设计原理(三)数据库的操作实现

最近事情实在是比较多,没有及时更新帖子,还望大家见谅啊。今天,一起讨论讨论Spring JDBC的实现吧。

关于Spring JDBC 
还是从Spring JDBC说起吧,虽然现在应用很多都是直接使用Hibernate或者其他的ORM工具。但JDBC毕竟还是很基本的,其中的JdbcTemplate就 是我们经常使用的,比如JDBCTemplate的execute方法,就是一个基本的方法,在这个方法的实现中,可以看到对数据库操作的基本过程。

Java代码

1. //execute方法执行的是输入的sql语句  

2. public void execute(final String sql) throws DataAccessException {  

3.     if (logger.isDebugEnabled()) {  

4.         logger.debug("Executing SQL statement [" + sql + "]");  

5.     }  

6.     class ExecuteStatementCallback implements StatementCallback<Object>, SqlProvider {  

7.         public Object doInStatement(Statement stmt) throws SQLException {  

8.             stmt.execute(sql);  

9.             return null;  

10.         }  

11.         public String getSql() {  

12.             return sql;  

13.         }  

14.     }  

15.     execute(new ExecuteStatementCallback());  

16. }  

17. //这是使用java.sql.Statement处理静态SQL语句的方法  

18. public <T> T execute(StatementCallback<T> action) throws DataAccessException {  

19.     Assert.notNull(action, "Callback object must not be null");  

20.     //这里取得数据库的Connection,这个数据库的Connection已经在Spring的事务管理之下  

21.     Connection con = DataSourceUtils.getConnection(getDataSource());  

22.     Statement stmt = null;  

23.     try {  

24.         Connection conToUse = con;  

25.         if (this.nativeJdbcExtractor != null &&  

26.                 this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {  

27.             conToUse = this.nativeJdbcExtractor.getNativeConnection(con);  

28.         }  

29.         //创建Statement  

30.         stmt = conToUse.createStatement();  

31.         applyStatementSettings(stmt);  

32.         Statement stmtToUse = stmt;  

33.         if (this.nativeJdbcExtractor != null) {  

34.             stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);  

35.         }  

36.         //这里调用回调函数  

37.         T result = action.doInStatement(stmtToUse);  

38.         handleWarnings(stmt);  

39.         return result;  

40.     }  

41.     catch (SQLException ex) {  

42.         // Release Connection early, to avoid potential connection pool deadlock  

43.         // in the case when the exception translator hasn't been initialized yet.  

44.         //如果捕捉到数据库异常,把数据库Connection释放,同时抛出一个经过Spring转换过的Spring数据库异常  

45.         //Spring做了一项有意义的工作,就是把这些数据库异常统一到自己的异常体系里了  

46.         JdbcUtils.closeStatement(stmt);  

47.         stmt = null;  

48.         DataSourceUtils.releaseConnection(con, getDataSource());  

49.         con = null;  

50.         throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);  

51.     }  

52.     finally {  

53.         JdbcUtils.closeStatement(stmt);  

54.         //释放数据库connection  

55.         DataSourceUtils.releaseConnection(con, getDataSource());  

56.     }  

57. }  

//execute方法执行的是输入的sql语句

public void execute(final String sql) throws DataAccessException {

if (logger.isDebugEnabled()) {

logger.debug("Executing SQL statement [" + sql + "]");

}

class ExecuteStatementCallback implements StatementCallback<Object>, SqlProvider {

public Object doInStatement(Statement stmt) throws SQLException {

stmt.execute(sql);

return null;

}

public String getSql() {

return sql;

}

}

execute(new ExecuteStatementCallback());

}

//这是使用java.sql.Statement处理静态SQL语句的方法

public <T> T execute(StatementCallback<T> action) throws DataAccessException {

Assert.notNull(action, "Callback object must not be null");

//这里取得数据库的Connection,这个数据库的Connection已经在Spring的事务管理之下

Connection con = DataSourceUtils.getConnection(getDataSource());

Statement stmt = null;

try {

Connection conToUse = con;

if (this.nativeJdbcExtractor != null &&

this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {

conToUse = this.nativeJdbcExtractor.getNativeConnection(con);

}

//创建Statement

stmt = conToUse.createStatement();

applyStatementSettings(stmt);

Statement stmtToUse = stmt;

if (this.nativeJdbcExtractor != null) {

stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);

}

//这里调用回调函数

T result = action.doInStatement(stmtToUse);

handleWarnings(stmt);

return result;

}

catch (SQLException ex) {

// Release Connection early, to avoid potential connection pool deadlock

// in the case when the exception translator hasn't been initialized yet.

//如果捕捉到数据库异常,把数据库Connection释放,同时抛出一个经过Spring转换过的Spring数据库异常

//Spring做了一项有意义的工作,就是把这些数据库异常统一到自己的异常体系里了

JdbcUtils.closeStatement(stmt);

stmt = null;

DataSourceUtils.releaseConnection(con, getDataSource());

con = null;

throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);

}

finally {

JdbcUtils.closeStatement(stmt);

//释放数据库connection

DataSourceUtils.releaseConnection(con, getDataSource());

}

}

 


在使用数据库的时候,有一个很重要的地方就是对数据库连接的管理,在这里,是由DataSourceUtils来完成的。Spring通过这个辅 助类来对数据的Connection进行管理。比如通过它来完成打开和关闭Connection等操作。DataSourceUtils对这些数据库 Connection管理的实现, 如以下代码所示。

Java代码

1. //这是取得数据库连接的调用,实现是通过调用doGetConnection完成的,这里执行了异常的转换操作  

2. public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {  

3.     try {  

4.         return doGetConnection(dataSource);  

5.     }  

6.     catch (SQLException ex) {  

7.         throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);  

8.     }  

9. }  

10. public static Connection doGetConnection(DataSource dataSource) throws SQLException {  

11.     Assert.notNull(dataSource, "No DataSource specified");  

12.     //把对数据库的Connection放到事务管理中进行管理,这里使用TransactionSynchronizationManager中定义的ThreadLocal变量来和线程绑定数据库连接  

13.     //如果在TransactionSynchronizationManager中已经有与当前线程绑定数据库连接,那就直接取出来使用  

14.     ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);  

15.     if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {  

16.         conHolder.requested();  

17.         if (!conHolder.hasConnection()) {  

18.             logger.debug("Fetching resumed JDBC Connection from DataSource");  

19.             conHolder.setConnection(dataSource.getConnection());  

20.         }  

21.         return conHolder.getConnection();  

22.     }  

23.     // Else we either got no holder or an empty thread-bound holder here.  

24.     // 这里得到需要的数据库Connection,在Bean配置文件中定义好的,  

25.     // 同时最后把新打开的数据库Connection通过TransactionSynchronizationManager和当前线程绑定起来。  

26.     logger.debug("Fetching JDBC Connection from DataSource");  

27.     Connection con = dataSource.getConnection();  

28.   

29.     if (TransactionSynchronizationManager.isSynchronizationActive()) {  

30.         logger.debug("Registering transaction synchronization for JDBC Connection");  

31.         // Use same Connection for further JDBC actions within the transaction.  

32.         // Thread-bound object will get removed by synchronization at transaction completion.  

33.         ConnectionHolder holderToUse = conHolder;  

34.         if (holderToUse == null) {  

35.             holderToUse = new ConnectionHolder(con);  

36.         }  

37.         else {  

38.             holderToUse.setConnection(con);  

39.         }  

40.         holderToUse.requested();  

41.         TransactionSynchronizationManager.registerSynchronization(  

42.                 new ConnectionSynchronization(holderToUse, dataSource));  

43.         holderToUse.setSynchronizedWithTransaction(true);  

44.         if (holderToUse != conHolder) {  

45.             TransactionSynchronizationManager.bindResource(dataSource, holderToUse);  

46.         }  

47.     }  

48.     return con;  

49. }  

//这是取得数据库连接的调用,实现是通过调用doGetConnection完成的,这里执行了异常的转换操作

public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {

try {

return doGetConnection(dataSource);

}

catch (SQLException ex) {

throw new CannotGetJdbcConnectionException("Could not get JDBC Connection", ex);

}

}

public static Connection doGetConnection(DataSource dataSource) throws SQLException {

Assert.notNull(dataSource, "No DataSource specified");

//把对数据库的Connection放到事务管理中进行管理,这里使用TransactionSynchronizationManager中定义的ThreadLocal变量来和线程绑定数据库连接

//如果在TransactionSynchronizationManager中已经有与当前线程绑定数据库连接,那就直接取出来使用

ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource);

if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) {

conHolder.requested();

if (!conHolder.hasConnection()) {

logger.debug("Fetching resumed JDBC Connection from DataSource");

conHolder.setConnection(dataSource.getConnection());

}

return conHolder.getConnection();

}

// Else we either got no holder or an empty thread-bound holder here.

// 这里得到需要的数据库Connection,在Bean配置文件中定义好的,

// 同时最后把新打开的数据库Connection通过TransactionSynchronizationManager和当前线程绑定起来。

logger.debug("Fetching JDBC Connection from DataSource");

Connection con = dataSource.getConnection();

 

if (TransactionSynchronizationManager.isSynchronizationActive()) {

logger.debug("Registering transaction synchronization for JDBC Connection");

// Use same Connection for further JDBC actions within the transaction.

// Thread-bound object will get removed by synchronization at transaction completion.

ConnectionHolder holderToUse = conHolder;

if (holderToUse == null) {

holderToUse = new ConnectionHolder(con);

}

else {

holderToUse.setConnection(con);

}

holderToUse.requested();

TransactionSynchronizationManager.registerSynchronization(

new ConnectionSynchronization(holderToUse, dataSource));

holderToUse.setSynchronizedWithTransaction(true);

if (holderToUse != conHolder) {

TransactionSynchronizationManager.bindResource(dataSource, holderToUse);

}

}

return con;

}

 



关于数据库操作类RDBMS 
从JdbcTemplate中,我们看到,他提供了许多简单查询和更新的功能。但是,如果需要更高层次的抽象,以及更面向对象的方法来访问数据 库,Spring为我们提供了org.springframework.jdbc.object包,里面包含了SqlQuery、 SqlMappingQuery、SqlUpdate和StoredProcedure等类,这些类都是Spring JDBC应用程序可以使用的。但要注意,在使用这些类时需要为它们配置好JdbcTemplate作为其基本的操作实现,因为在它们的功能实现中,对数据 库操作的那部分实现基本上还是依赖于JdbcTemplate来完成的。

比如,对MappingSqlQuery使用的过程,是非常简洁的;在设计好数据的映射代码之后,查询得到的记录已经按照前面的设计转换为对象 List了,一条查询记录对应于一个数据对象,可以把数据库的数据记录直接映射成Java对象在程序中使用,同时又可避免使用第三方ORM工具的配置,对 于简单的数据映射场合是非常方便的;在mapRow方法的实现中提供的数据转换规则,和我们使用Hibernate时,Hibernate的hbm文件起 到的作用是非常类似的。这个MappingSqlQuery需要的对设置进行compile,这些compile是这样完成的,如以下代码所示:

Java代码

1. protected final void compileInternal() {  

2.     //这里是对参数的compile过程,所有的参数都在getDeclaredParameters里面,生成了一个PreparedStatementCreatorFactory  

3.     this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());  

4.     this.preparedStatementFactory.setResultSetType(getResultSetType());  

5.     this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());  

6.     this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());  

7.     if (getGeneratedKeysColumnNames() != null) {  

8.         this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());  

9.     }  

10. his.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());  

11.     onCompileInternal();  

12. }  

protected final void compileInternal() {

//这里是对参数的compile过程,所有的参数都在getDeclaredParameters里面,生成了一个PreparedStatementCreatorFactory

this.preparedStatementFactory = new PreparedStatementCreatorFactory(getSql(), getDeclaredParameters());

this.preparedStatementFactory.setResultSetType(getResultSetType());

this.preparedStatementFactory.setUpdatableResults(isUpdatableResults());

this.preparedStatementFactory.setReturnGeneratedKeys(isReturnGeneratedKeys());

if (getGeneratedKeysColumnNames() != null) {

this.preparedStatementFactory.setGeneratedKeysColumnNames(getGeneratedKeysColumnNames());

}

this.preparedStatementFactory.setNativeJdbcExtractor(getJdbcTemplate().getNativeJdbcExtractor());

onCompileInternal();

}

在执行查询时,执行的实际上是SqlQuery的executeByNamedParam方法,这个方法需要完成的工作包括配置SQL语句, 配置数据记录到数据对象的转换的RowMapper,然后使用JdbcTemplate来完成数据的查询,并启动数据记录到Java数据对象的转换,如以 下代码所示:

Java代码

1. public List<T> executeByNamedParam(Map<String, ?> paramMap, Map context) throws DataAccessException {  

2.     validateNamedParameters(paramMap);  

3.     //得到需要执行的SQL语句  

4.     ParsedSql parsedSql = getParsedSql();  

5.     MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);  

6.     String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);  

7.     //配置好SQL语句需要的Parameters及rowMapper,这个rowMapper完成数据记录到对象的转换  

8.     Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());  

9.     RowMapper<T> rowMapper = newRowMapper(params, context);  

10.     //我们又看到了JdbcTemplate,这里使用JdbcTemplate来完成对数据库的查询操作,所以我们说JdbcTemplate是非常基本的操作类  

11.         return getJdbcTemplate().query(newPreparedStatementCreator(sqlToUse, params), rowMapper);  

12. }  

public List<T> executeByNamedParam(Map<String, ?> paramMap, Map context) throws DataAccessException {

validateNamedParameters(paramMap);

//得到需要执行的SQL语句

ParsedSql parsedSql = getParsedSql();

MapSqlParameterSource paramSource = new MapSqlParameterSource(paramMap);

String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);

//配置好SQL语句需要的Parameters及rowMapper,这个rowMapper完成数据记录到对象的转换

Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, getDeclaredParameters());

RowMapper<T> rowMapper = newRowMapper(params, context);

//我们又看到了JdbcTemplate,这里使用JdbcTemplate来完成对数据库的查询操作,所以我们说JdbcTemplate是非常基本的操作类

  return getJdbcTemplate().query(newPreparedStatementCreator(sqlToUse, params), rowMapper);

}



在Spring对JDBC的操作中,基本上是对JDBC/Hibernate基础上API的封装。这些封装可以直接使用,也可以在IoC容器中配 置好了再使用,当结合IoC容器的基础上进行使用的时候,可以看到许多和事务管理相关的处理部分,都是非常值得学习的,在那里,可以看到对数据源的管理 - Hibernate中session的管理,与线程的结合等等。

 

 

深入解析Spring架构与设计原理(四)Web MVC的实现

以前的欠账,现在补上,欢迎指正和讨论。

Spring Web MVC的实现 
关于MVC,这是和WEB开发相关的部分,显然大家都是很熟悉了。从最初的JSP到struts,再到像wicket等等,真是百花齐放,百家争 鸣.在WEB UI上,这部分是做web应用架构选择不可缺少的一部分。而作为MVC框架,也许SPRING MVC不能算得上是表现力最出色的UI框架,但无疑,它的实现也是非常的优秀,同时,我们可以从它的实现上,看到一个非常清晰的MVC实现的过程,从这点 上看,真是非常的过瘾啊!

在了解IOC容器的基本实现的基础上,下面我们来看看,在典型的Web环境中,Spring IOC容器是如何在Web环境中被载入并起作用的。我们可以看到,对于MVC这部分,主要建立在IOC的基础上,AOP的特性应用得并不多。Spring 并不是天生就能在Web容器中起作用的,同样也需要一个启动过程,把自己的IOC容器导入,并在Web容器中建立起来。

与对IoC容器的初始化的分析一样,我们同样看到了loadBeanDefinition对BeanDefinition的载入。在Web环境 中,对定位BeanDefinition的Resource有特别的要求,对这个要求的处理体现在getDefaultConfigLocations方 法的处理中。可以看到,在这里,使用了默认的BeanDefinition的配置路径,这个路径在XmlWebApplicationContext中, 已经作为一个常量定义好了,这个常量就是/WEB-INF/applicationContext.xml。这里的loadBeanDefinition 实现如下所示:

Java代码

1. public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {  

2.   

3.     /** Default config location for the root context */  

4.     //这里是设置缺省BeanDefinition的地方,在/WEB-INF/applicationContext.xml文件里,如果不特殊指定其他文件,IoC容器会从这里读取BeanDefinition来初始化IoC容器  

5.     public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";  

6.   

7.     /** Default prefix for building a config location for a namespace */  

8.     public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";  

9.   

10.     /** Default suffix for building a config location for a namespace */  

11.     public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";  

12.     //我们又看到了熟悉的loadBeanDefinition,就像我们前面对IOC容器的分析一样,这个加载过程在容器refresh()时启动。  

13.     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {  

14.         // Create a new XmlBeanDefinitionReader for the given BeanFactory.  

15.         // 对于XmlWebApplicationContext,当然是使用XmlBeanDefinitionReader来对BeanDefinition信息进行解析  

16.         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  

17.   

18.         // Configure the bean definition reader with this context's  

19.         // resource loading environment.  

20.         // 这里设置ResourceLoader,因为XmlWebApplicationContext是DefaultResource的子类,所以这里同样会使用DefaultResourceLoader来定位BeanDefinition       

21.         beanDefinitionReader.setResourceLoader(this);  

22.         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  

23.   

24.         // Allow a subclass to provide custom initialization of the reader,  

25.         // then proceed with actually loading the bean definitions.  

26.         initBeanDefinitionReader(beanDefinitionReader);  

27.         //这里使用定义好的XmlBeanDefinitionReader来载入BeanDefinition  

28.         loadBeanDefinitions(beanDefinitionReader);  

29.     }  

30.   

31.   

32.     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {  

33.     }  

34.   

35.   

36.     //如果有多个BeanDefinition的文件定义,需要逐个载入,都是通过reader来完成的,这个初始化过程是由refreshBeanFactory方法来完成的,这里只是负责载入BeanDefinition  

37.     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {  

38.         String[] configLocations = getConfigLocations();  

39.         if (configLocations != null) {  

40.             for (String configLocation : configLocations) {  

41.                 reader.loadBeanDefinitions(configLocation);  

42.             }  

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {

 

/** Default config location for the root context */

//这里是设置缺省BeanDefinition的地方,在/WEB-INF/applicationContext.xml文件里,如果不特殊指定其他文件,IoC容器会从这里读取BeanDefinition来初始化IoC容器

public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

 

/** Default prefix for building a config location for a namespace */

public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";

 

/** Default suffix for building a config location for a namespace */

public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";

//我们又看到了熟悉的loadBeanDefinition,就像我们前面对IOC容器的分析一样,这个加载过程在容器refresh()时启动。

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException {

// Create a new XmlBeanDefinitionReader for the given BeanFactory.

// 对于XmlWebApplicationContext,当然是使用XmlBeanDefinitionReader来对BeanDefinition信息进行解析

XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

 

// Configure the bean definition reader with this context's

// resource loading environment.

// 这里设置ResourceLoader,因为XmlWebApplicationContext是DefaultResource的子类,所以这里同样会使用DefaultResourceLoader来定位BeanDefinition

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);

//这里使用定义好的XmlBeanDefinitionReader来载入BeanDefinition

loadBeanDefinitions(beanDefinitionReader);

}

 

 

protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {

}

 

 

//如果有多个BeanDefinition的文件定义,需要逐个载入,都是通过reader来完成的,这个初始化过程是由refreshBeanFactory方法来完成的,这里只是负责载入BeanDefinition

protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {

String[] configLocations = getConfigLocations();

if (configLocations != null) {

for (String configLocation : configLocations) {

reader.loadBeanDefinitions(configLocation);

}

 



进入DispatcherServlet和MVC实现 
完成了在Web环境中,IoC容器的建立以后,也就是在完成对ContextLoaderListener的初始化以后,Web容器开始初始化 DispatcherServlet,接着,会执行DispatcherServlet持有的IoC容器的初始化过程,在这个初始化过程中,一个新的上下 文被建立起来,这个DispatcherServlet持有的上下文,被设置为根上下文的子上下文。可以大致认为,根上下文是和Web应用相对应的一个上 下文,而DispatcherServlet持有的上下文是和Servlet对应的一个上下文,在一个Web应用中,往往可以容纳多个Servlet存 在;与此相对应,对于应用在Web容器中的上下体系,也是很类似的,一个根上下文可以作为许多Servlet上下文的双亲上下文。在 DispatcherServlet,我们可以看到对MVC的初始化,是在DispatcherServlet的initStrategies完成的。
在这个初始化完成以后,会在上下文中建立器一个执行器于url的对应关系,这个对应关系可以让在url请求到来的时候,MVC可以检索到相应的控制器来进行处理,如以下代码所示:

Java代码

1. protected Object getHandlerInternal(HttpServletRequest request) throws Exception {  

2.     //这里从request中得到请求的url路径  

3.     String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);  

4.     //这里使用得到的url路径对Handler进行匹配,得到对应的Handler,如果没有对应的Hanlder,返回null,这样默认的Handler会被使用  

5.     Object handler = lookupHandler(lookupPath, request);  

6.     if (handler == null) {  

7.         // We need to care for the default handler directly, since we need to  

8.         // expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well.  

9.         Object rawHandler = null;  

10.         if ("/".equals(lookupPath)) {  

11.             rawHandler = getRootHandler();  

12.         }  

13.         if (rawHandler == null) {  

14.             rawHandler = getDefaultHandler();  

15.         }  

16.         if (rawHandler != null) {  

17.             validateHandler(rawHandler, request);  

18.             handler = buildPathExposingHandler(rawHandler, lookupPath, null);  

19.         }  

20.     }  

21.     if (handler != null && logger.isDebugEnabled()) {  

22.         logger.debug("Mapping [" + lookupPath + "] to handler '" + handler + "'");  

23.     }  

24.     else if (handler == null && logger.isTraceEnabled()) {  

25.         logger.trace("No handler mapping found for [" + lookupPath + "]");  

26.     }  

27.     return handler;  

28. }  

29.   // lookupHandler是根据url路径,启动在handlerMap中对handler的检索,并最终返回handler对象  

30. protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {  

31.     // Direct match?  

32.     Object handler = this.handlerMap.get(urlPath);  

33.     if (handler != null) {  

34.         validateHandler(handler, request);  

35.         return buildPathExposingHandler(handler, urlPath, null);  

36.     }  

37.     // Pattern match?  

38.     String bestPathMatch = null;  

39.     for (String registeredPath : this.handlerMap.keySet()) {  

40.         if (getPathMatcher().match(registeredPath, urlPath) &&  

41.                 (bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) {  

42.             bestPathMatch = registeredPath;  

43.         }  

44.     }  

45.     if (bestPathMatch != null) {  

46.         handler = this.handlerMap.get(bestPathMatch);  

47.         validateHandler(handler, request);  

48.         String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath);  

49.         Map<String, String> uriTemplateVariables =  

50.                 getPathMatcher().extractUriTemplateVariables(bestPathMatch, urlPath);  

51.         return buildPathExposingHandler(handler, pathWithinMapping, uriTemplateVariables);  

52.     }  

53.     // No handler found...  

54.     return null;  

55. }  

protected Object getHandlerInternal(HttpServletRequest request) throws Exception {

//这里从request中得到请求的url路径

String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);

//这里使用得到的url路径对Handler进行匹配,得到对应的Handler,如果没有对应的Hanlder,返回null,这样默认的Handler会被使用

Object handler = lookupHandler(lookupPath, request);

if (handler == null) {

// We need to care for the default handler directly, since we need to

// expose the PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE for it as well.

Object rawHandler = null;

if ("/".equals(lookupPath)) {

rawHandler = getRootHandler();

}

if (rawHandler == null) {

rawHandler = getDefaultHandler();

}

if (rawHandler != null) {

validateHandler(rawHandler, request);

handler = buildPathExposingHandler(rawHandler, lookupPath, null);

}

}

if (handler != null && logger.isDebugEnabled()) {

logger.debug("Mapping [" + lookupPath + "] to handler '" + handler + "'");

}

else if (handler == null && logger.isTraceEnabled()) {

logger.trace("No handler mapping found for [" + lookupPath + "]");

}

return handler;

}

   // lookupHandler是根据url路径,启动在handlerMap中对handler的检索,并最终返回handler对象

protected Object lookupHandler(String urlPath, HttpServletRequest request) throws Exception {

// Direct match?

Object handler = this.handlerMap.get(urlPath);

if (handler != null) {

validateHandler(handler, request);

return buildPathExposingHandler(handler, urlPath, null);

}

// Pattern match?

String bestPathMatch = null;

for (String registeredPath : this.handlerMap.keySet()) {

if (getPathMatcher().match(registeredPath, urlPath) &&

(bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) {

bestPathMatch = registeredPath;

}

}

if (bestPathMatch != null) {

handler = this.handlerMap.get(bestPathMatch);

validateHandler(handler, request);

String pathWithinMapping = getPathMatcher().extractPathWithinPattern(bestPathMatch, urlPath);

Map<String, String> uriTemplateVariables =

getPathMatcher().extractUriTemplateVariables(bestPathMatch, urlPath);

return buildPathExposingHandler(handler, pathWithinMapping, uriTemplateVariables);

}

// No handler found...

return null;

}


最后,我们可以结合在DispatcherServlet中,对请求的分发处理来了解一个url请求到来时,MVC的实现和协同处理过程,如以下代码所示:

Java代码

1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {  

2.     HttpServletRequest processedRequest = request;  

3.     HandlerExecutionChain mappedHandler = null;  

4.     int interceptorIndex = -1;  

5.     //这里为视图准备好一个ModelAndView,这个ModelAndView持有handler处理请求的结果  

6.     try {  

7.         ModelAndView mv = null;  

8.         boolean errorView = false;  

9.         try {  

10.             processedRequest = checkMultipart(request);  

11.             // Determine handler for the current request.  

12.             // 根据请求得到对应的handler,hander的注册以及getHandler的实现在前面已经分析过  

13.             mappedHandler = getHandler(processedRequest, false);  

14.             if (mappedHandler == null || mappedHandler.getHandler() == null) {  

15.                 noHandlerFound(processedRequest, response);  

16.                 return;  

17.             }  

18.             // Apply preHandle methods of registered interceptors.  

19.             // 调用hander的拦截器,从HandlerExecutionChain中取出Interceptor进行前处理  

20.             HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();  

21.             if (interceptors != null) {  

22.                 for (int i = 0; i < interceptors.length; i++) {  

23.                     HandlerInterceptor interceptor = interceptors[i];  

24.                     if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {  

25.                         triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);  

26.                         return;  

27.                     }  

28.                     interceptorIndex = i;  

29.                 }  

30.             }  

31.             // Actually invoke the handler.  

32.             // 这里是实际调用handler的地方,在执行handler之前,用HandlerAdapter先检查一下handler的合法性:是不是按Spring的要求编写的handler  

33.             // handler处理的结果封装到ModelAndView对象,为视图提供展现数据  

34.             HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());  

35.             //这里通过调用HandleAdapter的handle方法,实际上触发对Controller的handleRequest方法的调用  

36.             mv = ha.handle(processedRequest, response, mappedHandler.getHandler());  

37.             // Do we need view name translation?  

38.             if (mv != null && !mv.hasView()) {  

39.                 mv.setViewName(getDefaultViewName(request));  

40.             }  

41.             // Apply postHandle methods of registered interceptors.  

42.             if (interceptors != null) {  

43.                 for (int i = interceptors.length - 1; i >= 0; i--) {  

44.                     HandlerInterceptor interceptor = interceptors[i];  

45.                     interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);  

46.                 }  

47.             }  

48.         }  

49.         catch (ModelAndViewDefiningException ex) {  

50.             logger.debug("ModelAndViewDefiningException encountered", ex);  

51.             mv = ex.getModelAndView();  

52.         }  

53.         catch (Exception ex) {  

54.             Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);  

55.             mv = processHandlerException(processedRequest, response, handler, ex);  

56.             errorView = (mv != null);  

57.         }  

58.         // Did the handler return a view to render?  

59.         // 这里使用视图对ModelAndView数据的展现  

60.         if (mv != null && !mv.wasCleared()) {  

61.             render(mv, processedRequest, response);  

62.             if (errorView) {  

63.                 WebUtils.clearErrorRequestAttributes(request);  

64.             }  

65.         }  

66.         else {  

67.             if (logger.isDebugEnabled()) {  

68.                 logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +  

69.                         "': assuming HandlerAdapter completed request handling");  

70.             }  

71.         }  

72.         // Trigger after-completion for successful outcome.  

73.         triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);  

74.     }  

75.     catch (Exception ex) {  

76.         // Trigger after-completion for thrown exception.  

77.         triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);  

78.         throw ex;  

79.     }  

80.     catch (Error err) {  

81.         ServletException ex = new NestedServletException("Handler processing failed", err);  

82.         // Trigger after-completion for thrown exception.  

83.         triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);  

84.         throw ex;  

85.     }  

86.     finally {  

87.         // Clean up any resources used by a multipart request.  

88.         if (processedRequest != request) {  

89.             cleanupMultipart(processedRequest);  

90.         }  

91.     }  

92. }  

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

HttpServletRequest processedRequest = request;

HandlerExecutionChain mappedHandler = null;

int interceptorIndex = -1;

//这里为视图准备好一个ModelAndView,这个ModelAndView持有handler处理请求的结果

try {

ModelAndView mv = null;

boolean errorView = false;

try {

processedRequest = checkMultipart(request);

// Determine handler for the current request.

// 根据请求得到对应的handler,hander的注册以及getHandler的实现在前面已经分析过

mappedHandler = getHandler(processedRequest, false);

if (mappedHandler == null || mappedHandler.getHandler() == null) {

noHandlerFound(processedRequest, response);

return;

}

// Apply preHandle methods of registered interceptors.

// 调用hander的拦截器,从HandlerExecutionChain中取出Interceptor进行前处理

HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();

if (interceptors != null) {

for (int i = 0; i < interceptors.length; i++) {

HandlerInterceptor interceptor = interceptors[i];

if (!interceptor.preHandle(processedRequest, response, mappedHandler.getHandler())) {

triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);

return;

}

interceptorIndex = i;

}

}

// Actually invoke the handler.

// 这里是实际调用handler的地方,在执行handler之前,用HandlerAdapter先检查一下handler的合法性:是不是按Spring的要求编写的handler

// handler处理的结果封装到ModelAndView对象,为视图提供展现数据

HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

//这里通过调用HandleAdapter的handle方法,实际上触发对Controller的handleRequest方法的调用

mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

// Do we need view name translation?

if (mv != null && !mv.hasView()) {

mv.setViewName(getDefaultViewName(request));

}

// Apply postHandle methods of registered interceptors.

if (interceptors != null) {

for (int i = interceptors.length - 1; i >= 0; i--) {

HandlerInterceptor interceptor = interceptors[i];

interceptor.postHandle(processedRequest, response, mappedHandler.getHandler(), mv);

}

}

}

catch (ModelAndViewDefiningException ex) {

logger.debug("ModelAndViewDefiningException encountered", ex);

mv = ex.getModelAndView();

}

catch (Exception ex) {

Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);

mv = processHandlerException(processedRequest, response, handler, ex);

errorView = (mv != null);

}

// Did the handler return a view to render?

// 这里使用视图对ModelAndView数据的展现

if (mv != null && !mv.wasCleared()) {

render(mv, processedRequest, response);

if (errorView) {

WebUtils.clearErrorRequestAttributes(request);

}

}

else {

if (logger.isDebugEnabled()) {

logger.debug("Null ModelAndView returned to DispatcherServlet with name '" + getServletName() +

"': assuming HandlerAdapter completed request handling");

}

}

// Trigger after-completion for successful outcome.

triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, null);

}

catch (Exception ex) {

// Trigger after-completion for thrown exception.

triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);

throw ex;

}

catch (Error err) {

ServletException ex = new NestedServletException("Handler processing failed", err);

// Trigger after-completion for thrown exception.

triggerAfterCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex);

throw ex;

}

finally {

// Clean up any resources used by a multipart request.

if (processedRequest != request) {

cleanupMultipart(processedRequest);

}

}

}



通过MVC框架,实际上是DispatcherServlet的协调运作,得到了ModelAndView对象作为数据处理结果,最 后,DispatcherServlet把获得的模型数据交给特定的视图对象,从而完成这些数据的视图呈现工作,这个视图呈现由视图对象的render方 法来完成,毫无疑问,对应于不同的视图对象,render方法会完成不同的视图呈现处理,从而为用户提供丰富的Web UI表现。关于这些不同的视图展现,还可以看到很多很有参考意义的开源软件的灵活使用,限于篇幅,这里就不详细说了。

对Spring MVC框架的个人理解

对Spring作为应用平台的Web应用开发而言,Spring为它们提供了Spring MVC框架,作为一个像struts这样的Web框架的替代;当然,作为应用平台,Spring并不会强制应用对Web框架的选择。但对Web应用开发而 言,选择直接使用Spring MVC,可以给应用开发带来许多便利。因为Spring MVC, 毫无疑问,很好的提供了与Web环境中的IoC容器的集成。同时,和其他Web应用一样,使用Spring MVC, 应用只需要专注于处理逻辑和视图呈现的开发(当然这些开发需要符合Spring MVC的开发习惯),在视图呈现部分,Spring MVC同时也集成了许多现有的Web UI实现,比如像Excel, PDF这些文档视图的生成,因为,集成第三方解决方案,实在可以说是Spring的拿手好戏,从这种一致性的开发模式上看,它在很大程度上降低了Web应 用开发的门槛。

 

 

深入解析Spring架构与设计原理(五)Spring与远端调用

在应用开发中,常常涉及服务器系统中各种不同进程之间的通信与计算交互,远端调用(RMI)是实现这种计算场景的一种有效方式。此外,还存在着另一种情 况,在这种应用场景中,与那些典型的基于HTML的B/S应用不同,客户端程序需要完成对服务器端应用的直接调用,这也是需要远端调用大显身手的场合。

Spring中提供了轻量级的远端调用模块,从而为我们在上面提到的应用场景开发,提供平台支持。根据Spring的既定策略,它依然只是起到一 个集成平台的作用,而并不期望在实现方案上,与已有的远端调用方案形成竞争。也就是说,在Spring远端调用架构中,具体的通信协议设计、通信实现,以 及在服务器和客户端对远端调用的处理封装,Spring没有将其作为实现重点,在这个技术点上,并不需要重新发明轮子。对Spring来说,它所要完成的 工作,是在已有远端调用技术实现的基础上,通过IoC与AOP的封装,让应用更方便地使用这些远端调用服务,并能够更方便灵活地与现有应用系统实现集成。 通过Spring封装以后,应用使用远端过程调用非常方便,既不需要改变原来系统的相关实现接口,也不需要为远端调用功能增加新的封装负担。因此,这种使 用方式,在某种程度上,可以称为轻量级的远端调用方案。

在实现远端调用的过程中,往往需要涉及客户端和服务器端的相关设置,这些设置通过Spring的IoC容器就可以很好的完成,这是我们已经很熟悉 的IoC容器的强项了。同时,Spring为远端调用的实现,提供了许多不同的方案,玲琅满目,任君选择。如RMI、HTTP调用器、第三方远端调用库 Hessian/Burlap、基于Java RMI的解决方案,等等。

Spring对不同的远端调用的实现封装,基本上,都采用了类似的模式来完成,比如在客户端,都是通过相关的ProxyFactoryBean和 ClientInterceptor来完成的,在服务器端是通过ServiceExporter来导出远端的服务对象的。有了这些统一的命名规则,应用配 置和使用远端调用会非常方便,同时,通过对这些Spring远端调用基础设施实现原理的分析,还可以看到一些常用处理方法的技术实现,比如对代理对象的使 用、拦截器的使用、通过afterPropertiesSet来启动远端调用基础设施的建立,等等,这些都是在Spring中常用的技术。

HTTP调用器客户端的实现 
在HtttpInvokerProxyFactory中,设置了serviceProxy对象作为远端服务的本地代理对象;同时,在依赖注入完成以后,通过afterPropertiesSet来对远端调用完成设置。

Java代码

1. public class HttpInvokerProxyFactoryBean extends HttpInvokerClientInterceptor  

2.         implements FactoryBean<Object> {  

3.     //这是远端对象的代理  

4.     private Object serviceProxy;  

5.   

6.     @Override  

7.     //在注入完成之后,设置远端对象代理  

8.     public void afterPropertiesSet() {  

9.         super.afterPropertiesSet();  

10.         //需要配置远端调用的接口  

11.         if (getServiceInterface() == null) {  

12.             throw new IllegalArgumentException("Property 'serviceInterface' is required");  

13.         }// 这里使用ProxyFactory来生成远端代理对象,注意这个this,因为HttpInvokerProxyFactoryBean的基类是 HttpInvokerClientInterceptor,所以代理类的拦截器被设置为HttpInvokerClientInterceptor  

14.         this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader());  

15.     }  

16.   

17.     //FactoryBean生产对象的入口。返回的是serviceProxy对象,这是一个代理对象  

18.     public Object getObject() {  

19.         return this.serviceProxy;  

20.     }  

21.   

22.     public Class<?> getObjectType() {  

23.         return getServiceInterface();  

24.     }  

25.   

26.     public boolean isSingleton() {  

27.         return true;  

28.     }  

public class HttpInvokerProxyFactoryBean extends HttpInvokerClientInterceptor

implements FactoryBean<Object> {

//这是远端对象的代理

private Object serviceProxy;

 

@Override

//在注入完成之后,设置远端对象代理

public void afterPropertiesSet() {

super.afterPropertiesSet();

//需要配置远端调用的接口

if (getServiceInterface() == null) {

throw new IllegalArgumentException("Property 'serviceInterface' is required");

}//这里使用ProxyFactory来生成远端代理对象,注意这个this,因为HttpInvokerProxyFactoryBean的基类是HttpInvokerClientInterceptor,所以代理类的拦截器被设置为HttpInvokerClientInterceptor

this.serviceProxy = new ProxyFactory(getServiceInterface(), this).getProxy(getBeanClassLoader());

}

 

//FactoryBean生产对象的入口。返回的是serviceProxy对象,这是一个代理对象

public Object getObject() {

return this.serviceProxy;

}

 

public Class<?> getObjectType() {

return getServiceInterface();

}

 

public boolean isSingleton() {

return true;

}


可以看到,为这个代理对象配置了一个拦截器HttpInvokerClientInterceptor,在这个拦截器中,拦截了对代理对象的方法调用。如以下代码所示:

Java代码

1. //对代理对象的方法调用入口  

2. public Object invoke(MethodInvocation methodInvocation) throws Throwable {  

3.     if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {  

4.         return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";  

5.     }  

6.     //创建RemoteInvocation对象,这个对象封装了对远端的调用,这些远端调用通过序列化的机制完成  

7.     RemoteInvocation invocation = createRemoteInvocation(methodInvocation);  

8.     RemoteInvocationResult result = null;  

9.     try {  

10.         //这里是对远端调用的入口  

11.         result = executeRequest(invocation, methodInvocation);  

12.     }  

13.     catch (Throwable ex) {  

14.         throw convertHttpInvokerAccessException(ex);  

15.     }  

16.     try {//返回远端调用的结果  

17.         return recreateRemoteInvocationResult(result);  

18.     }  

19.     catch (Throwable ex) {  

20.         if (result.hasInvocationTargetException()) {  

21.             throw ex;  

22.         }  

23.         else {  

24.             throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +  

25.                     "] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex);  

26.         }  

27.     }  

28. }  

//对代理对象的方法调用入口

public Object invoke(MethodInvocation methodInvocation) throws Throwable {

if (AopUtils.isToStringMethod(methodInvocation.getMethod())) {

return "HTTP invoker proxy for service URL [" + getServiceUrl() + "]";

}

//创建RemoteInvocation对象,这个对象封装了对远端的调用,这些远端调用通过序列化的机制完成

RemoteInvocation invocation = createRemoteInvocation(methodInvocation);

RemoteInvocationResult result = null;

try {

//这里是对远端调用的入口

result = executeRequest(invocation, methodInvocation);

}

catch (Throwable ex) {

throw convertHttpInvokerAccessException(ex);

}

try {//返回远端调用的结果

return recreateRemoteInvocationResult(result);

}

catch (Throwable ex) {

if (result.hasInvocationTargetException()) {

throw ex;

}

else {

throw new RemoteInvocationFailureException("Invocation of method [" + methodInvocation.getMethod() +

"] failed in HTTP invoker remote service at [" + getServiceUrl() + "]", ex);

}

}

}


远端调用的具体实现过程,是由executeRequest来完成的,也就是在SimpleHttpInvokerRequestExecutor的实现中,封装了整个HTTP调用器客户端实现的基本过程,如下所示:

Java代码

1. //这是HTTP调用器实现的基本过程,通过HTTP的request和reponse来完成通信,在通信的过程中传输的数据是序列化的对象  

2. protected RemoteInvocationResult doExecuteRequest(  

3.         HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)  

4.         throws IOException, ClassNotFoundException {  

5.     //打开一个标准J2SE HttpURLConnection  

6.     HttpURLConnection con = openConnection(config);  

7.     prepareConnection(con, baos.size());  

8.     //远端调用封装成RemoteInvocation对象,这个对象通过序列化被写到对应的HttpURLConnection中去  

9.     writeRequestBody(config, con, baos);  

10.     //这里取得远端服务返回的结果,然后把结果转换成RemoteInvocationResult返回  

11.     validateResponse(config, con);  

12.     InputStream responseBody = readResponseBody(config, con);  

13.   

14.     return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());  

15. }  

16.   

17. //把序列化对象输出到HttpURLConnection去  

18. protected void writeRequestBody(  

19.         HttpInvokerClientConfiguration config, HttpURLConnection con, ByteArrayOutputStream baos)  

20.         throws IOException {  

21.   

22.     baos.writeTo(con.getOutputStream());  

23. }  

24.   

25. //为使用HttpURLConnection完成对象序列化,需要进行一系列的配置  

26. //比如配置请求方式为post,请求属性等等  

27. protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {  

28.     con.setDoOutput(true);  

29.     con.setRequestMethod(HTTP_METHOD_POST);  

30.     con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());  

31.     con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));  

32.     LocaleContext locale = LocaleContextHolder.getLocaleContext();  

33.     if (locale != null) {  

34.         con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));  

35.     }  

36.     if (isAcceptGzipEncoding()) {  

37.         con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);  

38.     }  

39. }  

40. //获得HTTP响应的IO流  

41. protected InputStream readResponseBody(HttpInvokerClientConfiguration config, HttpURLConnection con)  

42.         throws IOException {  

43.     //如果是通过gzip压缩,那么需要先解压  

44.     if (isGzipResponse(con)) {  

45.         // GZIP response found - need to unzip.  

46.         return new GZIPInputStream(con.getInputStream());  

47.     }  

48.     else {  

49.         // Plain response found.  

50.         // 正常的HTTP响应输出  

51.         return con.getInputStream();  

52.     }  

53. }  

//这是HTTP调用器实现的基本过程,通过HTTP的request和reponse来完成通信,在通信的过程中传输的数据是序列化的对象

protected RemoteInvocationResult doExecuteRequest(

HttpInvokerClientConfiguration config, ByteArrayOutputStream baos)

throws IOException, ClassNotFoundException {

//打开一个标准J2SE HttpURLConnection

HttpURLConnection con = openConnection(config);

prepareConnection(con, baos.size());

//远端调用封装成RemoteInvocation对象,这个对象通过序列化被写到对应的HttpURLConnection中去

writeRequestBody(config, con, baos);

//这里取得远端服务返回的结果,然后把结果转换成RemoteInvocationResult返回

validateResponse(config, con);

InputStream responseBody = readResponseBody(config, con);

 

return readRemoteInvocationResult(responseBody, config.getCodebaseUrl());

}

 

//把序列化对象输出到HttpURLConnection去

protected void writeRequestBody(

HttpInvokerClientConfiguration config, HttpURLConnection con, ByteArrayOutputStream baos)

throws IOException {

 

baos.writeTo(con.getOutputStream());

}

//为使用HttpURLConnection完成对象序列化,需要进行一系列的配置

//比如配置请求方式为post,请求属性等等

protected void prepareConnection(HttpURLConnection con, int contentLength) throws IOException {

con.setDoOutput(true);

con.setRequestMethod(HTTP_METHOD_POST);

con.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());

con.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

LocaleContext locale = LocaleContextHolder.getLocaleContext();

if (locale != null) {

con.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale.getLocale()));

}

if (isAcceptGzipEncoding()) {

con.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);

}

}

//获得HTTP响应的IO流

protected InputStream readResponseBody(HttpInvokerClientConfiguration config, HttpURLConnection con)

throws IOException {

//如果是通过gzip压缩,那么需要先解压

if (isGzipResponse(con)) {

// GZIP response found - need to unzip.

return new GZIPInputStream(con.getInputStream());

}

else {

// Plain response found.

// 正常的HTTP响应输出

return con.getInputStream();

}

}



HTTP调用器服务器端的实现 

在服务器端使用Spring HTTP远端调用,需要配置HttpInvokerServiceExporter,作为远端服务的服务导出器,来接收HTTP服务请求。在通过HTTP 请求,得到客户端传过来的RemoteInvocation对象以后,就可以进行服务方法的调用了。服务调用需要的基本信息,都封装在 RemoteInvocation对象中。这个服务调用过程,是由invokeAndCreateResult方法来实现的,如 RemoteInvocationSerializingExporter的invoke实现所示:

Java代码

1. protected Object invoke(RemoteInvocation invocation, Object targetObject)  

2.         throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {  

3.   

4.     if (logger.isTraceEnabled()) {  

5.         logger.trace("Executing " + invocation);  

6.     }  

7.     try {//调用RemoteInvocationExecutor,这个执行器是DefaultRemoteInvocationExecutor  

8.         return getRemoteInvocationExecutor().invoke(invocation, targetObject);  

9.     }  

10.     catch (NoSuchMethodException ex) {  

11.         if (logger.isDebugEnabled()) {  

12.             logger.warn("Could not find target method for " + invocation, ex);  

13.         }  

14.         throw ex;  

15.     }  

16.     catch (IllegalAccessException ex) {  

17.         if (logger.isDebugEnabled()) {  

18.             logger.warn("Could not access target method for " + invocation, ex);  

19.         }  

20.         throw ex;  

21.     }  

22.     catch (InvocationTargetException ex) {  

23.         if (logger.isDebugEnabled()) {  

24.             logger.debug("Target method failed for " + invocation, ex.getTargetException());  

25.         }  

26.         throw ex;  

27.     }  

28. }  

protected Object invoke(RemoteInvocation invocation, Object targetObject)

throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {

 

if (logger.isTraceEnabled()) {

logger.trace("Executing " + invocation);

}

try {//调用RemoteInvocationExecutor,这个执行器是DefaultRemoteInvocationExecutor

return getRemoteInvocationExecutor().invoke(invocation, targetObject);

}

catch (NoSuchMethodException ex) {

if (logger.isDebugEnabled()) {

logger.warn("Could not find target method for " + invocation, ex);

}

throw ex;

}

catch (IllegalAccessException ex) {

if (logger.isDebugEnabled()) {

logger.warn("Could not access target method for " + invocation, ex);

}

throw ex;

}

catch (InvocationTargetException ex) {

if (logger.isDebugEnabled()) {

logger.debug("Target method failed for " + invocation, ex.getTargetException());

}

throw ex;

}

}


看到的invoke方法封装了服务器端调用的主体,这个invoke方法在HttpInvokerServiceExporter的基类 RemoteInvocationSerializingExporter中实现,服务对象的方法调用完成之后,会把调用结果,通过HTTP响应和对象序 列化,传给HTTP调用器客户端,从而完成整个HTTP调用器的远端调用过程,如以下代码所示:

Java代码

1. protected void writeRemoteInvocationResult(  

2.         HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result)  

3.         throws IOException {  

4. //设置Response的ContentType属性,设置为application/x-java-serialized-object  

5.     response.setContentType(getContentType());  

6.     writeRemoteInvocationResult(request, response, result, response.getOutputStream());  

7. }  

8. //输出到HTTP的Response,然后把Response关闭  

9. protected void writeRemoteInvocationResult(  

10.         HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os)  

11.         throws IOException {  

12.   

13.     ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os));  

14.     try {  

15.         doWriteRemoteInvocationResult(result, oos);  

16.         oos.flush();  

17.     }  

18.     finally {  

19.         oos.close();  

20.     }  

21. }  

protected void writeRemoteInvocationResult(

HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result)

throws IOException {

//设置Response的ContentType属性,设置为application/x-java-serialized-object

response.setContentType(getContentType());

writeRemoteInvocationResult(request, response, result, response.getOutputStream());

}

//输出到HTTP的Response,然后把Response关闭

protected void writeRemoteInvocationResult(

HttpServletRequest request, HttpServletResponse response, RemoteInvocationResult result, OutputStream os)

throws IOException {

 

ObjectOutputStream oos = createObjectOutputStream(decorateOutputStream(request, response, os));

try {

doWriteRemoteInvocationResult(result, oos);

oos.flush();

}

finally {

oos.close();

}

}


经过这一系列的处理过程,服务执行结果对象又回到了HTTP的远端调用客户端。在客户端从HTTP响应读取对象之后,它把这个看起来像是在本地实 现,其实是由远端服务对象完成的调用结果,交给发起远端调用的客户端调用方法,从而最终完成整个远端调用的过程。这个过程很有特点,它使用了HTTP的请 求和响应作为通信通道,在这个通信通道里面,并没有再做进一步的附加的通信协议的封装,而且,在这个处理过程中,使用的都是Java和Spring框架已 有的特性,比如,通过IoC的配置,以及代理对象拦截器的封装处理,再加Java的序列化和反序列化,以及在服务器端的Spring MVC框架的使用,通过这些已有的技术实现,让使用者感觉,它的实现风格非常的简洁轻快,整个代码实现,阅读起来,也让人感到非常的赏心悦目。

 

 

深入解析Spring架构与设计原理(六)Spring ACEGI

Spring ACEGI 
作为Spring丰富生态系统中的一个非常典型的应用,安全框架Spring ACEGI的使用是非常普遍的。尽管它不属于Spring平台的范围,但由于它建立在Spring的基础上,因此可以方便地与Spring应用集成,从而 方便的为基于Spring的应用提供安全服务。
作为一个完整的Java EE安全应用解决方案,ACEGI能够为基于Spring构建的应用项目,提供全面的安全服务,它可以处理应用需要的各种典型的安全需求;例如,用户的身 份验证、用户授权,等等。ACEGI因为其优秀的实现,而被Spring开发团队推荐作为Spring应用的通用安全框架,随着Spring的广泛传播而 被广泛应用。在各种有关Spring的书籍,文档和应用项目中,都可以看到它活跃的身影。

Spring ACEGI的基本实现 
关于ACEGI的基本设置,在这里就不多啰嗦了。我们关心的是ACEGI是怎样实现用户的安全需求的,比如最基本的用户验证,授权的工作原理和实现。
在ACEGI配置中,是通过AuthenticationProcessingFilter的过滤功能来启动Web页面的用户验证实现的。 AuthenticationProcessingFilter过滤器的基类是AbstractProcessingFilter,在这个 AbstractProcessingFilter的实现中,可以看到验证过程的实现模板,在这个实现模板中,可以看到它定义了实现验证的基本过程,如以 下代码所示:

Java代码

1.     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  

2.         throws IOException, ServletException {  

3.         //检验是不是符合ServletRequest/SevletResponse的要求  

4.         if (!(request instanceof HttpServletRequest)) {  

5.             throw new ServletException("Can only process HttpServletRequest");  

6.         }  

7.   

8.         if (!(response instanceof HttpServletResponse)) {  

9.             throw new ServletException("Can only process HttpServletResponse");  

10.         }  

11.   

12.         HttpServletRequest httpRequest = (HttpServletRequest) request;  

13.         HttpServletResponse httpResponse = (HttpServletResponse) response;  

14.   

15.         if (requiresAuthentication(httpRequest, httpResponse)) {  

16.             if (logger.isDebugEnabled()) {  

17.                 logger.debug("Request is to process authentication");  

18.             }  

19. //这里定义ACEGI中的Authentication对象,从而通过这个Authentication对象,来持有用户验证信息  

20.             Authentication authResult;  

21.   

22.             try {  

23.                 onPreAuthentication(httpRequest, httpResponse);  

24. //具体验证过程委托给子类完成,比如通过AuthenticationProcessingFilter来完成基于Web页面的用户验证  

25.                 authResult = attemptAuthentication(httpRequest);  

26.             } catch (AuthenticationException failed) {  

27.                 // Authentication failed  

28.                 unsuccessfulAuthentication(httpRequest, httpResponse, failed);  

29.   

30.                 return;  

31.             }  

32.   

33.             // Authentication success  

34.             if (continueChainBeforeSuccessfulAuthentication) {  

35.                 chain.doFilter(request, response);  

36.             }  

37. //验证工作完成后的后续工作,跳转到相应的页面,跳转的页面路径已经做好了配置  

38.             successfulAuthentication(httpRequest, httpResponse, authResult);  

39.   

40.             return;  

41.         }  

42.   

43.         chain.doFilter(request, response);  

44.     }  

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

        throws IOException, ServletException {

//检验是不是符合ServletRequest/SevletResponse的要求

        if (!(request instanceof HttpServletRequest)) {

            throw new ServletException("Can only process HttpServletRequest");

        }

 

        if (!(response instanceof HttpServletResponse)) {

            throw new ServletException("Can only process HttpServletResponse");

        }

 

        HttpServletRequest httpRequest = (HttpServletRequest) request;

        HttpServletResponse httpResponse = (HttpServletResponse) response;

 

        if (requiresAuthentication(httpRequest, httpResponse)) {

            if (logger.isDebugEnabled()) {

                logger.debug("Request is to process authentication");

            }

//这里定义ACEGI中的Authentication对象,从而通过这个Authentication对象,来持有用户验证信息

            Authentication authResult;

 

            try {

                onPreAuthentication(httpRequest, httpResponse);

//具体验证过程委托给子类完成,比如通过AuthenticationProcessingFilter来完成基于Web页面的用户验证

                authResult = attemptAuthentication(httpRequest);

            } catch (AuthenticationException failed) {

                // Authentication failed

                unsuccessfulAuthentication(httpRequest, httpResponse, failed);

 

                return;

            }

 

            // Authentication success

            if (continueChainBeforeSuccessfulAuthentication) {

                chain.doFilter(request, response);

            }

//验证工作完成后的后续工作,跳转到相应的页面,跳转的页面路径已经做好了配置

            successfulAuthentication(httpRequest, httpResponse, authResult);

 

            return;

        }

 

        chain.doFilter(request, response);

    }


在看到上面的对WEB页面请求的拦截后,处理开始转到ACEGI框架中后台了,我们看到,完成验证工作的主要类在ACEGI中是AuthenticationManager。如以下代码所示:

Java代码

1. public final Authentication authenticate(Authentication authRequest)  

2.     throws AuthenticationException {  

3.     try {  

4.  /*doAuthentication 是一个抽象方法,由具体的AuthenticationManager实现,从而完成验证工作。传入的参数是一个Authentication对象,在这 个对象中已经封装了从HttpServletRequest中得到的用户名和密码,这些信息都是在页面登录时用户输入的*/  

5.         Authentication authResult = doAuthentication(authRequest);  

6.         copyDetails(authRequest, authResult);  

7.         return authResult;  

8.     } catch (AuthenticationException e) {  

9.         e.setAuthentication(authRequest);  

10.         throw e;  

11.     }  

12. }  

13.   

14. /** 

15.  * Copies the authentication details from a source Authentication object to a destination one, provided the 

16.  * latter does not already have one set. 

17.  */  

18. private void copyDetails(Authentication source, Authentication dest) {  

19.     if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {  

20.         AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;  

21.   

22.         token.setDetails(source.getDetails());  

23.     }  

24. }  

25. protected abstract Authentication doAuthentication(Authentication authentication)  

26.     throws AuthenticationException;  

    public final Authentication authenticate(Authentication authRequest)

        throws AuthenticationException {

        try {

  /*doAuthentication是一个抽象方法,由具体的AuthenticationManager实现,从而完成验证工作。传入的参数是一个Authentication对象,在这个对象中已经封装了从HttpServletRequest中得到的用户名和密码,这些信息都是在页面登录时用户输入的*/

            Authentication authResult = doAuthentication(authRequest);

            copyDetails(authRequest, authResult);

            return authResult;

        } catch (AuthenticationException e) {

            e.setAuthentication(authRequest);

            throw e;

        }

    }

 

    /**

     * Copies the authentication details from a source Authentication object to a destination one, provided the

     * latter does not already have one set.

     */

    private void copyDetails(Authentication source, Authentication dest) {

        if ((dest instanceof AbstractAuthenticationToken) && (dest.getDetails() == null)) {

            AbstractAuthenticationToken token = (AbstractAuthenticationToken) dest;

 

            token.setDetails(source.getDetails());

        }

    }

    protected abstract Authentication doAuthentication(Authentication authentication)

        throws AuthenticationException;

 


而读取用户信息的操作,我们举大家已经很熟悉的DaoAuthenticationProvider作为例子。可以看到,在配置的JdbcDaoImpl中,定义了读取用户数据的操作,如以下代码所示:

Java代码

1.  public static final String DEF_USERS_BY_USERNAME_QUERY = "SELECT username,password,enabled FROM users WHERE username = ?";  

2.  public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "SELECT username,authority FROM authorities WHERE username = ?";  

3.  public UserDetails loadUserByUsername(String username)  

4.      throws UsernameNotFoundException, DataAccessException {  

5. //使用Spring JDBC SqlMappingQuery来完成用户信息的查询  

6.      List users = usersByUsernameMapping.execute(username);  

7. //根据输入的用户名,没有查询到相应的用户信息  

8.      if (users.size() == 0) {  

9.          throw new UsernameNotFoundException("User not found");  

10.      }  

11. //如果查询到一个用户列表,使用列表中的第一个作为查询得到的用户  

12.      UserDetails user = (UserDetails) users.get(0); // contains no GrantedAuthority[]  

13. //使用Spring JDBC SqlMappingQuery来完成用户权限信息的查询  

14.      List dbAuths = authoritiesByUsernameMapping.execute(user.getUsername());  

15.   

16.      addCustomAuthorities(user.getUsername(), dbAuths);  

17.   

18.      if (dbAuths.size() == 0) {  

19.          throw new UsernameNotFoundException("User has no GrantedAuthority");  

20.      }  

21.   

22.      GrantedAuthority[] arrayAuths = (GrantedAuthority[]) dbAuths.toArray(new GrantedAuthority[dbAuths.size()]);  

23.   

24.      String returnUsername = user.getUsername();  

25.   

26.      if (!usernameBasedPrimaryKey) {  

27.          returnUsername = username;  

28.      }  

29.  //根据查询的用户信息和权限信息,构造User对象返回  

30.      return new User(returnUsername, user.getPassword(), user.isEnabled(), true, true, true, arrayAuths);  

31.  }  

    public static final String DEF_USERS_BY_USERNAME_QUERY = "SELECT username,password,enabled FROM users WHERE username = ?";

    public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "SELECT username,authority FROM authorities WHERE username = ?";

    public UserDetails loadUserByUsername(String username)

        throws UsernameNotFoundException, DataAccessException {

//使用Spring JDBC SqlMappingQuery来完成用户信息的查询

        List users = usersByUsernameMapping.execute(username);

//根据输入的用户名,没有查询到相应的用户信息

        if (users.size() == 0) {

            throw new UsernameNotFoundException("User not found");

        }

//如果查询到一个用户列表,使用列表中的第一个作为查询得到的用户

        UserDetails user = (UserDetails) users.get(0); // contains no GrantedAuthority[]

//使用Spring JDBC SqlMappingQuery来完成用户权限信息的查询

        List dbAuths = authoritiesByUsernameMapping.execute(user.getUsername());

 

        addCustomAuthorities(user.getUsername(), dbAuths);

 

        if (dbAuths.size() == 0) {

            throw new UsernameNotFoundException("User has no GrantedAuthority");

        }

 

        GrantedAuthority[] arrayAuths = (GrantedAuthority[]) dbAuths.toArray(new GrantedAuthority[dbAuths.size()]);

 

        String returnUsername = user.getUsername();

 

        if (!usernameBasedPrimaryKey) {

            returnUsername = username;

        }

  //根据查询的用户信息和权限信息,构造User对象返回

        return new User(returnUsername, user.getPassword(), user.isEnabled(), true, true, true, arrayAuths);

    }



ACEGI授权器的实现 
ACEGI就像一位称职的,负责安全保卫工作的警卫,在它的工作中,不但要对来访人员的身份进行检查(通过口令识别身份),还可以根据识别出来的 身份,赋予其不同权限的钥匙,从而可以去打开不同的门禁,得到不同级别的服务。从这点上看,与在这个场景中的“警卫”人员承担的角色一样,ACEGI在 Spring应用系统中,起到的也是类似的保卫系统安全的作用,而验证和授权,就分别对应于警卫识别来访者身份和为其赋予权限的过程。
为用户授权是由AccessDecisionManager授权器来完成的,授权的过程,在授权器的decide方法中实现,这个decide方 法是AccessDecisionManger定义的一个接口方法,通过这个接口方法,可以对应好几个具体的授权器实现,对于授权器完成决策的规则实现, 在这里,我们以AffirmativeBased授权器为例,看看在AffirmativeBased授权器中,实现的一票决定授权规则是怎样完成的,这 个实现过程,如以下代码所示:

Java代码

1.     public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config)  

2.         throws AccessDeniedException {  

3.            //取得配置投票器的迭代器,可以用来遍历所有的投票器  

4.         Iterator iter = this.getDecisionVoters().iterator();  

5.         int deny = 0;  

6.   

7.         while (iter.hasNext()) {  

8.               //取得当前投票器的投票结果  

9.             AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();  

10.             int result = voter.vote(authentication, object, config);  

11.               //对投票结果进行处理,如果是遇到ACCESS_GRANT的结果,授权直接通过  

12.               //否则,累计ACCESS_DENIED的投票票数  

13.             switch (result) {  

14.             case AccessDecisionVoter.ACCESS_GRANTED:  

15.                 return;  

16.   

17.             case AccessDecisionVoter.ACCESS_DENIED:  

18.                 deny++;  

19.   

20.                 break;  

21.   

22.             default:  

23.                 break;  

24.             }  

25.         }  

26. //如果有反对票,那么拒绝授权  

27.         if (deny > 0) {  

28.             throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",  

29.                     "Access is denied"));  

30.         }  

31. // 这里对弃权票进行处理,看看是全是弃权票的决定情况,默认是不通过,这种处理情况,是由allowIfAllAbstainDecisions变量来控制的  

32.         // To get this far, every AccessDecisionVoter abstained  

33.             checkAllowIfAllAbstainDecisions();  

34.     }  

    public void decide(Authentication authentication, Object object, ConfigAttributeDefinition config)

        throws AccessDeniedException {

   //取得配置投票器的迭代器,可以用来遍历所有的投票器

        Iterator iter = this.getDecisionVoters().iterator();

        int deny = 0;

 

        while (iter.hasNext()) {

  //取得当前投票器的投票结果

            AccessDecisionVoter voter = (AccessDecisionVoter) iter.next();

            int result = voter.vote(authentication, object, config);

  //对投票结果进行处理,如果是遇到ACCESS_GRANT的结果,授权直接通过

  //否则,累计ACCESS_DENIED的投票票数

            switch (result) {

            case AccessDecisionVoter.ACCESS_GRANTED:

                return;

 

            case AccessDecisionVoter.ACCESS_DENIED:

                deny++;

 

                break;

 

            default:

                break;

            }

        }

//如果有反对票,那么拒绝授权

        if (deny > 0) {

            throw new AccessDeniedException(messages.getMessage("AbstractAccessDecisionManager.accessDenied",

                    "Access is denied"));

        }

// 这里对弃权票进行处理,看看是全是弃权票的决定情况,默认是不通过,这种处理情况,是由allowIfAllAbstainDecisions变量来控制的

        // To get this far, every AccessDecisionVoter abstained

checkAllowIfAllAbstainDecisions();

    }



可以看到,在ACEGI的框架实现中,应用的安全需求管理,主要是由过滤器、验证器、用户数据提供器、授权器、投票器,这几个基本模块的协作一起 完成的。这几个基本模块的关系,刻画出了ACEGI内部架构的基本情况,也是我们基于ACEGI实现Spring安全应用,需要重点关注的地方。

 

0 0