Spring 前置通知简单案例
文件目录
annotation
- src
- - main
- - - java
- - - - com.zhangxin9727
- - - - - MyAdvice.java
- - - - - StudentDao.java
- - - - - StudentDaoImp.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>aop</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.0</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
StudentDao.java
package com.zhangxin9727;
public interface StudentDao {
void find();
void save();
void update();
void delete();
}
StudentDaoImp.java
package com.zhangxin9727;
public class StudentDaoImpl implements StudentDao {
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.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) {
System.out.println("===权限校验===");
}
}
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="studentDao" class="com.zhangxin9727.StudentDaoImpl"/>
<bean id="myAdvice" class="com.zhangxin9727.MyAdvice"/>
<bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="studentDao"/>
<property name="proxyInterfaces" value="com.zhangxin9727.StudentDao"/>
<property name="interceptorNames" value="myAdvice"/>
</bean>
</beans>
MyTest.java
import com.zhangxin9727.StudentDao;
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 = "studentDaoProxy")
private StudentDao studentDao;
@Test
public void test() {
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
}
}