锁定老帖子 主题:Spring 2.x AOP 配置方式整理
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-01-26
spring官方手册给出的配置实例。 --------------------------------------------------------------------------------
让我们假定你所有的服务层类定义在以 <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* x.y.service..*Service.*(..))"/> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice"/> </aop:config> <!-- these two beans will be transactional... --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <bean id="barService" class="x.y.service.extras.SimpleBarService"/> <!-- ... and these two beans won't --> <bean id="anotherService" class="org.xyz.SomeService"/> <!-- (not in the right package) --> <bean id="barManager" class="x.y.service.SimpleBarManager"/> <!-- (doesn't end in 'Service') --> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> 下面的配置示例演示了两个拥有完全不同的事务配置的bean。 <aop:config> <aop:pointcut id="defaultServiceOperation" expression="execution(* x.y.service.*Service.*(..))"/> <aop:pointcut id="noTxServiceOperation" expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))"/> <aop:advisor pointcut-ref="defaultServiceOperation" advice-ref="defaultTxAdvice"/> <aop:advisor pointcut-ref="noTxServiceOperation" advice-ref="noTxAdvice"/> </aop:config> <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) --> <bean id="fooService" class="x.y.service.DefaultFooService"/> <!-- this bean will also be transactional, but with totally different transactional settings --> <bean id="anotherFooService" class="x.y.service.ddl.DefaultDdlManager"/> <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <tx:advice id="noTxAdvice"> <tx:attributes> <tx:method name="*" propagation="NEVER"/> </tx:attributes> </tx:advice> 在service接口所有的方法上执行的一个业务service方法。这里的定义假设所有的接口都被 /** * A business service is the execution of any method defined on a service * interface. This definition assumes that interfaces are placed in the * "service" package, and that implementation types are in sub-packages. * * If you group service interfaces by functional area (for example, * in packages com.xyz.someapp.abc.service and com.xyz.def.service) then * the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))" * could be used instead. * * Alternatively, you can write the expression using the 'bean' * PCD, like so "bean(*Service)". (This assumes that you have * named your Spring service beans in a consistent fashion.) */ @Pointcut("execution(* com.xyz.someapp.service.*.*(..))") public void businessService() {}
在dao接口上定义的所有方法内执行一个数据访问操作。这个定义假设所有的dao接口定义 /** * A data access operation is the execution of any method defined on a * dao interface. This definition assumes that interfaces are placed in the * "dao" package, and that implementation types are in sub-packages. */ @Pointcut("execution(* com.xyz.someapp.dao.*.*(..))") public void dataAccessOperation() {}
任何一个名字以“set”开始的方法的执行: execution(* set*(..))
execution(* com.xyz.service.AccountService.*(..))
在service包中定义的任意方法的执行: execution(* com.xyz.service.*.*(..))
在service包或其子包中定义的任意方法的执行: execution(* com.xyz.service..*.*(..))
其他的例子:
<!-- 数据源1事务管理器 --> <bean id="txManager-datasourceone" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="DataSource"/> </bean> <!-- 数据源2事务管理器 --> <bean id="txManager-datasourcetwo" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="srcDataSource"/> </bean> <!-- 数据源1事务拦截器 --> <tx:advice id="txAdvice-datasourceone" transaction-manager="txManager-datasourceone" > <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 数据源2事务拦截器 --> <tx:advice id="txAdvice-datasourcetwo" transaction-manager="txManager-datasourcetwo" > <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- aop配置 强制使用cglib代理 --> <aop:config proxy-target-class="true"> <!-- datasourceone 数据库事务管理切点 包含的方法:service包(或子包)下所有名字以 'Service' 结尾的类内所有的方法。 不包含的方法:x.y.service包下PublishService类的getResCategory(..)方法, getEditorResList(..)方法,updateResbackrmd(..)方法 --> <aop:pointcut id="txPointcut-datasourceone" expression="execution(* x.y.service..*Service.*(..)) and !execution(* x.y.service.PublishService.getResCategory(..)) and !execution(* x.y.service.PublishService.getEditorResList(..)) and !execution(* x.y.service.PublishService.updateResbackrmd(..))"/> <!-- datasourcetwo 数据库事务管理切点 包含的方法:x.y.service包PublishService类的getResCategory(..)方法, getEditorResList(..)方法,updateResbackrmd(..)方法。 --> <aop:pointcut id="txPointcut-datasourcetwo" expression="execution(* x.y.service.PublishService.getResCategory(..)) or execution(* x.y.service.PublishService.getEditorResList(..)) or execution(* x.y.service.PublishService.updateResbackrmd(..))"/> <!-- 运行日志拦截点 包含的方法:service包(或子包)下所有名字以 'Service' 结尾的类内所有的方法。 不包含的方法:x.y.service包RuntimeLogService类createRuntimeLogBeforeRequest(..)方法, getRuntimeLog(..)方法,setRuntimeLog(..)方法,completeRuntimeLogAfterRequest(..)方法。 --> <aop:pointcut id="runtimeLogInterceptorPoint" expression="execution(* x.y.service..*Service.*(..)) and !execution(* x.y.service.RuntimeLogService.createRuntimeLogBeforeRequest(..)) and !execution(* x.y.service.RuntimeLogService.getRuntimeLog(..)) and !execution(* x.y.service.RuntimeLogService.setRuntimeLog(..)) and !execution(* x.y.service.RuntimeLogService.completeRuntimeLogAfterRequest(..))"/> <!-- 运行日志拦截 --> <aop:advisor advice-ref="RuntimeLogInterceptor" pointcut-ref="runtimeLogInterceptorPoint"/> <!-- datasourceone,datasourcetwo数据库事务拦截 --> <aop:advisor advice-ref="txAdvice-datasourceone" pointcut-ref="txPointcut-datasourceone"/> <aop:advisor advice-ref="txAdvice-datasourcetwo" pointcut-ref="txPointcut-datasourcetwo"/> </aop:config> 总结一下:
1,pointcut既可以定义在一个接口上面(表示实现该接口的类方法将被拦截),同时也可以定义在一个类上面(无接口的情 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-01-27
正学习Spring中,谢谢LZ的分享
|
|
返回顶楼 | |
浏览 6250 次