空间站

北极心空

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks
Spring 2.x在配置文件的简化的方面做了很多工作,原来1.x中比较麻烦的配置都已经拥有了比较完美的解决方案。最近刚看完《精通Spring 2.x --企业应用开发精解》的书,结合自己的经验整理一下简化配置的内容。

一、关于集合的配置
1.List
>1.x版本的
Java代码 复制代码
  1. <bean id="parentBoss" abstract="true"class="com.baobaotao.attr.Boss"> <--父<bean>   
  2.     <property name="favorites">   
  3.         <set>   
  4.             <value>看报</value>   
  5.             <value>赛车</value>   
  6.             <value>高尔夫</value>   
  7.         </set>   
  8.     </property>   
  9. </bean>  


>2.x版本的
Java代码 复制代码
  1. <util:list id="favoriteList1" list-class="java.util.LinkedList">   
  2.     <value>看报</value>   
  3.     <value>赛车</value>   
  4.     <value>高尔夫</value>   
  5. </util:list>  


2.Set
> 1.x
Java代码 复制代码
  1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
  2.     <property name="favorites">   
  3.        <set>    
  4.           <value>看报</value>   
  5.           <value>赛车</value>   
  6.           <value>高尔夫</value>   
  7.        </set>   
  8.     </property>   
  9. </bean>  

> 2.x
Java代码 复制代码
  1.     
  2. <util:set id="favoriteSet1">   
  3.  <value>看报</value>   
  4.  <value>赛车</value>   
  5.   <value>高尔夫</value>   
  6. </util:set>  

3.Map
> 1.x
Java代码 复制代码
  1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
  2.     <property name="jobs">   
  3.     <map>   
  4.           <!--Map第一个元素-->   
  5.                <entry>    
  6.               <key><value>AM</value></key>   
  7.               <value>会见客户</value>   
  8.            </entry>   
  9.           <!--Map第二个元素-->   
  10.                <entry>    
  11.               <key><value>PM</value></key>   
  12.               <value>公司内部会议</value>   
  13.           </entry>                 
  14.     </map>   
  15.     </property>   
  16. </bean>  

> 2.x
Java代码 复制代码
  1. <util:map id="emails1">   
  2.     <entry key="AM" value="会见客户" />   
  3.     <entry key="PM" value="公司内部会议" />   
  4. </util:map>  


4. Properties

> 1.x
Java代码 复制代码
  1. <bean id="boss1" class="com.baobaotao.attr.Boss">   
  2.     <property name="mails">   
  3.         <props>   
  4.            <prop key="jobMail">john-office@baobaotao.com</prop>   
  5.            <prop key="lifeMail">john-life@baobaotao.com</prop>   
  6.         </props>   
  7.     </property>   
  8. </bean>  


> 2.x
Java代码 复制代码
  1. <util:properties id="emailProps1" location="classpath:com/baobaotao/fb/mails.properties"/>  

可以在一个属性文件中直接配置属性,这比较符合一般的项目习惯。

二、 关于事务配置


1.1.x
Java代码 复制代码
  1.  <bean id="bbtForum"    
  2. class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">   
  3. <property name="transactionManager" ref="txManager" />    
  4. <property name="target" ref="bbtForumTarget"/>   
  5. <property name="transactionAttributes">    
  6.     <props>   
  7.         <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>    
  8.         <prop key="*">PROPAGATION_REQUIRED</prop>    
  9.     </props>   
  10. </property>   
  11.  </bean>  


2.2.x

有两种新方法
a)使用@Transactional注解
在需要的服务类或服务方法处直接打上@Transactional注解,然后在Spring配置文件中启用注解事务驱动就可以了:

Java代码 复制代码
  1. @Transactional    
  2. public class BbtForumImpl implements BbtForum {   
  3.     @Transactional(readOnly=true)    
  4.     public Forum getForum(int forumId) {   
  5.         return forumDao.getForum(forumId);   
  6.     }   
  7.        。。。。   
  8. }  

在Spring配置文件中相应添加上:
Java代码 复制代码
  1. <bean id="txManager"  
  2.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
  3.         <property name="dataSource" ref="dataSource" />   
  4. </bean>   
  5. <tx:annotation-driven transaction-manager="txManager"/>  

这样就OK了,简单吧:)

b)使用aop/tx

如果你的Service服务类都很规范,我觉得使用aop/tx更方面,因为不用到处打注解,在一处集中配置就OK了,可谓运筹帷幄之中,决胜于千里之外:)
Java代码 复制代码
  1. <aop:config>    
  2. t;aop:pointcut id="serviceMethod"    
  3.    expression="execution(* com.baobaotao.service.*Forum.*(..))" />   
  4. t;aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />    
  5.  </aop:config>   
  6. <tx:advice id="txAdvice" transaction-manager="txManager">    
  7.    <tx:attributes>    
  8.        <tx:method name="get*" read-only="false"/>   
  9.        <tx:method name="add*" rollback-for="Exception" />   
  10.        <tx:method name="update*"/>            
  11.    </tx:attributes>   
  12. lt;/tx:advice>   


三、关于AOP配置

原来1.x的AOP太麻烦了,提都不想提,直接说一下2.x的AOP。
Spring 2.x使用@AspectJ来描述切面,由于@AspectJ的语法描述能力超强,因此在Spring 2.x中使用AOP真的非常方便。

