Spring 2.X 中AOP的使用浅析
2011-05-24 11:10
399 查看
Spring AOP 使用
和Spring1.X相比,Spring2.X使用AspectJ的语法来声明AOP,这使得它更“标准”,更灵活了。还是那句话,如果你不了解AspectJ并且打算使用Spring2.X的AspectJ式AOP,那就学学AspectJ吧,这方面的书还是很多了。Spring2.X下的切面有两种实现方式,一种是以Java文件定义切面(其只是普通的Java类),然后在配置文件中声明定义的切面;另一种是在Java类中引入和AOP相关的元数据(注释)。
先介绍第一种配置方式。需要指出的是,Spring2.X的beans名称空间和Spring1.X有所不同,它采用了Schema而不是DTD(也可采用DTD方式,具体的请参考文档)。还是引入毫无意义的日志切面,定义的切面类LogingAspect 如下:
public class LogingAspect { public void logMethod(JoinPoint jp) } |
< bean id = " logAspectTarget " class = " hibernatesample.service.util.LogingAspect " > </ bean > < aop:config > < aop:aspect id = " logAspect " ref = " logAspectTarget " > < aop:pointcut id = " businessService " expression = " execution(* hibernatesample.service.*.*(..)) " /> < aop:after pointcut - ref = " businessService " method = " logMethod " /> </ aop:aspect > </ aop:config > |
< tx:advice id = " txAdvice " transaction - manager = " transactionManager " > < tx:attributes > < tx:method name = " get* " read - only = " true " /> < tx:method name = " find* " read - only = " true " /> < tx:method name = " * " /> </ tx:attributes > </ tx:advice > < aop:config > < aop:pointcut id = " demoServiceMethods " expression = " execution(* hibernatesample.service.*.*(..)) " /> < aop:advisor advice - ref = " txAdvice " pointcut - ref = " demoServiceMethods " /> < aop:aspect id = " logAspect " ref = " logAspectTarget " > < aop:pointcut id = " businessService " expression = " execution(* hibernatesample.service.*.*(..)) " /> < aop:after pointcut - ref = " businessService " method = " logMethod " /> </ aop:aspect > </ aop:config > |
< bean id ="accountService" class ="hibernatesample.service.impl.AccountServiceImpl" > < property name ="accountDAO" ref ="accountDAO" ></ property > </ bean > |
@Aspect public class LogingAspect { @Pointcut( " execution(* hibernatesample.service.*.*(..)) " ) public void businessService(){} @After( " businessService() " ) public void logMethod(JoinPoint jp) } |
< aop:aspect id ="logAspect" ref ="logAspectTarget" > < aop:pointcut id ="businessService" expression ="execution(* hibernatesample.service.*.*(..))" /> < aop:after pointcut-ref ="businessService" method ="logMethod" /> </ aop:aspect > < bean id ="logAspectTarget" class ="hibernatesample.service.util.LogingAspect" ></ bean > |
<aop:aspectj-autoproxy/>
如果觉得事务的配置没有使用注释更简洁(我倒不会有这样的想法,毕竟在配置文件中声明的事务只是那么固定的几段,除非作用于类上的事务逻辑上很复杂),也可以使用Spring提供的事务注释作用于类文件上,这可是更细粒度的事务声明了。
坦率的说,由于时间有限,该文写的比较粗糙。对于Spring AOP有兴趣并有疑问的朋友,可以参考Spring的文档,它的文档做的还是不错的。
相关文章推荐
- jdk动态代理与cglib代理、spring Aop代理原理-代理使用浅析
- 浅析Spring.net 中的Aop使用
- spring使用AOP技术对DAO层操作进行增强功能
- 8 -- 深入使用Spring -- 4...3 AOP的基本概念
- 浅析Spring AOP(一)——代理模式
- 详解使用Spring Boot的AOP处理自定义注解
- Spring AOP使用整理:使用@AspectJ风格的切面声明
- 46. Spring Boot中使用AOP统一处理Web请求日志【从零开始学Spring Boot】
- springboot【19】日志管理之使用AOP统一处理Web请求日志
- 使用Spring进行面向切面(AOP)编程
- ITCAST视频-Spring学习笔记(使用CGLIB实现AOP功能与AOP概念解释)
- Spring学习笔记(14)----使用CGLIB实现AOP功能
- spring使用aop时需要设置proxy-target-class="true" 否则无法依赖注入
- 使用spring的aop模拟事物
- spring AOP拦截器的配置使用
- Spring AOP之AspectJ的注解方式使用
- spring-boot使用AOP统一处理日志
- 使用动态代理简易模拟spring aop
- 使用Spring进行切面(AOP)编程
- spring中自定义Event事件的使用和浅析