pom.xml
1<dependencies> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-context</artifactId> 5 <version>5.0.2.RELEASE</version> 6 </dependency> 7 <!--解析切入点表达式--> 8 <dependency> 9 <groupId>org.aspectj</groupId> 10 <artifactId>aspectjweaver</artifactId> 11 <version>1.8.7</version> 12 </dependency> 13 </dependencies>
bean.xml
1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop 8 http://www.springframework.org/schema/aop/spring-aop.xsd"> 9 10 <!--配置spring的IOC,把service对象配置进来--> 11 <bean id="accountService" class="com.service.impl.AccountServiceImpl"> 12 </bean> 13 <!--spring中基于XML的AOP配置步骤 14 1.把通知Bean也饿交给spring来管理 15 2.使用aop:config标签表明开始AOP的配置 16 3.使用aop:aspect标签表明开始配置切面 17 id属性:是给切面提供一个唯一标识 18 ref属性:是指定通知类的bean的Id 19 4.在aop:aspect标签的内部使用对应标签来配置通知的类型 20 我们现在实例是让pringLog方法在切入点方法执行之前执行,所以是前置通知 21 aop:before:表示配置前置通知 22 method属性:用于指定Logger类中哪个方法是前置通知 23 pointcut属性:用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强 24 25 切入点表达式的写法: 26 关键字:execution(表达式) 27 表达式: 28 访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表) 29 标准的表达式写法: 30 public void com.itheima.service.impl.AccountServiceImpl.saveAccount() 31 包名可以使用通配符,表示任意包,但是有几级包,就需要写几个*. 32 包名可以使用..表示当前包及其子包 33 * *..AccountServiceImpl.saveAccount() 34 类名和方法名都可以使用*号来实现统配 35 * *..*.*() 36 参数列表: 37 可以直接写数据类型: 38 基本类型直接写名称 int 39 引用类型写包名.类名的方式 java.lang.String 40 可以使用通配符表示任意类型,但是必须有参数 41 可以使用..表示有无参数均可,有参数可以是任意类型 42 全通配写法: 43 * *..*.*(..) 44 45 实际开发中切入点表达的通常写法: 46 且到业务层实现类下的所有方法 47 * com.service.impg.*.*(..) 48 --> 49 <!--配置Logger类--> 50 <bean id="logger" class="com.utils.Logger"></bean> 51 <!--配置AOP--> 52 <aop:config> 53 <aop:aspect id="logAdvic" ref="logger"> 54 55 <aop:pointcut id="pt" expression="execution(* com.service.impl.*.*(..))"/> 56 57 <!--环绕通知--> 58 <aop:around method="aroundPrintLog" pointcut-ref="pt"></aop:around> 59 </aop:aspect> 60 61 </aop:config> 62</beans>
代码
1import org.aspectj.lang.ProceedingJoinPoint; 2 3/** 4 * 用于记录日志的工具类,它里面提供了公共的代码 5 */ 6public class Logger { 7 /** 8 * 用于打印日志:计划让其在切入点方法执行之前执行(切入点方法就是业务层方法) 9 * 环绕通知 10 * 当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了。 11 * 分析:通过对比动态代理中的环绕通知代码,发现动态代理的环绕通知有明企鹅的切入点方法调用,而我们的代码中没有 12 * 解决: 13 * spring框架为我们提供了一个接口:ProcedingJoinPoint。该接口有一个方法proced(),此方法就相当于明确调用切入点方法。 14 * 该接口可以作为环绕通知的方法参数,在程序运行时,spring框架会为我们提供该接口的实现类供我们使用。 15 * 16 * spring中的环绕通知: 17 * 它是spring框架为我们提供的一种可以在代理中手动控制增强方法何时执行的方式。 18 */ 19 /* public void aroundPrintLog(){*/ 20 public Object aroundPrintLog(ProceedingJoinPoint pjp){ 21 Object rtValue=null; 22 try { 23 System.out.println("环绕通知 Logger类中的aroundPrintLog方法开始记录日志了。。。前置"); 24 Object [] args=pjp.getArgs(); //得到方法执行所需的参数 25 rtValue= pjp.proceed(); //明确调用业务层方法(切入点方法) 26 27 System.out.println("环绕通知 Logger类中的aroundPrintLog方法开始记录日志了。。。后置"); 28 return rtValue; 29 } catch (Throwable throwable) { 30 //throwable.printStackTrace(); 31 System.out.println("环绕通知 Logger类中的aroundPrintLog方法开始记录日志了。。。异常"); 32 throw new RuntimeException(throwable); 33 } finally { 34 System.out.println("环绕通知 Logger类中的aroundPrintLog方法开始记录日志了。。。最终"); 35 } 36 /* System.out.println("环绕通知 Logger类中的aroundPrintLog方法开始记录日志了。。。");*/ 37 } 38}