`
starbhhc
  • 浏览: 638583 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

Spring 2.x AOP 配置方式整理

 
阅读更多

让我们假定你所有的服务层类定义在以 'x.y.service' 为根的包内。 为了让service包(或子包)下所有名字以'Service' 结尾的类的对象拥有默认的事务语义,你可以做如下的配置:

Xml代码  收藏代码
  1. <aop:config>  
  2.   
  3.   <aop:pointcut id="serviceOperation"  
  4.         expression="execution(* x.y.service..*Service.*(..))"/>  
  5.   
  6.   <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/>  
  7.   
  8. </aop:config>  
  9.   
  10. <!-- these two beans will be transactional... -->  
  11. <bean id="fooService" class="x.y.service.DefaultFooService"/>  
  12. <bean id="barService" class="x.y.service.extras.SimpleBarService"/>  
  13.   
  14. <!-- ... and these two beans won't -->  
  15. <bean id="anotherService" class="org.xyz.SomeService"/> <!-- (not in the right package) -->  
  16. <bean id="barManager" class="x.y.service.SimpleBarManager"/> <!-- (doesn't end in 'Service') -->  
  17.   
  18. <tx:advice id="txAdvice">  
  19.   <tx:attributes>  
  20.     <tx:method name="get*" read-only="true"/>  
  21.     <tx:method name="*"/>  
  22.   </tx:attributes>  
  23. </tx:advice>  

 

下面的配置示例演示了两个拥有完全不同的事务配置的bean。

Xml代码  收藏代码
  1. <aop:config>  
  2.   
  3.   <aop:pointcut id="defaultServiceOperation"  
  4.         expression="execution(* x.y.service.*Service.*(..))"/>  
  5.   
  6.   <aop:pointcut id="noTxServiceOperation"  
  7.         expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/>  
  8.   
  9.   <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/>  
  10.   
  11.   <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/>  
  12.   
  13. </aop:config>  
  14.   
  15. <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) -->  
  16. <bean id="fooService" class="x.y.service.DefaultFooService"/>  
  17.   
  18. <!-- this bean will also be transactional, but with totally different transactional settings -->  
  19. <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/>  
  20.   
  21. <tx:advice id="defaultTxAdvice">  
  22.   <tx:attributes>  
  23.     <tx:method name="get*" read-only="true"/>  
  24.     <tx:method name="*"/>  
  25.   </tx:attributes>  
  26. </tx:advice>  
  27.   
  28. <tx:advice id="noTxAdvice">  
  29.   <tx:attributes>  
  30.     <tx:method name="*" propagation="NEVER"/>  
  31.   </tx:attributes>  
  32. </tx:advice>  

 

在service接口所有的方法上执行的一个业务service方法。这里的定义假设所有的接口都被
放置在service包内,它们的实现被放置在service包的子包内。
如果你按照功能对接口进行分组(例如:包com.xyz.someapp.abc.service,com.xyz.def.service),
则这种情况下这个切点表达式应该是:"execution(* com.xyz.someapp..service.*.*(..))"

Java代码  收藏代码
  1. /** 
  2.  * A business service is the execution of any method defined on a service 
  3.  * interface. This definition assumes that interfaces are placed in the 
  4.  * "service" package, and that implementation types are in sub-packages. 
  5.  *  
  6.  * If you group service interfaces by functional area (for example,  
  7.  * in packages com.xyz.someapp.abc.service and com.xyz.def.service) then 
  8.  * the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))" 
  9.  * could be used instead. 
  10.  * 
  11.  * Alternatively, you can write the expression using the 'bean' 
  12.  * PCD, like so "bean(*Service)". (This assumes that you have 
  13.  * named your Spring service beans in a consistent fashion.) 
  14. */  
  15. @Pointcut"execution(* com.xyz.someapp.service.*.*(..))")  
  16. public void businessService() {}  

 

在dao接口上定义的所有方法内执行一个数据访问操作。这个定义假设所有的dao接口定义
在dao包内,实现被放置在了子包内。

Java代码  收藏代码
  1. /** 
  2.  * A data access operation is the execution of any method defined on a  
  3.  * dao interface. This definition assumes that interfaces are placed in the 
  4.  * "dao" package, and that implementation types are in sub-packages. 
  5. */  
  6. @Pointcut"execution(* com.xyz.someapp.dao.*.*(..))")  
  7. public void dataAccessOperation() {}  

 

任何一个名字以“set”开始的方法的执行:

Xml代码  收藏代码
  1. execution(* set*(..))  

 

AccountService 接口定义的任意方法的执行:

Xml代码  收藏代码
  1. execution(* com.xyz.service.AccountService.*(..))  

 

在service包中定义的任意方法的执行:

Xml代码  收藏代码
  1. execution(* com.xyz.service.*.*(..))  

 

在service包或其子包中定义的任意方法的执行:

Xml代码  收藏代码
  1. execution(* com.xyz.service..*.*(..))  

 

 

其他的例子: 
--------------------------------------------------------------------------------
 

两个数据源,两个数据库事务拦截器,两个数据库事物切点。
execution组合表达式表述数据库事务切点:
大部分service类的方法使用数据源txManager-datasourceone,对应事物切点txPointcut-datasourceone,事物拦截器txAdvice-datasourceone;
service层PublishService类的几个方法使用数据源txManager-datasourcetwo,对应事物切点txPointcut-datasourcetwo,事物拦截器txAdvice-datasourcetwo;
一个自定义方法拦截器RuntimeLogInterceptor(拦截每个方法,并记录每个方法的执行日志),拦截切点runtimeLogInterceptorPoint;

Xml代码  收藏代码
  1. <!-- 数据源1事务管理器 -->  
  2. <bean id="txManager-datasourceone" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  3.   <property name="dataSource" ref="DataSource"/>  
  4. </bean>  
  5.   
  6. <!-- 数据源2事务管理器 -->  
  7. <bean id="txManager-datasourcetwo" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  8.   <property name="dataSource" ref="srcDataSource"/>  
  9. </bean>  
  10.       
  11. <!-- 数据源1事务拦截器 -->  
  12. <tx:advice id="txAdvice-datasourceone" transaction-manager="txManager-datasourceone" >  
  13.   <tx:attributes>  
  14.     <tx:method name="get*" read-only="true"/>  
  15.     <tx:method name="*"/>  
  16.   </tx:attributes>  
  17. </tx:advice>  
  18.   
  19. <!-- 数据源2事务拦截器 -->  
  20. <tx:advice id="txAdvice-datasourcetwo" transaction-manager="txManager-datasourcetwo" >  
  21.   <tx:attributes>  
  22.     <tx:method name="get*" read-only="true"/>  
  23.     <tx:method name="*"/>  
  24.   </tx:attributes>  
  25. </tx:advice>  
  26.   
  27. <!-- aop配置 强制使用cglib代理 -->  
  28. <aop:config proxy-target-class="true">  
  29.   
  30.   <!-- datasourceone 数据库事务管理切点   
  31.        包含的方法:service包(或子包)下所有名字以 'Service' 结尾的类内所有的方法。  
  32.        不包含的方法:x.y.service包下PublishService类的getResCategory(..)方法,  
  33.        getEditorResList(..)方法,updateResbackrmd(..)方法  
  34.   -->  
  35.   <aop:pointcut id="txPointcut-datasourceone"   
  36.     expression="execution(* x.y.service..*Service.*(..))   
  37.       and !execution(* x.y.service.PublishService.getResCategory(..))   
  38.       and !execution(* x.y.service.PublishService.getEditorResList(..))   
  39.       and !execution(* x.y.service.PublishService.updateResbackrmd(..))"/>  
  40.   
  41.   <!-- datasourcetwo 数据库事务管理切点   
  42.        包含的方法:x.y.service包PublishService类的getResCategory(..)方法,  
  43.        getEditorResList(..)方法,updateResbackrmd(..)方法。  
  44.   -->                                                  
  45.   <aop:pointcut id="txPointcut-datasourcetwo"   
  46.     expression="execution(* x.y.service.PublishService.getResCategory(..))   
  47.       or execution(* x.y.service.PublishService.getEditorResList(..))   
  48.       or execution(* x.y.service.PublishService.updateResbackrmd(..))"/>  
  49.   
  50.   <!-- 运行日志拦截点   
  51.        包含的方法:service包(或子包)下所有名字以 'Service' 结尾的类内所有的方法。  
  52.        不包含的方法:x.y.service包RuntimeLogService类createRuntimeLogBeforeRequest(..)方法,  
  53.     getRuntimeLog(..)方法,setRuntimeLog(..)方法,completeRuntimeLogAfterRequest(..)方法。  
  54.   -->  
  55.   <aop:pointcut id="runtimeLogInterceptorPoint"   
  56.     expression="execution(* x.y.service..*Service.*(..))   
  57.       and !execution(* x.y.service.RuntimeLogService.createRuntimeLogBeforeRequest(..))   
  58.       and !execution(* x.y.service.RuntimeLogService.getRuntimeLog(..))   
  59.       and !execution(* x.y.service.RuntimeLogService.setRuntimeLog(..))   
  60.       and !execution(* x.y.service.RuntimeLogService.completeRuntimeLogAfterRequest(..))"/>  
  61.               
  62.   <!-- 运行日志拦截 -->  
  63.   <aop:advisor advice-ref="RuntimeLogInterceptor" pointcut-ref="runtimeLogInterceptorPoint"/>  
  64.   <!-- datasourceone,datasourcetwo数据库事务拦截 -->  
  65.   <aop:advisor advice-ref="txAdvice-datasourceone" pointcut-ref="txPointcut-datasourceone"/>  
  66.   <aop:advisor advice-ref="txAdvice-datasourcetwo" pointcut-ref="txPointcut-datasourcetwo"/>  
  67.   
  68. </aop:config>  

 

总结一下: 
--------------------------------------------------------------------------------

 

1,pointcut既可以定义在一个接口上面(表示实现该接口的类方法将被拦截),同时也可以定义在一个类上面(无接口的情
   况,需要强制使用cglib)。在接口上面定义pointcut时无需关心接口实现类的具体位置,只需要定义被拦截的接口及方
   法位置。

2,常见的情况:
x.y.service..*Service.*(..)
x.y.service —— 包“x.y.service”
x.y.service.. —— 包“x.y.service”及其子包例如:“x.y.service.abc”,“x.y.service.def”,“x.y.service.ghi”,“x.y.service.jkl”。。。
*Service —— 定义接口(或没有实现接口的类,需要使用cglib代理)表达式;所有以Service结尾的类或接口,注意不是所有以Service结尾的包名。
*(..) —— 定义方法名,方法参数表达式;任意方法的名称,任意方法参数。

com.xyz.service.*.*(..)
com.xyz.service —— 包“com.xyz.service”
*.*(..) —— 任意接口(或没有实现接口的类,需要使用cglib代理),任意方法,任意参数
在service包下定义的任意方法的执行。

com.xyz.service..*.*(..)
com.xyz.service —— 包“com.xyz.service”
com.xyz.service.. ——包“com.xyz.service”及其子包
*.*(..) —— 任意接口(或没有实现接口的类,需要使用cglib代理),任意方法,任意参数

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics