【翻译】spring配置全书(上)

作者简介:

 

Craig Walls Texas-based 公司的软件开发人员,有着超过 13 年的开发经验,涉及的领域有通信,金融,零售,教育以及软件业等。他是 Spring Framework 的狂热拥护者,频繁的在当地 local user groups 讨论组和相关会议上演讲 Spring ,并且他的 Blog 上也有很多关于 Spring 的内容。

 

出版的著作有:

l          Spring in Action, 2nd Edition, 2007

l          XDoclet in Action, 2003

 

他的 Blog 是:

l          http://www.springinaction.com

 

所参与的项目:

l          Committer to XDoclet project;

l          Originator of Portlet and Spring modules for XDoclet

 

 

 

本手册主要是将分布于文档中的那些零散的配置文件部分统一成一个比较系统的整体。结合 Spring 文档一起查阅也许能节省你一些时间。不过,并不推荐你全部掌握;很多陌生的元素或标签只应用于特定场合。本手册英文版本可以在: http://java.dzone.com 下载。

 

 

 

 

 

 

 

Spring 配置全书

 

 

关于 Spring 的配置

 

Spring Framework 总是不断的改变着 Java 企业开发的方向,它用一种松耦合的方式来配置和组装应用程序对象和业务对象,比以往的 Java 企业开发来的更加简洁。一旦你开发了基于 Spring 的应用程序,在 Spring 上下文配置的那些资源简直就是唾手可得。

 

 

 

依赖注入是 Spring 容器的核心

 

 

尽管 Spring Framework 可以做很多事,但依赖注入却是 Spring 容器提供的最基本的功能。

 

任何稍微复杂一点的应用程序都至少由两个或两个以上的对象协作在一起,共同完成一些业务逻辑。以往的 Java 企业开发,每个对象都要自己去主动获得他们所引用(或依赖)的对象,才可正常运作。这将导致代码之间的紧耦合,难以测试。

 

 

 

 

 

 

有了依赖注入后,对象所依赖的资源则可通过外部来获得。换句话说,对象所依赖的资源是按照它们的需要给注入进去的。对于基于 Spring 的应用程序来说,是 Spring 容器将这些对象所依赖的资源帮助实现注入依赖的。

 

 

 

 

 

 

 

XML 来配置 Spring

 

 

到了 Spring2.0 Spring 鼓励你使用基于 XML Scheme 的配置方式来应用于你的系统,这比起过去基于 DTD 的方式要更加灵活。一个典型的 Spring2.5 配置文件至少拥有以下结构:

 

 

 

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd> 

<!-- place configuration details here -->

</beans>
 

 

 

 

 

Beans 命名空间简介

 

 

Schema URI

www.springframework.org/schema/beans

 

Schema XSD

www.springframework.org/schema/beans/spring-beans-2.5.xsd

 

beans 命名空间是Spring 命名空间的核心,也是你配置Spring 时使用最多的一个。根元素是<beans> ,它不仅可以包含一个或多个<bean> 子元素,而且还可以包含其它命名空间的元素,甚至在<beans> 下你可以不配置任何<bean> 子元素。

 

 

Spring XML 图表的一些约定

 

Spring XML 图通常使用以下符号来表示哪些元素是必选的,可选的以及它们之间的包含关系。

 

 

 

 

 

 

 

 

 

Bean 命名空间下的元素简介

<bean> 元素揭密

 

虽然有很多 XML 元素可以用来配置 Spring context ,但也许你用的最多的可能还是 <bean> 元素。因此,让你深入了解 <bean> 标签是十分必要的。

 

 

 

Bean 命名空间实例

 

下面的 Spring XML 配置文件配置了两个 beans ,其中一个注入到另一个中去:

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd”> 

<bean id=”pirate” class=”Pirate”> 
<constructor-arg value=”Long John Silver” /> 
<property name=”map” ref=”treasureMap” /> 
</bean> 

<bean id=”treasureMap” class=”TreasureMap” />

</beans>

 

 

 

第一个 bean ID 为“ pirate ”,类型为“ Pirate ”。它使用了构造函数注入,该构造函数带有一个 String 参数,在这个例子中参数的值为“ Long John Silver ”。另外,它的“ map ”属性引用了另一个叫“ treasureMap ”的 bean ,该 bean TreasureMap 的一个实例。

 

 

 

 

温馨提示:

 

不要把你所有的 beans 都定义在一个 XML 文件中。一旦你的应用程序变得越来越复杂,在 Spring XML 配置文件中定义的 beans 的数量一定让你印象深刻。也没有什么理由要把所有的 beans 都定义在一个 XML 配置文件中去。通过将所有的 beans 分别放在多个 XML 文件中,有助于你的 Spring 配置文件更易于管理。当应用程序上下文 (application context) 建立的时候,可以使用 <import> 元素将它们全部组装起来:

 

 

<import resource=”service-layer-config.xml” /> 
<import resource=”data-layer-config.xml” /> 
<import resource=”transaction-config.xml” />

 

 

 

 

 

 

Context 命名空间简介

 

 

Schema URI

www.springframework.org/schema/context

 

Schema XSD

www.springframework.org/schema/context/spring-context-2.5.xsd

 

 

