1.新建maven工程,在pom文件上加上以下代码
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
2.配置约束
<?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">
</beans>
3.配置spring创建容器时要扫描的包和spring开启注解AOP的支持
<!--配置spring创建容器时要扫描的包-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!--配置spring开启注解AOP的支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4.配置AccountServiceImpl的注解
package com.itheima.service.impl;
import com.itheima.service.IAccountService;
import org.springframework.stereotype.Service;
/**
* 账户的业务层实现类
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
public void saveAccount(){
System.out.println("执行了保存");
// int i =1/0;
}
public void updateAccount(int i){
System.out.println("执行了更新");
}
public int deleteAccount(){
System.out.println("执行了删除");
return 0;
}
}
5.配置Logger的注解
package com.itheima.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* 用于记录日志的工具类,它里面提供了公共的代码
*/
@Component("logger")
public class Logger {
//省略
}
6.配置切入点和通知类型
@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {
//切入点
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1(){}
/**
* 前置通知
*/
@Before("pt1()")
public void beforePrintLog(){
System.out.println("前置通知Logger类中的beforePrintLog方法开始记录日志了。。。");
}
/**
* 后置通知
*/
@AfterReturning("pt1()")
public void afterReturningPrintLog(){
System.out.println("后置通知Logger类中的afterReturningPrintLog方法开始记录日志了。。。");
}
/**
* 异常通知
*/
@AfterThrowing("pt1()")
public void afterThrowingPrintLog(){
System.out.println("异常通知Logger类中的afterThrowingPrintLog方法开始记录日志了。。。");
}
/**
* 最终通知
*/
@After("pt1()")
public void afterPrintLog(){
System.out.println("最终通知Logger类中的afterPrintLog方法开始记录日志了。。。");
}
}
7.测试
package com.itheima.test;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试AOP的配置
*/
public class AOPTest {
public static void main(String[] args) {
//1.获取容器
ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
IAccountService as= ac.getBean("accountService",IAccountService.class);
//3.执行方法
as.saveAccount();
}
}
8.结果
发现结果有点问题,最终通知在后置通知(异常通知)执行前执行了,这是spring的问题,如果使用环绕通知就不会出现这样的情况了
9.使用环绕通知
@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {
//切入点
@Pointcut("execution(* com.itheima.service.impl.*.*(..))")
private void pt1(){}
/**
* 环绕通知
*/
@Around("pt1()")
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue=null;
try{
Object[] args = pjp.getArgs();//得到方法执行所需要的参数
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。前置通知");
rtValue=pjp.proceed(args);//明确调用业务层方法
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。最终");
}
}
}
10.使用环绕通知的执行结果
运行结果是正常的