目录结构
导入maven 坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
接口类AccountService.java
package com.cenzn.service;
public interface IAccountService {
void saveAccount();
}
实现类AccountServiceImpl.java
package com.cenzn.service.impl;
import com.cenzn.service.IAccountService;
/**
* 模拟账户业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
public void saveAccount() {
System.out.println("保存了账户");
}
}
通知类Logger.java
package com.cenzn.utils;
public class Logger {
public void beforePrintLog(){
System.out.println("前置通知Logger类printLog方法开始记录日记");
}
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类printLog方法开始记录日记");
}
public void afterReturnPrintLog(){
System.out.println("后置通知Logger类printLog方法开始记录日记");
}
public void afterPrintLog(){
System.out.println("最终通知Logger类printLog方法开始记录日记");
}
}
配置文件bean.xml
aop:config:声明 aop 配置
作用:
用于声明开始 aop 的配置
<aop:config>
<!-- 配置的代码都写在此处 -->
</aop:config>
aop:aspect:配置切面
作用:
用于配置切面。
属性:
id:给切面提供一个唯一标识。
ref:引用配置好的通知类 bean 的 id。
<aop:aspect id="txAdvice" ref="logger">
<!--配置通知的类型要写在此处-->
</aop:aspect>
aop:pointcut:配置切入点表达式
作用:
用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。
属性:
expression:用于定义切入点表达式。
id:用于给切入点表达式提供一个唯一标识
<aop:pointcut id="pt1" expression="execution(* com.cenzn.service.impl.AccountServiceImpl.*(..))"/>
aop:before
作用:
用于配置前置通知。指定增强的方法在切入点方法之前执行
属性:
method:用于指定通知类中的增强方法名称
ponitcut-ref:用于指定切入点的表达式的引用
poinitcut:用于指定切入点表达式
执行时间点:
切入点方法执行之前执行
<!-- 前置通知-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
<!-- 后置通知-->
<aop:after-returning method="afterReturnPrintLog" pointcut-ref="pt1"></aop:after-returning>
<!-- 异常通知-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
<!-- 最终通知-->
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>
execution:匹配方法的执行(常用)
execution(表达式)
表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountService" class="com.cenzn.service.impl.AccountServiceImpl"/>
<bean id="logger" class="com.cenzn.utils.Logger"/>
<aop:config>
<!-- <aop:pointcut id="pt1" expression="execution(* com.cenzn.service.impl.AccountServiceImpl.*(..))"/>-->
<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!-- 前置通知-->
<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
<!-- 后置通知-->
<aop:after-returning method="afterReturnPrintLog" pointcut-ref="pt1"></aop:after-returning>
<!-- 异常通知-->
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
<!-- 最终通知-->
<aop:after method="afterPrintLog" pointcut-ref="pt1"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试类AOPTest.java
package com.cenzn.test;
import com.cenzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPtest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService accountService = (IAccountService) ac.getBean("accountService");
accountService.saveAccount();
}
}
运行结果
前置通知Logger类printLog方法开始记录日记
保存了账户
后置通知Logger类printLog方法开始记录日记
最终通知Logger类printLog方法开始记录日记