这篇将介绍AspectJ的第二种开发方法:基于注解的声明式-AspectJ。
与基于代理类的AOP实现相比,基于XML的声明式AspectJ要便捷的多,但是它也存在一些缺点,那就是在Spring文件中配置大量的代码信息。为了介绍这个问题,AspectJ框架为AOP的实现提供了一套注解,用以取代Spring配置文件中为实现AOP功能所配置的臃肿代码。
同第一种配置方法,我们也创建了一个接口,并创建一个类实现这个接口。
package com.itheima.jdk;
public interface UserDao {
public void addUser();
public void deleteUser();
}
package com.itheima.jdk;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("添加用户");
}
@Override
public void deleteUser() {
// TODO Auto-generated method stub
System.out.println("删除用户");
}
}
然后创建一个MyAspect,与第一种方法相比。注解方式在每一个方法前面添加注解
package com.itheima.aspectj.annotation;
import org.aopalliance.intercept.Joinpoint;
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.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class MyAspect {
//定义切入点表达式
@Pointcut("execution(* com.itheima.jdk.*.*(..))")
//使用一个返回值为为void、方法体为空的方法来命名切入点
private void myPointCut(){}
@Before("myPointCut()")
public void myBefore(JoinPoint joinPoint)
{
System.out.print("注解声明式的AspectJ\n"+"author:Black_YeJing\n");
System.out.println("");
System.out.println("前置通知:模拟执行权限检查...,");
System.out.println("目标类是:"+joinPoint.getTarget());
System.out.println(",被植入增强处理的目标方法为:"+joinPoint.getSignature().getName());
}
//后置通知
@AfterReturning("myPointCut()")
public void myAfterReturning(JoinPoint joinPoint)
{
System.out.println("后置通知:模拟记录日志....,");
System.out.println("被植入增强处理的目标方法为:"+joinPoint.getSignature().getName());
}
//环绕通知
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable
{
//开始
System.out.println("环绕开始:执行目标方法之前,模拟开启事务");
//执行当前目标方法
Object obj=proceedingJoinPoint.proceed();
//结束
System.out.println("环绕结束:执行目标方法之后,模拟关闭事务....");
return obj;
}
//异常通知
@AfterThrowing(value="myPointCut()",throwing="e")
public void myAfterThrowing(JoinPoint joinPoint,Throwable e)
{
System.out.println("异常通知:"+"出错了"+e.getMessage());
}
//最终通知
@After("myPointCut()")
public void myAfter()
{
System.out.println("最终通知:模拟方法结束后的释放资源");
}
}
接着写配置文件,相比于第一种的开发模式,第二种注解的方式便简便了许多。
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 指定需要扫描的包,使注解生效 -->
<context:component-scan base-package="com.itheima" />
<!-- 启动基于注解的声明式AspectJ支持 -->
<aop:aspectj-autoproxy />
</beans>
最后写一个测试类TestAnnotationAspectj.java
package com.itheima.aspectj.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itheima.jdk.UserDao;
public class TestAnnotationAspectj {
public static void main(String[] args) {
String xmlPath="com/itheima/aspectj/annotation/applicationContext.xml";
ApplicationContext context=new ClassPathXmlApplicationContext(xmlPath);
UserDao userDao=(UserDao)context.getBean("userDao");
userDao.addUser();
userDao.deleteUser();
}
}
运行结果:
总结:可以看出,基于注解的方式与基于XML的方式的执行结果相同,只是在目标方法前后同志的执行顺序发生了变化。相对来说,使用注解的方式更加简单、方便,所以在实际开发中推荐使用注解的方式进行AOP开发。
ps:第一种方式的链接:https://blog.csdn.net/black_yejing/article/details/79667290