使用@AspectJ注解
来源:互联网 发布:协同过滤算法代码 编辑:程序博客网 时间:2024/06/02 04:13
- package com.tgb.aop;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.DeclareParents;
- import org.aspectj.lang.annotation.Pointcut;
- /**
- * 测试after,before,around,throwing,returning Advice.
- * @author Admin
- *
- */
- @Aspect
- public class AspceJAdvice {
- /**
- * Pointcut
- * 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数
- * 该方法就是一个标识,不进行调用
- */
- @Pointcut("execution(* find*(..))")
- private void aspectjMethod(){};
- /**
- * Before
- * 在核心业务执行前执行,不能阻止核心业务的调用。
- * @param joinPoint
- */
- @Before("aspectjMethod()")
- public void beforeAdvice(JoinPoint joinPoint) {
- System.out.println("-----beforeAdvice().invoke-----");
- System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of beforeAdvice()------");
- }
- /**
- * After
- * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
- * @param joinPoint
- */
- @After(value = "aspectjMethod()")
- public void afterAdvice(JoinPoint joinPoint) {
- System.out.println("-----afterAdvice().invoke-----");
- System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of afterAdvice()------");
- }
- /**
- * Around
- * 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
- *
- * 注意:当核心业务抛异常后,立即退出,转向AfterAdvice
- * 执行完AfterAdvice,再转到ThrowingAdvice
- * @param pjp
- * @return
- * @throws Throwable
- */
- @Around(value = "aspectjMethod()")
- public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("-----aroundAdvice().invoke-----");
- System.out.println(" 此处可以做类似于Before Advice的事情");
- //调用核心逻辑
- Object retVal = pjp.proceed();
- System.out.println(" 此处可以做类似于After Advice的事情");
- System.out.println("-----End of aroundAdvice()------");
- return retVal;
- }
- /**
- * AfterReturning
- * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
- * @param joinPoint
- */
- @AfterReturning(value = "aspectjMethod()", returning = "retVal")
- public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {
- System.out.println("-----afterReturningAdvice().invoke-----");
- System.out.println("Return Value: " + retVal);
- System.out.println(" 此处可以对返回值做进一步处理");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of afterReturningAdvice()------");
- }
- /**
- * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
- *
- * 注意:执行顺序在Around Advice之后
- * @param joinPoint
- * @param ex
- */
- @AfterThrowing(value = "aspectjMethod()", throwing = "ex")
- public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
- System.out.println("-----afterThrowingAdvice().invoke-----");
- System.out.println(" 错误信息:"+ex.getMessage());
- System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of afterThrowingAdvice()------");
- }
- }
application-config.xml中,只需要配置业务逻辑bean和Aspect bean,并启用Aspect注解即可:
- <?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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 启用AspectJ对Annotation的支持 -->
- <aop:aspectj-autoproxy/>
- <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>
- <bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>
- </beans>
结果如图:
通过测试的发现AroundAdvice、BeforeAdvice、AfterAdvice、ReturningAdvice的执行顺序是根据注解的顺序而定的。但是有时候修改了顺序,结果却没有变化,可能是缓存的缘故。前几天我也遇到了这样的问题,不过今天再测试了一下,发现执行顺序又跟注解的顺序一致了。
xml 和 Annotation 注解都可以作为配置项,对Spring AoP进行配置管理,那么它们各自都有什么优缺点呢?
首先说说 xml 。目前 web 应用中几乎都使用 xml 作为配置项,例如我们常用的框架 Struts、Spring、Hibernate 等等都采用 xml 作为配置。xml 之所以这么流行,是因为它的很多优点是其它技术的配置所无法替代的:
- xml 作为可扩展标记语言最大的优势在于开发者能够为软件量身定制适用的标记,使代码更加通俗易懂。
- 利用 xml 配置能使软件更具扩展性。例如 Spring 将 class 间的依赖配置在 xml 中,最大限度地提升应用的可扩展性。
- 具有成熟的验证机制确保程序正确性。利用 Schema 或 DTD 可以对 xml 的正确性进行验证,避免了非法的配置导致应用程序出错。
- 修改配置而无需变动现有程序。
- 需要解析工具或类库的支持。
- 解析 xml 势必会影响应用程序性能,占用系统资源。
- 配置文件过多导致管理变得困难。
- 编译期无法对其配置项的正确性进行验证,或要查错只能在运行期。
- IDE 无法验证配置项的正确性无能为力。
- 查错变得困难。往往配置的一个手误导致莫名其妙的错误。
- 开发人员不得不同时维护代码和配置文件,开发效率变得低下。
- 配置项与代码间存在潜规则。改变了任何一方都有可能影响另外一方。
- 保存在 class 文件中,降低维护成本。
- 无需工具支持,无需解析。
- 编译期即可验证正确性,查错变得容易。
- 提升开发效率。
- 若要对配置项进行修改,不得不修改 Java 文件,重新编译打包应用。
- 配置项编码在 Java 文件中,可扩展性差。
- <aop:config>
- <aop:aspect id="testAspect" ref="authAspect">
- <aop:pointcut id="businessService"
- expression="execution(* com.ncl.cf.ui.*.*.*(..)) " />
- <aop:before pointcut-ref="businessService" method="doBefore" />
- <aop:after pointcut-ref="businessService" method="doAfter" />
- <aop:around pointcut-ref="businessService" method="doAround" />
- <aop:after-throwing pointcut-ref="businessService"
- method="doThrowing" throwing="ex" />
- </aop:aspect>
- </aop:config>
import EDU.oswego.cs.dl.util.concurrent.*;
public aspect Lock {
......
ReentrantWriterPreferenceReadWriteLock rwl =
new ReentrantWriterPreferenceReadWriteLock();
public pointcut writeOperations():
execution(public boolean Worker.createData()) ||
execution(public boolean Worker.updateData()) ||
execution(public boolean AnotherWorker.updateData()) ;
before() : writeOperations() {
rwl.writeLock().acquire();//上锁 advice body
}
after() : writeOperations() {
rwl.writeLock().release(); //解锁 advice body
}
......
}
- public aspect PeopleAspect {
- pointcut setXXX():
- call(public void People.set*(*));
- before():setXXX(){
- System.out.println(">>> Log : modify people's attribute ...");
- }
- }
- 使用@AspectJ注解
- 使用@AspectJ注解
- SpringAop中使用@AspectJ注解
- AspectJ使用注解方式报错
- 使用AspectJ基于注解的Aop
- spring之基于aspectj注解aop使用
- Spring 之AOP 使用@AspectJ注解方式
- Spring AOP的使用 基于全注解AspectJ
- Spring AOP之AspectJ的注解方式使用
- Spring使用AspectJ进行AOP的开发:注解方式
- 使用AspectJ提供的注解方式实现aop
- Spring AspectJ注解
- 1--springBoot的注解使用,aspectj注解的使用,配置类的使用
- spring使用aspectj开发的增强类型注解和aspectj在spring中用到的切点函数
- AOP注解实现(AspectJ)
- javaEE之------Spring-----》 AspectJ注解
- AspectJ基于注解的实现
- Spring AOP+AspectJ注解实例
- VMware安装linux网络设置 桥接方式
- 第一篇java,实现加减乘除与平方运算
- 大华股份2013届校园招聘软件算法类试题D卷
- UVa12657 Boxes in a Line
- 事件的触发机制,定义,注册
- 使用@AspectJ注解
- 使用android快速开发框架afinal的FinalDb操作android数据库
- PHP 日期转换为时间戳 strtotime 与 mktime 函数
- Java中设计模式
- fastJSON的各种转换
- java 策略模式 模板方法 (Template method) 示例
- Linux下查看某个进程创建了多少线程
- MapReduce(十一): 详解Reduce任务运行
- Hibernate中get和load方法的区别