spring的基于xml的AOP的案例
基本的环境配置
首先建立maven工程,在pom.xml中导入
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springxml-proxy-config</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
</project>
建立代理和被代理类
被代理类的接口
package test.service;
public interface service {
public void getall();
}
被代理的类
package test.service.impl;
import test.service.service;
public class serviceimpl implements service {
public void getall() {
System.out.println("需要代理增强的函数");
}
}
需要在切面执行的通知的类的方法
package test.service.util;
import org.aspectj.lang.ProceedingJoinPoint;
public class publiccode {
public void Addcode(){
System.out.println("ADDcode函数增强了");
}
public void Addcode1(){
System.out.println("ADDcode函数增强了1");
}
}
环绕通知的函数,比较特殊,所以特意提出来,是上个类的函数
环绕通知
问题:
当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了。
分析:
通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有。
解决:
Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。
该接口可以作为环绕通知的方法参数,在程序执行时,spring框架会为我们提供该接口的实现类供我们使用。
spring中的环绕通知:
它是spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式。
public void Addcode2(ProceedingJoinPoint pjp) {
Object rtValue=null;
try {
System.out.println("前置");
Object []args=pjp.getArgs();
System.out.println("环绕通知执行了");
rtValue=pjp.proceed(args);
System.out.println("后置");
}catch (Throwable e){
System.out.println("异常");
throw new RuntimeException(e);
}finally {
System.out.println("最终");
}
前置、后置、异常、最终通知的bean.xml的配置
配置切入点表达式 id属性用于指定表达式的唯一标识。expression属性用于指定表达式内容
此标签写在aop:aspect标签内部只能当前切面使用。
它还可以写在aop: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"
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">
<bean id="service" class="test.service.impl.serviceimpl"></bean>
<bean id="log" class="test.service.util.publiccode"></bean>
<aop:config>
<aop:aspect id="logAdvice" ref="log">
<aop:pointcut id="servicecut" expression="execution(public void test.service.impl.serviceimpl.getall())"/>
<aop:before method="Addcode" pointcut-ref="servicecut"/>
<aop:after method="Addcode" pointcut-ref="servicecut"/>
<aop:after-returning method="Addcode1" pointcut="execution(* *.*.*.*.getall())"/>
<aop:around method="Addcode2" pointcut-ref="servicecut"/>
</aop:aspect>
</aop:config>
</beans>