Spring4入门之第四章AOP的注解方式
Spring的基于ApsectJ的注解的AOP开发
-
将Spring中对于AOP的xml开发方式转化成注解的开发方式
-
编写目标类并进行相应的配置
Order.java
public class Order { public void save() { System.out.println("保存商品...."); } public void update() { System.out.println("更新商品...."); } public void find() { System.out.println("查询商品...."); // int count = 1 / 0; } public String delete() { System.out.println("删除商品...."); return "afterReturning"; } }
-
编写切面类并进行配置(对目标类中的save方法进行增强)
MyAspectAnno.java
@Aspect public class MyAspectAnno { /* * 在保存之前,进行前置增强 */ @Before(value = "execution(* com.syj.Spring.aopAonotation.Order.save(..))") public void before() { System.out.println("权限校验==========="); } }
-
将目标类和切面类都交给Spring管理(创建配置文件并添加配置)
要想使用注解对AOP进行开发,我们需要再配置文件中添加<aop:aspectj-autoproxy/>
applicationContextAOP.xml
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 在Spring中开启的AOP注解开发 --> <aop:aspectj-autoproxy/> <!-- 将Order交给 Spring管理 --> <bean id="order" class="com.syj.Spring.aopAonotation.Order"> </bean> <!-- 将Aspecti切面类交给Spring管理 --> <bean id="aspect" class="com.syj.Spring.aopAonotation.MyAspectAnno" > </bean> </beans>
-
在开启了AOP注解开发的配置之后,我们需要声明切面类(Aspect)和通知(Advice)
使用到的注解分别是:@Aspect和AOP的通知类型注解
-
在AOP的通知类型的注解上面使用Pointcut表达式
Spring注解的AOP通知类型
在上面的AOP的开发的环境下
-
@Before:前置通知
-
在save方法执行之前,进行权限的校验。
-
在切面类为切入点添加Advice(通知)类型的注解。
/* * 在保存之前,进行前置增强 */ @Before(value = "execution(* com.syj.Spring.aopAonotation.Order.save(..))") public void before() { System.out.println("权限校验==========="); }
-
-
@AfterReturning:后置通知(可以获取返回值)
-
在删除方法执行之后,进行日志的打印。
-
后置通知的注解中有一个属性returning,可以获取返回值类型为Object
-
在切面类为切入点添加Advice(通知)类型的注解。
/* * 在删除方法之后进行后置增强(有返回值) */ @AfterReturning(value = "execution(* com.syj.Spring.aopAonotation.Order.delete(..))", returning = "result") public void afterReturning(Object result) { System.out.println("后置通知。。。。。。" + result; }
-
-
@Around:环绕通知
-
在更新方法的前后进行性能的监控。
-
joinPoint.proceed();就相当于执行目标类中的目标方法。
-
在切面类为切入点添加Advice(通知)类型的注解。
/* * 在更新时,进行环绕通知 */ @Around(value = "execution(* com.syj.Spring.aopAonotation.Order.update(..)))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕前==========="); Object obj = joinPoint.proceed(); System.out.println("环绕后==========="); return obj; }
-
-
@AfterReturning:异常抛出通知
-
在查询方法上,制造一个异常,通过AfterReturning对异常进行捕获
-
在切面类为切入点添加Advice(通知)类型的注解。
/* * 在查询。进行异常抛出通知 */ @AfterThrowing(value = "execution(* com.syj.Spring.aopAonotation.Order.find(..))", throwing = "ex") public void afterThrowing(Throwable ex) { System.out.println("异常抛出的通知==============" + ex.getMessage()); }
-
-
@After最终通知
-
在异常抛出通知的基础上进行最终通知的测试
-
在切面类为切入点添加Advice(通知)类型的注解。
/* * 最终通知 */ @After(value = "execution(* com.syj.Spring.aopAonotation.Order.find(..))") public void after() { System.out.println("最终通知============="); }
-
Spring注解的AOP的切入点的配置
-
为了方法后期的以使用配置对切入点进行引入
-
使用的注解是:@Pointcut里面的一个属性值value编写Pointcut表达式
-
将上面的改变成切入点引入的方式
@Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.save(..))") private void pointcut1() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.delete(..))") private void pointcut2() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.update(..))") private void pointcut3() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.find(..))") private void pointcut4() { }
-
完整的注解的切面类
/** * 切面类:注解的切面类 * * @author SYJ * */ @Aspect public class MyAspectAnno { /* * 在保存之前,进行前置增强 */ @Before(value = "MyAspectAnno.pointcut1()") public void before() { System.out.println("权限校验==========="); } /* * 在删除方法之后进行后置增强(有返回值) */ @AfterReturning(value = "MyAspectAnno.pointcut2()", returning = "result") public void afterReturning(Object result) { System.out.println("后置通知。。。。。。" + result); } /* * 在更新时,进行环绕通知 */ @Around(value = "MyAspectAnno.pointcut3()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("环绕前==========="); Object obj = joinPoint.proceed(); System.out.println("环绕后==========="); return obj; } /* * 在查询。进行异常抛出通知 */ @AfterThrowing(value = "MyAspectAnno.pointcut4()", throwing = "ex") public void afterThrowing(Throwable ex) { System.out.println("异常抛出的通知==============" + ex.getMessage()); } /** * 最终通知 */ @After(value = "MyAspectAnno.pointcut4()") public void after() { System.out.println("最终通知============="); } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.save(..))") private void pointcut1() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.delete(..))") private void pointcut2() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.update(..))") private void pointcut3() { } @Pointcut(value = "execution(* com.syj.Spring.aopAonotation.Order.find(..))") private void pointcut4() { } }