Spring2.5 中, context 命名空间主要用来提供多种 application context 特定的配置。它包括:支持基于 annotation 的配置方式, JMX 以及领域对象 (domain object) 的注入。

 

 

 

 

 

 

 

Context 命名空间元素简介

 

 

 

 

 

 

Bean 命名空间实例

 

 

下面的 Spring 配置文件使用了 <context:component-scan> 用来自动注册“ com.springinactin.service ”包下的 beans

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:context=”http://www.springframework.org/schema/context” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd”> 

<context:component-scan base-package=”com.springinac¬tion.service” />

</beans>
 

 

 

 

在上面的配置文件中, <context:component-scan> 元素会自动扫描“ com.springinac­tion.service ”包下的类,并自动将那些标记有 @Component, @Controller, @Repository, @Service @Aspect. 的类全部注册到 Spring 容器中。

 

 

 

 

 

温馨提示:

 

尽量为你的最终用户提供外部配置文件。

将所有的配置都定义在 Spring 配置文件中并不推荐。你根本别指望应用程序的管理员或最终用户会去研究 Spring XML 然后搞懂数据库的配置和其它特定布署细节,你不会真打算靠他们吧?相反,使用外部配置文件,我们可以使用 <context:property-placeholder> 这么来做:

 

<context:property-placeholder location=”file:etc/pirate.properties”
 

 

定义在 /etc/pirate.properties 属性文件中的那些键值对现在可以在 Spring 配置文件中大显身手啦:

 

<bean id=”pirate” class=”Pirate”> 
<constructor-arg value=”${pirate.name}” />
</bean>
 

 

 

 

 

AOP 命名空间简介

 

 

Schema URI

www.springframework.org/schema/aop

 

Schema XSD

www.springframework.org/schema/aop/spring-context-2.5.xsd

 

 

 

aop 命名空间是用来在 Spring context 中声明 aspects, pointcutsadvice 的。同样,使用 @Aspectj

annotation 的话,也可以基于 annotation 方式来配置你的 aop 。使用 aspects 的话,可以定义被你多个程序切点使用(或织入)的功能。

 

 

 

 

 

AOP 命名空间元素简介

 

 

 

 

 

 

AOP 命名空间实例

 

下面的 Spring 配置文件使用了 aop 命名空间来建一个 aspect

 

 

<?xml version=”1.0” encoding=”UTF-8”?>
<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:aop=”http://www.springframework.org/schema/aop” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd”> 
<bean id=”pirateTalker” class=”PirateTalker” /> 
	<aop:config> 
	<aop:pointcut id=”plunderPointcut” expression=”execution(* *.plunder(..))” /> 
	<aop:aspect ref=”pirateTalker”> 
	<aop:before pointcut-ref=”plunderPointcut” method=”sayAvastMeHearties” /> 
	<aop:after-returning pointcut-ref=”plunderPointcut” method=”sayYarr” /> 
</aop:aspect> 
</aop:config>
</beans>
 

 

 

 

 

aspect 由一个 pointcut 和两个 advice 组成。该 pointcut 用来定义所有对象 plunder() 方法的执行。标有 <asp:before> advice 用来配置当 plunder() 执行时,立即调用 pirateTalker beansayAvastMeHearties() 方法;类似的,当 plunder() 方法调用成功时, sayYarr() 方法同样也会触发。

 

 

 

 

 

JEE 命名空间简介

 

 

Schema URI

www.springframework.org/schema/jee

 

Schema XSD

www.springframework.org/schema/jee/spring-context-2.5.xsd

 

 

 

 

 

JEE 命名空间元素简介

 

 

 

 

 

 

JEE 命名空间实例

 

下面的 Spring 配置文件使用了部分 jee 命名空间元素,用来获取 Spring 容器外的对象,并且将这些对象作为 Springbeans

 

 

<?xml version=”1.0” encoding=”UTF-8”?>

<beans xmlns=”http://www.springframework.org/schema/beans” 
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” 
xmlns:jee=”http://www.springframework.org/schema/jee” 
xsi:schemaLocation=”http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd”> 

<jee:remote-slsb id=”hispaniola” jndi-name=”ejb/PirateShip” business-interface=”com.pirate.PirateShipEjb”  resource-ref=”true” />
<jee:jndi-lookup id=”parrot” jndi-name=”pirate/Parrot “ resource-ref=”false” />

</beans>
 

 

 

第一个元素 <jee:remote-slsb> 配置了一个叫“ Hispaniola ”的 bean ,它实际上引用的是一个 EJB2remote stateless session bean 。这里 EJBhome interface 可以通过 JNDI 名称“ java:comp/env/ejb/PirateShip ”找到。属性“ resource-ref ”表示应该值会自动加上“ java:comp/env/ ”前缀。 EJB 的实现方法定义在 PirateShipEjb 业务接口中。

 

 

另一个元素 <jee:jndi-lookup> 用于从 JNDI 获取对象的引用(它可以是一个 EJB3 session bean 或一个普通 pojo )。对象会在这个叫“ pirate/Parrot ”的 JNDI 中获得。注意这里我们将“ resource-ref ”属性配置为“ false ”,所以应该 jndi-name 不会加上“ java:comp/env ”前缀。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值