版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/82843674
AspectJ是一个基于java语言的AOP框架,他提供了强大的AOP功能,在Spring框架中建议使用AspectJ来开发AOP。
AspectJ实现AOP有两种方式,一、基于xml的声明式AspectJ,二、基于注解的声明式AspectJ
今天我就先来学习基于xml的声明式AspectJ
(!!!!!下面代码涉及前几天所写的UserDaoImpl,需要查看的点击这里)
1、首先来需要注意必须导入AspectJ相关的jar包。
aspectjweaver-1.8.10.jar
spring-aspects-4.3.6.RELEASE.jar
2、在chapter03的src目录下创建一个com.itheima.aspectj.xml包,并在包中创建切面类MyAspect,并在类中定义不同的通知,
代码如下面所示:
package com.itheima.aspectj.xml;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import jdk.nashorn.internal.runtime.Context.ThrowErrorManager;
public class MyAspect {
//前置通知
private void myBefore(JoinPoint joinPoint) {
System.out.println("前置通知:模拟执行权限检查,,,");
System.out.println("目标类是:"+joinPoint.getTarget());
System.out.println(",被植入增强的目标方法是:"+joinPoint.getSignature().getName());
}
//后置通知
private void myAfterReturning(JoinPoint joinPoint) {
System.out.println("后置通知:模拟记录日志,,,");
System.out.println(",被植入增强的目标方法是:"+joinPoint.getSignature().getName());
}
//环绕通知
private Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {
System.out.println("环绕开始:模拟开启事务,,,");
Object obj = proceedingJoinPoint.proceed();
System.out.println("环绕结束:模拟关闭事务,,,");
return obj;
}
//异常通知
private void myAfterThrowing(JoinPoint joinPoint,Throwable e) {
System.out.println("异常通知:不好了,出错了,,,"+e.getMessage());
}
//最终通知
private void myAfter() {
System.out.println("最终通知:模拟结束后释放资源,,,");
}
}
3、在com.itheima.aspectj.xml包中创建配置文件applicationContext.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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 1.定义目标类 -->
<bean id="userDao" class="com.itheima.jdk.UserDaoImpl"></bean>
<!-- 2.定义切面 -->
<bean id="myAspect" class="com.itheima.aspectj.xml.MyAspect" />
<aop:config >
<!-- 3.配置切面 -->
<aop:aspect id="aspect" ref="myAspect">
<!-- 3.1配置切入点 -->
<aop:pointcut expression="execution(* com.itheima.jdk.*.*(..))" id="myPointCut"/>
<!-- 3.2配置通知 -->
<!-- 3.2.1前置通知 -->
<aop:before method="myBefore" pointcut-ref="myPointCut"/>
<!-- 3.2.2后置通知 -->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/>
<!-- 3.2.3环绕通知 -->
<aop:around method="myAround" pointcut-ref="myPointCut"/>
<!-- 3.2.4抛出通知 -->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/>
<!-- 3.2.5最终通知 -->
<aop:after method="After" pointcut-ref="myPointCut"/>
</aop:aspect>
</aop:config>
</beans>
4、在com.itheima.aspectj.xml包中创建测试类TestXmlAspect.java,在类中为了更加清晰地演示几种通知的执行情况,这里只对addUser进行增强,具体代码如下所示:
package com.itheima.aspectj.xml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itheima.jdk.UserDao;
public class TestXmlAspectj {
public static void main(String[] args) {
String xmlPath = "com/itheima/aspectj/xml/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//从spring容器中获得内容
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
//执行方法
userDao.addUser();
}
}
至此,基于XML的声明式AspectJ就全部完成了