参考链接:
https://blog.csdn.net/u011983531/article/details/70504281
https://blog.csdn.net/qq_41767337/article/details/89077073
结构
依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="myService" class="aop.MyServiceImpl" />
<bean id="myService2" class="aop.MyService2" />
<bean id="log" class="aop.Log" />
<aop:config>
<aop:aspect ref="log">
<aop:pointcut id="pointcut" expression="execution(* aop.MyServiceImpl.*(..))" />
<aop:before method="before" pointcut-ref="pointcut" />
<aop:before method="before2" pointcut-ref="pointcut" />
<aop:after method="after" pointcut-ref="pointcut" />
</aop:aspect>
<aop:aspect ref="log">
<aop:pointcut id="pointcut2" expression="execution(* aop.MyService2.*(..))" />
<aop:before method="before" pointcut-ref="pointcut2" />
<aop:before method="before2" pointcut-ref="pointcut2" />
<aop:after method="after" pointcut-ref="pointcut2" />
</aop:aspect>
</aop:config>
</beans>
代码
MyService
public interface MyService {
void m1();
String m2();
}
**MyServiceImpl **
public class MyServiceImpl implements MyService {
public void m1() {
System.out.println("执行方法m1");
}
public String m2() {
System.out.println("执行方法m2:返回Hello World!");
return "Hello World!";
}
}
**MyService2 **
public class MyService2 {
public void m1() {
System.out.println("执行方法m1");
}
public String m2() {
System.out.println("执行方法m2:返回Hello World!");
return "Hello World!";
}
}
Log
import org.aspectj.lang.JoinPoint;
public class Log {
public void before(JoinPoint joinPoint) {
System.out.println("前置通知:" + joinPoint.getTarget().getClass().getSimpleName()+ "的"+
joinPoint.getSignature().getName()+ "方法开始执行");
}
public void before2(JoinPoint joinPoint) {
System.out.println("前置通知2:" + joinPoint.getTarget().getClass().getSimpleName()+ "的"+
joinPoint.getSignature().getName()+ "方法开始执行");
}
public void after() {
System.out.println("后置通知");
}
}
测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AopTest {
public static void main(String[] args) {
System.out.println("加载xml文件");
String xmlPath = "applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
MyService myServiceImpl = applicationContext.getBean("myService", MyService.class);
MyService2 myService2 = applicationContext.getBean("myService2", MyService2.class);
myServiceImpl.m1();
System.out.println("---------");
myServiceImpl.m2();
System.out.println("---------");
myService2.m1();
System.out.println("---------");
myService2.m2();
}
}
结果输出
加载xml文件
前置通知:MyServiceImpl的m1方法开始执行
前置通知2:MyServiceImpl的m1方法开始执行
执行方法m1
后置通知
---------
前置通知:MyServiceImpl的m2方法开始执行
前置通知2:MyServiceImpl的m2方法开始执行
执行方法m2:返回Hello World!
后置通知
---------
前置通知:MyService2的m1方法开始执行
前置通知2:MyService2的m1方法开始执行
执行方法m1
后置通知
---------
前置通知:MyService2的m2方法开始执行
前置通知2:MyService2的m2方法开始执行
执行方法m2:返回Hello World!
后置通知