Spring带切入点的环绕通知案例
文件目录
pointcut
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - MyAdvice.java
- - - - - CustomerDao.java
- - - resources
- - - - applicationContext.xml
- - test
- - - java
- - - - MyTest.java
- pom.xml
具体文件
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>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<groupId>com.zhangxin9727</groupId>
<artifactId>pointcut</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</project>
CustomerDao.java
package com.zhangxin9727;
public class CustomerDao {
public void find(){
System.out.println("查询客户···");
}
public void save(){
System.out.println("增加用户···");
}
public void update(){
System.out.println("修改用户···");
}
public void delete(){
System.out.println("删除用户···");
}
}
MyAdvice.java
package com.zhangxin9727;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("===环绕前增强===");
Object obj = methodInvocation.proceed();
System.out.println("===环绕后增强===");
return obj;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="customerDao" class="com.zhangxin9727.CustomerDao"/>
<bean id="myAdvice" class="com.zhangxin9727.MyAdvice"/>
<bean id="advice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="pattern" value=".*update.*"/>
<property name="advice" ref="myAdvice"/>
</bean>
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerDao"/>
<property name="proxyTargetClass" value="true"/>
<property name="interceptorNames" value="advice"/>
</bean>
</beans>
MyTest.java
import com.zhangxin9727.CustomerDao;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.annotation.Resource;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MyTest {
@Resource(name = "customerDaoProxy")
private CustomerDao customerDao;
@Test
public void test() {
customerDao.save();
customerDao.delete();
customerDao.update();
customerDao.find();
}
}