【翻译】spring配置全书(下)——附PDF完整版下载

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 annotationbean 在方法执行过程中抛出的 SQLExceptions 会被转换成 Springunchecked DataAccessExceptions

 

 

 

 

 

Spring MVC Annoations

 

 

这组 annotationsSpring2.5 时才被引入。它让 Spring MVC 应用程序的创建变得更加容易,不仅将 XML 配置文件缩减到最小化,而且不需要去继承 Controller interface 的实现了。

 

 

 

JMX Annoations

 

 

这组 annotations 协同 <context:mbean-export> 元素,将 bean 的方法和属性声明为 MBean 的操作和属性。

 

 

 

Testing Annoations

 

 

这组 annotations 主要用来建立 JUnit4 风格的单元测试,但前提是得依赖于 Spring context 中定义的 beans 或一个 transactional context

 

 

 

 

 

 

 

全文完

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值