简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。图1是Spring的总体架构图。
图1 Spring 框架的总体架构
- Core Container
在图1中的Core Container中的Core组件 和Bean组件提供了Spring最基本的特性:IOC(控制反转)和DI(依赖注入)特性。这里最主要的概念是BeanFactory。
Data Access/Integeration
Data Access/Integeration中的Transaction组件,用于事务编程和声明事务管理。如下所示为Spring+struts+hibernate框架中的applicationContext.xml文件中的
事务管理器的配置:
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置Advice(事务的传播特性) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 配置事务管理器应用的范围 -->
<aop:config>
<aop:pointcut id="serviceMethod" expression="execution(* com.chengbin.client.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
</aop:config>
- Web
Web层包括Web、Web-Servlet。
1) Spring IOC
控制反转(IOC)是Spring整个框架的核心部份,所谓IOC,其实就是将设计好的类交给系统Spring框架去控制。基于IOC基础上的依赖注入(DI)方式来管理对象之间的依赖关系,使组件之间的依赖关系由组件的代码中来管理维护转移到容器来管理配置,有助于黏合程序中的各个不同模块,形有一个有逻辑意义的整体,简化业务层的工作量,提高模块的复用度。
Spring的IOC容器
Spring的两个最基本最重要的包是:org.springframework.beans和org.springframework.context包。这两个包中的代码为Spring的反向控制特性(也叫作依赖注射)提供了基础。如图二是 Spring IOC容器,其中的元数据通常是通过XML文件进行配置。当运用基于XML的形式配置元数据时,为Spring IOC 容器需要
管理的Bean 进行定义。
图二 Spring IOC容器
最基本的配置方式:
<!--more beans to go here -->
</beans>
BeanFactory提供了能够管理任何种类beans(对象)的先进的配置机制,潜在地利用任何一种存储设备。ApplicationContext建立在BeanFactory之上并增加了其他的功能,比如同Spring AOP特性更容易整合,信息资源处理(用于国际化),事件传播,声明式机制用于创建ApplicationContext和可选的父上下文以及与应用层相关的上下文(比如WebApplicationContext),以及其他的增强。
简而言之,BeanFactory提供了配置框架和基本的功能,而ApplicationContext为它增加了更强的功能,这些功能中的一些或许更加J2EE和企业中心(enterprise-centric)。一般来说,ApplicationContext是BeanFactory的完全超集,任何BeanFactory功能和行为的描述也同样被认为适用于ApplicationContext。
用户有时在特定的场合下不确定BeanFactory和ApplicationContext哪一个更适于使用。通常大部分在J2EE环境中构建的应用最好的选择是使用ApplicationContext,因为它不仅提供了BeanFactory所有的特性以及它自己附加的特性,而且还提供更声明化的方法去使用一些功能,这通常是令人满意的。你最好选择BeanFactory的主要场景通常是当内存使用是最主要的关注(比如在一个每kb都要计算的applet中),而且也不需要ApplicationContext所有特性的时候。
依赖注入(DI)
依赖注入存在的最常用的两种形式:构造子注入(Constructor Injection)和setter注入(Setter Injection),还有一种方式是接口注入。
- 构造子注入(Constructor Injection)
<bean id="exampleBean" class="examples.ExampleBean">
<!-- constructor injection using the nested <ref/> element -->
<constructor-arg>
<ref bean="anotherExampleBean"/>
</constructor-arg>
<!-- constructor injection using the neater 'ref' attribute -->
<constructor-arg ref="yetAnotherBean"/>
<constructor-arg type="int" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
- setter注入(Setter Injection)
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested <ref/> element -->
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<!-- setter injection using the neater 'ref' attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
- 接口注入
接口注入指的就是在接口中定义要注入的信息,并通过接口完成注入。
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
2) Spring AOP
Spring AOP可以以面向方面编程的方式增强POJO的运行能力,弥补OOP之缺憾,从另一个角度方便的解决了开发中所需的公共处理,例如它可向POJO组件提供方法拦截,透明的为方法执行增加服务如事务、检测、日志记录等公共服务。