在使用@AspectJ之前,首先你得保证你所使用的JDK的版本是5.0及以上版本,否则无法使用注解技术。
Spring在处理@Aspect注解表达式时,需要使用位于spring/lib/asm下asm关联类库,将该类库的三个类包加入到类路径中:asm-2.2.2.jar、asm-commons-2.2.2.jar和asm-util-2.2.2.jar。我们在第一章中了解了asm类库的用途,它是轻量级的字节码处理框架,因为Java的反射机制无法获取入参名,Spring就利用asm处理@AspectJ中所描述的方法入参名。

此外,Spring采用AspectJ提供的@AspectJ注解类库及相应的解析类库,它位于spring/lib/aspectj目录下,将目录下的aspectjrt.jar和aspectjweaver.jar类包加入类路径中。
在做好上节中所提到的前置工作后,我们就可以开始编写一个基于@AspectJ的切面了,首先来看一个简单的例子,以便对@AspectJ有一个切身的认识。

@AspectJ采用不同的方式对AOP进行描述, 我们使用NaiveWaiter的例子来说明,这是一个希望引入切面的目标类:
Java代码 复制代码
  1. package com.baobaotao;   
  2. public class NaiveWaiter implements Waiter {   
  3.     public void greetTo(String clientName) {   
  4.         System.out.println("NaiveWaiter:greet to "+clientName+"...");   
  5.     }   
  6.     public void serveTo(String clientName){   
  7.         System.out.println("NaiveWaiter:serving "+clientName+"...");   
  8.     }      
  9. }  


下面使用@AspectJ来定义一下切面:
Java代码 复制代码
  1. package com.baobaotao.aspectj.aspectj;   
  2. import org.aspectj.lang.annotation.Aspect;   
  3. import org.aspectj.lang.annotation.Before;   
  4. //通过该注解将PreGreetingAspect标识为一个切面   
  5. @Aspect    
  6. public class PreGreetingAspect{   
  7.     @Before("execution(* greetTo(..))"//<---定义切点和增强类型   
  8.     public void beforeGreeting(){ //<----增强的横切逻辑   
  9.         System.out.println("How are you");   
  10.     }   
  11. }  


然后启动@AspectJ的注解切面驱动就可以了!
Java代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"    
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6. <A href="http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" target=_blank>http://www.springframework.org/schema/beans/spring-beans-2.0.xsd</A>   
  7.                         http://www.springframework.org/schema/aop    
  8. <A href="http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" target=_blank>http://www.springframework.org/schema/aop/spring-aop-2.0.xsd</A>">   
  9.     <!--基于@AspectJ切面的驱动器-->   
  10.         <aop:aspectj-autoproxy />    
  11.     <bean id="waiter" class="com.baobaotao.NaiveWaiter" />   
  12.     <bean class="com.baobaotao.aspectj.example.PreGreetingAspect" />   
  13. </beans>  


四、关于Spring 2.1添加的新功能

1.原来引入一个外面属性配置文件需要使用以下的方式:
Java代码 复制代码
  1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   
  2.     <property name="locations">   
  3.        <list>   
  4.            <!--指定属性文件地址,可以在这里定义多个属性文件-->   
  5.           <value>classpath:com/baobaotao/place/car.properties</value>    
  6.        </list>   
  7.     </property>   
  8.     <property name="fileEncoding" value="utf-8"/>   
  9. </bean>   
  10.   
  11. <!--引用外部属性的值,对car属性进行配置-->   
  12. <bean id="car" class="com.baobaotao.place.Car">   
  13.     <property name="brand" value="${brand}" />   
  14.     <property name="maxSpeed" value="${maxSpeed}" />   
  15.     <property name="price" value="${price}" />   
  16. </bean>  


使用Spring 2.1后,你只需要象下面这样配置就可以了:
Java代码 复制代码
  1. <context:property-placeholder location=" classpath:com/baobaotao/place/car.properties "/>  


3.注解驱动的Bean注入

大家看看这段E文就OK了,
<context:component-scan>: scans classpath using one or more "include"/"exclude" filters, and automatically registers beans
In essence, this is a third way to define beans (1: classic xml, 2:javaconfig (code), 3:<context:component-scan>, matching on annotations or types)
Default naming strategy is based on short classname of discovered bean
@Component and @Repository annotations, when used, can optionally specify a bean name to use
For filter type "annotation", the value of "expression" attribute should resolve to a Java annotation type
For filter type "assignable", the value of "expression" attribute should resolve to a Java type
For filter type "aspectj", the value of "expression" should be an "type expression" (in Pointcut language, perhaps it could be injected?)
Relevant documentation can be found in preliminary spring 2.1 manual, sections 3.10 and 3.11
In addition, this last JIRA comment for http://www.jetbrains.net/jira/browse/IDEADEV-16886#action_163502 contains two links to articles showing example usage of <context:component-scan>
<context:annotation-config> (described in 3.10 in spring 2.1 manual linked above): allows autowiring to be defined using @Resource or @Autowired annotations.


五、关于Spring 2.5添加的新功能
Spring 2.5继续对context命名空间进行了扩充,添加了好用而强大的context:load-time-weaver,可以让我们更方便地应用AspectJ。大家可以看TSS上的这篇文章,它全面讲解了Spring 2.5的新特性。
http://www.theserverside.com/tt/articles/article.tss?l=IntrotoSpring25

注:以上大部分代码来直接引用自 《精通Spring 2.x--企业应用开发精解》
文章来源于:javaeye的stamen
posted on 2008-03-14 09:44 芦苇 阅读(973) 评论(1)  编辑  收藏 所属分类: Spring

Feedback

# re: 谈谈Spring 2.x中简化配置的问题 2009-09-25 14:07 hfliyy
分多个模块,多写几个例子就好了。  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: