- 目标对象的接口:IStudent.java
package com.springAOP.demo;
public interface IStudent {
public void addStudent(String name);
}
- 目标类:StudentImpl.java
package com.springAOP.demo;
public class StudentImpl implements IStudent{
@Override
public void addStudent(String name) {
System.out.println( " 欢迎 " + name + " 你加入Spring家庭! " );
}
}
- 前置通知:BeforeAdvice.java
package com.springAOP.demo;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(" 这是BeforeAdvice类的before方法. ");
}
}
- 后置通知:AfterAdvice.java
package com.springAOP.demo;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{
@Override
public void afterReturning(Object returnValue,Method method,
Object[] args,Object target) throws Throwable {
System.out.println("这是AfterAdvice类的afterReturning方法.");
}
}
- 环绕通知:CompareInterceptor.java
package com.springAOP.demo;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class CompareInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object result = null;
String stu_name = invocation.getArguments()[0].toString();
if (stu_name.equals("test")) {
// 如果学生是test时,执行目标方法,
result = invocation.proceed();
} else {
System.out.println("此学生是" + stu_name + "而不是test,不批准其加入.");
}
return result;
}
}
- 配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<bean id="beforeAdvice" class="com.springAOP.demo.BeforeAdvice"></bean>
<bean id="afterAdvice" class="com.springAOP.demo.AfterAdvice"></bean>
<bean id="compareInterceptor" class="com.springAOP.demo.CompareInterceptor"></bean>
<bean id="studenttarget" class="com.springAOP.demo.StudentImpl"></bean>
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.springAOP.demo.IStudent</value>
</property>
<property name="interceptorNames">
<list>
<value>beforeAdvice</value>
<value>afterAdvice</value>
<value>compareInterceptor</value>
</list>
</property>
<property name="target">
<ref bean="studenttarget"/>
</property>
</bean>
</beans>
- 测试类,App.java
package com.springAOP.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx =new FileSystemXmlApplicationContext("src/main/resources/applicationContext.xml");
IStudent person = (IStudent)ctx.getBean("student");
person.addStudent("test");
//person.addStudent("sunbeam");
}
}
运行结果
这是BeforeAdvice类的before方法.
欢迎 test 你加入Spring家庭!
这是AfterAdvice类的afterReturning方法.
- maven jar包 pom.xml
<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>springAOP</groupId>
<artifactId>springAOP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springAOP</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这里项目是一个maven项目引入jar包即可,如果是java项目需要下载对应的jar包