准备工作
- maven添加依赖AOP切入点表达式
<!-- 用于解析AOP切入点表达式-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
使用自定义类来实现AOP
- 编写业务类
public class AccountServiceImpl implements AccountService {
public void saveAccount() {
System.out.println("保存了数据");
// int i=1/0;
}
public void updateAccount(int i) {
System.out.println("更新了数据"+i);
}
public int deleteAccount() {
System.out.println("删除了数据");
return 0;
}
}
- 编写切入类:
public class Logger {
//用于打印日志,计划让其在切入点方法执行之前执行(前置通知)
public void beforePrintLog(){
System.out.println("Logger类中的beforePrintLog()方法开始记录日志了");
}
//(后置通知)
public void afterReturningPrintLog(){
System.out.println("Logger类中的afterReturningPrintLog()方法开始记录日志了");
}
//异常通知
public void afterThrowingPrintLog(){
System.out.println("Logger类中的afterThrowingPrintLog()方法开始记录日志了");
}
//最终通知
public void afterPrintLog(){
System.out.println("Logger类中的afterPrintLog()方法开始记录日志了");
}
}
- 在spring核心配置文件配置
<?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">
<!-- 添加aop命名空间-->
<!-- 配置springIOC中AccountServiceImpl对象-->
<bean id="accountService" class="com.chenhui.service.AccountServiceImpl"></bean>
<!-- 配置springIOC中Logger对象-->
<bean id="logger" class="com.chenhui.util.Logger"></bean>
<!--使用aop:config标签表明开始AOP的配置
使用aop:aspect标签表明配置切面
id属性:是给切面提供一个唯一标识
ref属性:是值指定通知类bean的id
aop:before标签表示前置通知
method属性表示:用于指定Logger类中哪个方法是前置通知
pointcut属性:用于指定切入点表达式,该表达式的含义指的是对哪个类中的哪个方法进行增强-->
<!-- 配置AOP-->
<aop:config>
<!-- 如果aop:pointcut标签在aop:aspect标签内部,则只能当前切面使用-->
<!-- 如果写在外部则,所有切面可用-->
<aop:pointcut id="pc" expression="execution(* com.chenhui.service.AccountServiceImpl.*(..))"/>
<aop:aspect id="logAdvice" ref="logger">
<!-- 配置前置通知:在切入点方法执行之前-->
<aop:before method="beforePrintLog" pointcut-ref="pc"></aop:before>
<!-- 配置后置通知:在方法正常执行之后执行 它和异常通知只能执行一个-->
<aop:after-returning method="afterReturningPrintLog"
pointcut="execution(* com.chenhui.service.AccountService.*(..))"></aop:after-returning>
<!-- 配置异常通知:在切入点方法执行产生异常之后执行 它和后置通知只能执行一个-->
<aop:after-throwing method="afterThrowingPrintLog"
pointcut="execution(* com.chenhui.service.AccountService.*(..))"></aop:after-throwing>
<!-- 配置最终通知:无论切入点方法是否正常执行它都会在其后面执行-->
<aop:after method="afterPrintLog"
pointcut="execution(* com.chenhui.service.AccountService.*(..))"></aop:after>
</aop:aspect>
</aop:config>
</beans>
测试类:
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
//注意:此时获取出的bean对象是一个proxy类型的类,它是AccountService的一个实现类,也是代理类,
//此时不能传入AccountServiceImpl.class,因为他们是同级,不能转化,只能通过接口转化,他们都是实现了AccountService接口
AccountService accountService = applicationContext.getBean("accountService",AccountService.class);
//执行方法
accountService.saveAccount();
accountService.deleteAccount();
accountService.updateAccount(1);
}
结果:
如果某个方法出现异常,则会打印异常通知,而后置通知和异常通知只能执行一个
总结:切入点的表达式的写法
关键字:execution(表达式)
表达式:访问修饰符 返回值 包名.包名…类名.方法名(参数列表)
标准的表达式写法:
public void com.chenhui.service.AccountServiceImpl.saveAccount()
- 访问权限修饰符可以省略:
void com.chenhui.service.AccountServiceImpl.saveAccount()
- 返回值可以使用通配符(*),表示任意返回值:
* com.chenhui.service.AccountServiceImpl.saveAccount()
- 包名可以使用通配符(*)表示任意包,有几级包,就要写几个:
* *.*.*.AccountServiceImpl.saveAccount()
- 包名可以使用…表示当前包及其子包
* *..AccountServiceImpl.saveAccount()
- 类名和方法名都可以使用*来实现通配
* *..*.*()
- 方法的参数列表写法
参数列表可以使用
1.基本类型 直接写名称
2.引用类型 包名.类名的方式
可以使用通配符表示任意类型,但是方法必须有参数才能执行该切点
可以使用…表示有无参数均可,有参数为任意类型
- 全通配写法:
* *..*.*(..)