1 > maven工程pom依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
2 > 需要用到的类
2.1 > 接口及其实现类
![在这里插入图片描述](https://img-blog.csdnimg.cn/b9edfd36258e4e20a2fdcbf53edee574.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzMwOTg5Mw==,size_16,color_FFFFFF,t_70)
2.2 > 通知类
package com.xx01.advice;
import org.aspectj.lang.ProceedingJoinPoint;
public class Logger {
public void beforeAdvice() {
System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
}
public void afterRunningAdvice() {
System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
}
public void exceptionAdvice() {
System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
}
public void afterAdvice() {
System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
}
public Object roundAdvice(ProceedingJoinPoint point) {
Object returnValue = null;
Object[] args = point.getArgs();
try {
System.out.println("This is : " + Thread.currentThread().getStackTrace()[1].getMethodName());
returnValue = point.proceed(args);
afterRunningAdvice();
} catch (Throwable throwable) {
exceptionAdvice();
throwable.printStackTrace();
}finally {
afterAdvice();
}
return returnValue;
}
}
2.3 > 接口及其实现类
public interface AccountDao {
void saveAccount(Float money);
}
public class AccountDaoImpl implements AccountDao {
@Override
public void saveAccount(Float money) {
System.out.println("Save money is : " + money);
int i= 4/0;
}
}
3 > spring_ioc容器配置AOP切面 (bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<bean id="accountDao" class="com.xx01.dao.impl.AccountDaoImpl"/>
<bean id="logger" class="com.xx01.advice.Logger"/>
<aop:config>
<aop:aspect id="aspect_weaver" ref="logger">
<aop:pointcut id="pointcut_accountDaoImpl_saveMoney"
expression="
execution(public void com.xx01.dao.impl.AccountDaoImpl.saveAccount(Float))"/>
<aop:before method="beforeAdvice" pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>
<aop:after-returning method="afterRunningAdvice"
pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>
<aop:after-throwing method="exceptionAdvice"
pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>
<aop:after method="afterAdvice" pointcut-ref="pointcut_accountDaoImpl_saveMoney"/>
</aop:aspect>
</aop:config>
</beans>
4 > 测试
4.1 > 测试结果(int i = 4 / 0;此异常存在)
public class Dome_01 {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("./bean.xml");
AccountDao accountDao = app.getBean("accountDao", AccountDao.class);
accountDao.saveAccount(1000f);
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/eecf9b3acde3412cb84fa7511a48ea27.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzMwOTg5Mw==,size_16,color_FFFFFF,t_70)
4.2 > 测试结果(int i = 4 / 0;此异常不存在)
![在这里插入图片描述](https://img-blog.csdnimg.cn/a39e1d38bb4d4ae8b4d4463641e2c7df.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzMwOTg5Mw==,size_16,color_FFFFFF,t_70)