基于XML方式的AOP实现
在spring中aop实现方法包括xml和注解方式,在了解了动态代理后,通过xml方式来实现spring的AOP
创建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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--配置IOC-->
<bean id="userDao" name="userDao" class="com.example.inf.impl.UserDaoImp" scope="singleton"></bean>
<!--配置增强的方法-->
<bean id="aopMethod" name="aopMethod" class="com.example.aspect.aopMethod"></bean>
<aop:config>
<aop:aspect id="myAspect" ref="aopMethod">
<aop:before method="beforeMethod" pointcut-ref="pt1"></aop:before>
<aop:after-returning method="afterReturnMethod" pointcut-ref="pt1"></aop:after-returning>
<aop:after-throwing method="throwMethod" pointcut-ref="pt1"></aop:after-throwing>
<aop:after method="afterMethod" pointcut-ref="pt1"></aop:after>
<aop:pointcut id="pt1" expression="execution(* com.example.inf.impl.UserDaoImp.*(..))"></aop:pointcut>
</aop:aspect>
</aop:config>
</beans>
在bean.xml中我们引入了aop约束,这个xml信息可以从spring官网中获取到。而且在pom.xml中也需要引入aspectjweaver来解析bean.xml中的aop信息。
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
在bean.xml中需要配置bean(也就是我们需要注入的组件)以及要增强的组件bean
aop的配置需要通过aop:config节点进行处理
其中<aop:aspect id="myAspect" ref="aopMethod">
中的ref指向要增强的方法的bean的id,可以看这对应上面的节点中增强方法的id。
aop节点存在5种方法,分别是
aop:before
在调用方法前执行aop:after-returning
在执行返回值后执行aop:after
方法执行后执行aop:after-throwing
异常后执行。异常后执行与after-returning执行一个aop-arround
环绕通知,单独介绍
<aop:config>
<aop:aspect id="myAspect" ref="aopMethod">
<aop:before method="beforeMethod" pointcut-ref="pt1"></aop:before>
<aop:after-returning method="afterReturnMethod" pointcut-ref="pt1"></aop:after-returning>
<aop:after-throwing method="throwMethod" pointcut-ref="pt1"></aop:after-throwing>
<aop:after method="afterMethod" pointcut-ref="pt1"></aop:after>
<aop:pointcut id="pt1" expression="execution(* com.example.inf.impl.UserDaoImp.*(..))"></aop:pointcut>
</aop:aspect>
</aop:config>
其中看出method就是要增强的方法,point-ref:引用的切面方法,也就是要增强哪些方法
在本例中通过aop:pointcut节点来定义切面方法
expression表达式可以用来表示包里面哪些方法进行增强,它的格式如下:
返回值 包.方法(参数)
如果都不想写就用*代替,包有几个节点就用 *代替
参数使用…代替有参数/无参数
增强方法
package com.example.aspect;
public class aopMethod {
public void beforeMethod(){
System.out.println("before方法执行");
}
public void afterReturnMethod(){
System.out.println("afterReturnMethod方法执行");
}
public void throwMethod(){
System.out.println("throwMethod方法执行");
}
public void afterMethod(){
System.out.println("afterMethod方法执行");
}
public void arroundMethod(){
}
}
测试类
package com.example.ui;
import com.example.inf.IUserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class aopClient {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
IUserDao userDao=(IUserDao) ac.getBean("userDao");
userDao.addUser(34);
}
}
输出
before方法执行
新增用户方法34
afterReturnMethod方法执行
afterMethod方法执行
可以看出增强方法完全执行了。
分析
这样配置是非常麻烦的,增抢方法分的非常细,执行前后/异常都要编码,是否存在写一个方法就能将在这一个方法里实现?这就是环绕通知。
环绕通知 aop-arround
环绕通知增强方法中必须要有参数ProceedingJoinPoint
,要不然代理对象的方法不会执行。
/**
* 环绕通知
*/
public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint){
try{
Object[] args=proceedingJoinPoint.getArgs();
System.out.println("环绕通知-执行前方法-aop:before");
Object rtValue=proceedingJoinPoint.proceed(args);
System.out.println("环绕通知-执行后方法-aop:after-returning");
return rtValue;
}
catch (Throwable e){
System.out.println("环绕通知-异常通知方法-aop:after-throwing");
throw new RuntimeException(e);
}
finally {
System.out.println("环绕通知-最终通知方法-aop:after");
}
}
这个方法里需要注意的是:catch中只能是Throwable
不能是Exception
,要不然proceedingJoinPoint.proceed
方法报错。
bean.xml
中增加环绕通知
<aop:config>
<aop:aspect id="myAspect" ref="aopMethod">
<aop:around method="arroundMethod" pointcut-ref="pt1"></aop:around>
<aop:pointcut id="pt1" expression="execution(* com.example.inf.impl.UserDaoImp.*(..))"/>
</aop:aspect>
</aop:config>
测试类不用到,和以上方法一样
输出
环绕通知-执行前方法-aop:before
新增用户方法34
环绕通知-执行后方法-aop:after-returning
环绕通知-最终通知方法-aop:after
分析
可以看出环绕通知实现方式比较紧凑,代码集中度比较高。