【翻译】spring配置全书(下)——附PDF完整版下载
关键字: spring2.5, 配置大全JMS 命名空间简介
Schema URI
www.springframework.org/schema/jms
Schema XSD
www.springframework.org/schema/jee/spring-jms-2.5.xsd
JMS 命名空间提供的元素可用来配置 message-driven POJO (消息驱动 POJO ), beans 用来响应 JMS destination ( 比如说一个 topic 或 queue) 。
JMS 命名空间元素简介
JMS
命名空间实例
在下面的
Spring
配置文件中,我们建了一个
message-driven POJO
,用来响应
JMS
上的消息队列。
<?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:jms=”http://www.springframework.org/schema/jms” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd”> <bean id=”connectionFactory” class=”org.apache.activemq.ActiveMQConnectionFactory”> <property name=”brokerURL” value=”tcp://localhost:61616” /> </bean> <bean id=”messageHandlerService” class=”com.pirate.Message¬HandlerImpl” /> <jms:listener-container connection-factory=”connectionFactory”> <jms:listener destination=”queue.bottle” ref=”messageHandlerService” method=”readMessageFromBottle” /> </jms:listener-container> </beans>
<jms:listener-container> 配置了一个容器,专门用于处理到达 JMS connection factory 里的 topics 或 queues 的消息。在这个元素里,你可以声明一个或多个 <jms:listener> 元素,用来响应各自的 topics 。在这个例子中,单独的 <jms:listener> 会在消息到达“ queue.bottle ”这个 topic 时,调用“ messageHandlerService ” bean 的 readMessageFromBottle() 方法。
LANG 命名空间简介
Schema URI
www.springframework.org/schema/lang
Schema XSD
www.springframework.org/schema/lang/spring-jms-2.5.xsd
“ LANG ”命名空间允许你在 Spring context 中配置 scripted objects( 脚本对象 ) 。这些对象可以是 Groovy , JRuby 或者 BeanShell 。
LANG 命名空间元素简介
LANG 命名空间实例
在下面的 Spring context 中, Pirate beans 的属性分别被定义为 <lang:groovy> 和 <lang:jruby> 的两个 scripted 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:lang=”http://www.springframework.org/schema/lang” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd”> <bean id=”jackSparrow” class=”Pirate”> <constructor-arg value=”Jack Sparrow” /> <property name=”compass” ref=”compass” /> <property name=”hat” ref=”hat” /> </bean> <lang:groovy id=”compass” script-source=”classpath:Compass.groovy” refresh-check-delay=”10000” /> <lang:jruby id=”hat” script-source=”classpath:PirateHat.rb” script-interface=”PirateHat” refresh-check-delay=”60000” /> </beans>
<lang:groovy> 元素建了一个由 Groovy script 实现的 bean ,名字叫 Compass.groovy 并且放在 classpath 的根目录下。 refresh-check-delay 属性表示每 10 秒钟看看该 script 是否改变了,然后对其进行相应的 update 和 reload 操作。
<lang:jruby> 元素建了一个由 Ruby( 指定为 JRuby) script 实现的 bean ,名字叫 PirateHat.rb 。它实现了 PirateHat 接口并每分钟都会检查是否需要 update 。
TX 命名空间简介
Schema URI
www.springframework.org/schema/tx
Schema XSD
www.springframework.org/schema/tx/spring-jms-2.5.xsd
“ TX ”命名空间为声明在 Spring context 中的那些 Beans 提供声明式事务处理的支持。
TX 命名空间元素简介
TX
命名空间实例
在下面的
Spring context
中,使用了
tx
命名空间下的元素,用来配置事务规则和界限:
<?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:tx=”http://www.springframework.org/schema/tx” 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”> <tx:jta-transaction-manager /> <tx:advice id=”txAdvice”> <tx:attributes> <tx:method name=”plunder*” propagation=”REQUIRED” /> <tx:method name=”*” propagation=”SUPPORTS” /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut=”execution(* ..Pirate.*(..))” advice-ref=”txAdvice” /> </aop:config> </beans>
<tx:jta-transaction-manager>
已经加入到
Spring2.5
中用于自动检测由
WebLogic
,
WebSphere
或
OC4J
提供的
JTA
事务管理。它默认是以叫“
transactionManager
”的
bean
配置在
Spring context
中的。
接下来,
<tx:advice>
建立了一个
AOP advice
用于声明事务处理规则。在这个例子中,任何以“
plunder
”为前缀的方法都会得到一个事务。其它方法的规则则是“
support
”事务,并不要求一定会有事务来参与。最后,这个从先前
aop
命令空间借用的例子使用了具备事务性质的
advice
来配置成一个
AOP advistor
。定义在这儿的
pointcut
只对
Pirate
类的所有方法起作用。
温馨提示:
Java
事务配置规则
如果你正在考虑压缩
Spring
配置的
XML
数据的话,考虑用
<tx:annotation-driven>
元素试试吧。一旦该元素配置好了,你可以使用开始使用
annotation
来配置你的
beans
,而且只要使用
@Transactional
来定义事务边界和规则即可。你可以看看
Spring In Action 2nd
第六章
6.4.4
节了解更多内容。
UTIL 命名空间简介
Schema URI
www.springframework.org/schema/util
Schema XSD
www.springframework.org/schema/util/spring-jms-2.5.xsd
util 命名空间可以将 collections 和其它 non-bean 对象封装在 Spring 配置文件中,让你感觉它们同其它 bean 没有什么区别。
UTIL 命名空间元素简介
UTIL
命名空间实例
在下面的
Spring context
配置文件中,使用了
util
命名空间下的元素:
<?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:util=”http://www.springframework.org/schema/util” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd”> <util:list id=”piratePhrases”> <value>Yo ho ho</value> <value>Yarr</value> <value>Avast me hearties!</value> <value>Blow me down</value> </util:list> <util:constant id=”pirateCode” static-field=”Pirate.PIRATE_CODE” /> <util:property-path id=”doubloonCount” path=”pirate.treasure.doubloonCount” /> </beans>
<util:list>
元素用来建一个持有
String
的
list
。
<util:constant>
元素则是用来建立一个
Pirate
类的常量字段“
PIRATE_CODE
”
(
该字段是
public static
标识符
)
的引用。最后,
<util:property-path>
元素先从“
pirate
”
bean
得到它的“
treasure
”属性,再从“
treasure
”得到它的属性“
doubloonCount
”,再将这个“
doubloonCount
”暴露在
Spring context
中。(注意上面配置文件中的“
pirate.treasure.doubloonCount
”里面的
pirate
是已经配置在
Spring context
中的
bean
,这里作者并没有配置出来。)以上三个配置实例里,都通过
util
命名空间元素将其暴露在
Spring context
中去了,然后你可以随意将它们注入到其它的
beans
中去。
Spring Annotation
简介
历史上,
Spring
的配置最初只涵盖
XML
方式。但是
Spring
渐渐也与基于
annotation
的配置方式打成一片。到了
Spring2.5
,已经提供超过
36
个
annotation
的支持了,这还不包括第三方类库和
Spring add-ons
。
Context Configuration Annoations
这一组
annotations
主要是让
Spring
来创建和注入
bean
对象的。
Transaction Annoations
@Transactional annotation
协同
<tx:annotation-driven>
元素用来为类或方法声明事务边界和规则。
Stereotyping Annoations
这组
annotations
主要用在应用程序中各个层次中的
stereotype
类型。如果在
Spring
配置文件中还配置了
<context:component-scan>
元素,而且某个类还被标注了下列
annotation
的话,该类就会自动被注册到
Spring context
中去。
另外,如果一个
PersistenceExceptionTranslationPostProcessor
配置在
Spring context
中,那么任何标记了
@Repository annotation
的
bean
在方法执行过程中抛出的
SQLExceptions
会被转换成
Spring
的
unchecked
DataAccessExceptions
。
Spring MVC Annoations
这组
annotations
到
Spring2.5
时才被引入。它让
Spring MVC
应用程序的创建变得更加容易,不仅将
XML
配置文件缩减到最小化,而且不需要去继承
Controller
interface
的实现了。
JMX Annoations
这组 annotations 协同 <context:mbean-export> 元素,将 bean 的方法和属性声明为 MBean 的操作和属性。
Testing Annoations
这组 annotations 主要用来建立 JUnit4 风格的单元测试,但前提是得依赖于 Spring context 中定义的 beans 或一个 transactional context 。
全文完
评论
楼主辛苦了!
share good
辛苦了,楼主
普渡众生啊
谢谢,谢谢
发表评论
- 浏览: 135254 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
共 26 张
最新评论
-
自己动手写一个Struts2
谢谢 phz50 提供的方法,很优雅。
-- by kyo100900 -
自己动手写一个Struts2
首先作者这个写得真不错,不过有几处可能不够妥当:关于StrutsFilter中s ...
-- by phz50 -
用Spring2.5和ICEFaces开 ...
我要页面啦~~~
-- by zjcone -
自己动手写一个Struts2
public static void main(String strs[]) ...
-- by kkllmey -
用Spring2.5和ICEFaces开 ...
能不能告诉偶要作你说的这个的话~都需要什么JAR包啊!~ - -困惑中~
-- by zjcone






评论排行榜