Spring中基于XML的AOP配置详解

1. 准备工作

1.1 创建工程 day03_eesy_03SpringAOP

1.2 在配置文件pom.xml中添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.itheima</groupId>
 <artifactId>day03_eesy_03springAOP</artifactId>
 <version>1.0-SNAPSHOT</version>

 <packaging>jar</packaging>

 <dependencies>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.4.RELEASE</version>
 </dependency>

 <dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.5</version>
 </dependency>
 </dependencies>
</project>

说明: aspect依赖是用来声明切入点坐标的

1.3 编写业务层代码

1.创建包 service

2.创建业务层接口IAccountService.java

/**
 * 账户的业务层接口
 */
public interface IAccountService {

 /**
 * 模拟保存账户
 */
 void saveAccount();

 /**
 * 模拟更新账户
 */
 void updateAccount(Integer i);
 /**
 * 模拟删除账户
 */
 int deleteAccount();
}

3.创建业务层实现类AccountServiceImpl.java

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {
 public void saveAccount() {
 System.out.println("执行了保存");
 }

 public void updateAccount(Integer i) {
 System.out.println("执行力更新");
 }

 public int deleteAccount() {
 System.out.println("执行了删除");
 return 0;
 }
}

4.创建日志类

​ 该类为用于记录日志的工具类,它里面提供了公共的代码(通知)

Logger.java

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {

 /**
 * 用于打印日志,计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
 */
 public void printLog(){
 System.out.println("Logger类中的printLog方法开始记录日志了");
 }
}

2. 进行配置

创建配置文件 bean.xml

先添加包含AOP的约束,后添加配置

<?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
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

 <!--Spring中基于XML的AOP配置步骤
  1.把通知Bean也交给Spring来管理
  2.使用aop:config标签表明开始AOP的配置
  3.使用aop:aspect标签表明配置切面
   id属性: 是给切面提供唯一标识
   ref属性: 是指定通知类bean的id
  4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
   我们现在的示例是让Logger类的printLog方法在切入点方法执行之前执行, 所以是前置通知
   aop:before : 表示配置前置通知
    method属性: 用于指定Logger类中哪个方法是前置通知
   pointcut属性: 用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强

  切入点表达式的写法:
   关键字: execution(表达式)
   表达式:
    访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
   标准的切入点表达式:
    public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
 -->

 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>
 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置通知的类型,并且建立通知方法和接入点方法的关联-->
  <aop:before method="printLog" pointcut="execution(public void com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:before>
 </aop:aspect>
 </aop:config>

</beans>

说明: 切入点表达式最好按软件的提示写,自己直接手写在测试时容易报错

3. 创建测试类AOPTest.java

/**
 * 测试AOP的配置
 */
public class AOPTest {
 public static void main(String[] args) {
 //1.获取容器
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
 //2.获取对象
 IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
 accountService.saveAccount();
 }
}

4. 运行结果

Spring中基于XML的AOP配置详解

5. 目录结构

Spring中基于XML的AOP配置详解

6. 切入点表达式写法补充

6.1 介绍

<!-- 切入点表达式的写法:
   关键字: execution(表达式)
   表达式:
    访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
   标准的切入点表达式:
    public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
   访问修饰符可以省略:
    void com.itheima.service.impl.AccountServiceImpl.saveAccount()
   返回值可以使用通配符,表示任意返回值
    * com.itheima.service.impl.AccountServiceImpl.saveAccount()
   包名可以使用通配符,表示任意包,但是又几级包,就需要写几个 *.
    * *.*.*.*.AccountServiceImpl.saveAccount()
   包名可以使用..表示当前包及其子包
    * *..AccountServiceImpl.saveAccount()
   类名和方法名都可以使用*来实现通配
    * *..*.*()
   参数列表:
    可以直接写数据类型:
    基本类型直接写名称  int
    引用类型写包名.类名的方式 java.lang.String
    可以使用通配符表示任意类型,但是必须有参数
    可以是使用..表示有无参数均可,有参数可以是任意类型
   全通配写法:
    * *..*.*(..)

   实际开发中切入点表达式的通常写法:
    切到业务层实现类下的所有的方法
    * com.itheima.service.impl.*.*(..)
-->

6.2 在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"
 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">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>

 <!--Spring中基于XML的AOP配置步骤
  1.把通知Bean也交给Spring来管理
  2.使用aop:config标签表明开始AOP的配置
  3.使用aop:aspect标签表明配置切面
   id属性: 是给切面提供唯一标识
   ref属性: 是指定通知类bean的id
  4.在aop:aspect标签的内部使用对应的标签来配置通知的类型
   我们现在的示例是让Logger类的printLog方法在切入点方法执行之前执行, 所以是前置通知
   aop:before : 表示配置前置通知
    method属性: 用于指定Logger类中哪个方法是前置通知
   pointcut属性: 用于指定切入点表达式,该表达式的含义指的是对业务层中哪些方法增强

  切入点表达式的写法:
   关键字: execution(表达式)
   表达式:
    访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
   标准的切入点表达式:
    public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
   访问修饰符可以省略:
    void com.itheima.service.impl.AccountServiceImpl.saveAccount()
   返回值可以使用通配符,表示任意返回值
    * com.itheima.service.impl.AccountServiceImpl.saveAccount()
   包名可以使用通配符,表示任意包,但是又几级包,就需要写几个 *.
    * *.*.*.*.AccountServiceImpl.saveAccount()
   包名可以使用..表示当前包及其子包
    * *..AccountServiceImpl.saveAccount()
   类名和方法名都可以使用*来实现通配
    * *..*.*()
   参数列表:
    可以直接写数据类型:
    基本类型直接写名称  int
    引用类型写包名.类名的方式 java.lang.String
    可以使用通配符表示任意类型,但是必须有参数
    可以是使用..表示有无参数均可,有参数可以是任意类型
   全通配写法:
    * *..*.*(..)

   实际开发中切入点表达式的通常写法:
    切到业务层实现类下的所有的方法
    * com.itheima.service.impl.*.*(..)

 -->

 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>
 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置通知的类型,并且建立通知方法和接入点方法的关联-->
  <aop:before method="printLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before>
 </aop:aspect>
 </aop:config>

</beans>

6.3 在测试类AOPTest.java中测试

/**
 * 测试AOP的配置
 */
public class AOPTest {
 public static void main(String[] args) {
 //1.获取容器
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
 //2.获取对象
 IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
 accountService.saveAccount();
 accountService.updateAccount(1);
 accountService.deleteAccount();
 }
}

6.4 运行结果

Spring中基于XML的AOP配置详解

7. 四种通知类型补充

 7.1 在Logger.java类中添加方法

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {

 /**
 * 前置通知
 */
 public void beforePrintLog(){
 System.out.println("前置通知:Logger类中的printLog方法开始记录日志了");
 }

 /**
 * 后置通知
 */
 public void afterReturningPrintLog(){
 System.out.println("后置通知:Logger类中的printLog方法开始记录日志了");
 }
 /**
 * 异常通知
 */
 public void afterThrowingPrintLog(){
 System.out.println("异常通知:Logger类中的printLog方法开始记录日志了");
 }
 /**
 * 最终通知
 */
 public void afterPrintLog(){
 System.out.println("最终通知:Logger类中的printLog方法开始记录日志了");
 }
}

7.2 在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"
 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">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>
 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置前置通知: 在切入点方法执行之前执行-->
  <aop:before method="beforePrintLog" pointcut="execution(public void com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:before>

  <!--配置后通知: 在切入点方法正常执行之后执行; 他和异常通知只能执行一个-->
  <aop:after-returning method="afterReturningPrintLog" pointcut="execution(public void com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:after-returning>

  <!--配置异常通知: 在切入点方法执行产生异常之后执行; 他和后置通知只能执行一个-->
  <aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(public void com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:after-throwing>

  <!--配置最终通知: 无论切入点方法是否正常执行他都会在其后面执行-->
  <aop:after method="afterPrintLog" pointcut="execution(public void com.itheima.service.impl.AccountServiceImpl.saveAccount())"></aop:after>
 </aop:aspect>
 </aop:config>

</beans>

7.3 在测试类AOPTest.java中运行

/**
 * 测试AOP的配置
 */
public class AOPTest {
 public static void main(String[] args) {
 //1.获取容器
 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
 //2.获取对象
 IAccountService accountService = (IAccountService) applicationContext.getBean("accountService");
 accountService.saveAccount();

 }
}

7.4 运行结果

Spring中基于XML的AOP配置详解

8. 通用化切入点表达式

用于解决在bean.xml文件中配置通知时多次写切入点表达式的问题

使用 aop:pointcut标签,在bean.xml中进行配置

8.1 在aop:aspect标签内部使用aop:pointcut

<?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
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>
 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置前置通知: 在切入点方法执行之前执行-->
  <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>

  <!--配置后通知: 在切入点方法正常执行之后执行; 他和异常通知只能执行一个-->
  <aop:after-returning method="afterReturningPrintLog" 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>


  <!--配置切入点表达式 id属性用于指定表达式的唯一标识, expression属性用于指定表达式的内容
   此标签写在app:aspect标签内部,只能当前切面使用。
   它还可以写在aop:aspect外面, 此时就变成了所有切面可用
  -->
  <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.AccountServiceImpl.*(..))"></aop:pointcut>
 </aop:aspect>
 </aop:config>

</beans>

运行结果:

Spring中基于XML的AOP配置详解

此时,aop:pointcut定义的切入点表达式只能在当前切面中使用

8.2 在aop:aspect标签外部使用aop:pointcut

<?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
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>

 <!--配置切入点表达式 id属性用于指定表达式的唯一标识, expression属性用于指定表达式的内容
   此标签写在app:aspect标签内部,只能当前切面使用。
   它还可以写在aop:aspect外面, 此时就变成了所有切面可用
  -->
 <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.AccountServiceImpl.*(..))"></aop:pointcut>
 
 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置前置通知: 在切入点方法执行之前执行-->
  <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>

  <!--配置后通知: 在切入点方法正常执行之后执行; 他和异常通知只能执行一个-->
  <aop:after-returning method="afterReturningPrintLog" 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>

运行结果:

Spring中基于XML的AOP配置详解

此时所有的切面都能使用该aop:pointcut定义的切入点表达式

主要: 当在aop:aspect标签外部使用aop:pointcut标签定义切入点表达式的时候,由于使用的约束的规定, aop:pointcut标签只能在 aop:aspect标签上方

9. Spring的环绕通知

9.1 在Logger.java中添加实现环绕通知的方法 aroundPringLog

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {
 /**
 * 环绕通知
 *
 */
 public void aroundPringLog(){
  System.out.println("Logger类中的aroundPringLog方法开始记录日志了");
 }

}

9.2 在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"
 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">

 <!--配置Spring的IOC,把service对象配置进来-->
 <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


 <!--配置Logger类-->
 <bean id="logger" class="com.itheima.utils.Logger"></bean>

 <!--配置AOP-->
 <aop:config>
 
 <aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.AccountServiceImpl.*(..))"></aop:pointcut>

 <!--配置切面-->
 <aop:aspect id="logAdvice" ref="logger">
  <!--配置环绕通知 详细的注释在Logger类中-->
  <aop:around method="aroundPringLog" pointcut-ref="pt1"></aop:around>
 </aop:aspect>
 </aop:config>

</beans>

9.3 此时运行结果

Spring中基于XML的AOP配置详解

9.4 问题分析

此时只执行了通知方法,而切入点方法没有执行

原因:

​ 通过对比动态的代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而本案例中的代码没有

9.5 完善aroundPringLog方法

Logger.java

/**
 * 用于记录日志的工具类,它里面提供了公共的代码
 */
public class Logger {
 /**
 * 环绕通知
 * 问题:
 * 当我们配置了环绕通知之后,切入点方法没有执行,而通知方法执行了
 * 分析:
 * 通过对比动态的代理中的环绕通知代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码中没有
 * 解决:
 * Spring框架为我们提供了一个接口,ProceedingJoinPoint,该接口有一个方法proceed(),此方法就相当于明确调用切入点方法
 * 该接口可以作为环绕通知的方法参数, 在程序执行的时候,Spring框架会为我们提供该接口供我们使用
 *
 * Spring的环绕通知:
 * 他是Spring框架为我们提供的一种可以在代码中手动控制增强方法何时执行的方式
 */
 public Object aroundPringLog(ProceedingJoinPoint proceedingJoinPoint){

 Object returnValue = null;
 try {

  Object[] args = proceedingJoinPoint.getArgs(); //得到方法执行所需要的参数

  System.out.println("Logger类中的aroundPringLog方法开始记录日志了-----前置");

  returnValue = proceedingJoinPoint.proceed(args); //明确调用业务层方法,切入点方法

  System.out.println("Logger类中的aroundPringLog方法开始记录日志了-----后置");

  return returnValue;
 }catch (Throwable throwable){
  System.out.println("Logger类中的aroundPringLog方法开始记录日志了-----异常");
  throw new RuntimeException(throwable);
 }finally {
  System.out.println("Logger类中的aroundPringLog方法开始记录日志了-----最终");
 }
 }

}

9.6 运行结果

Spring中基于XML的AOP配置详解

9.7 目录结构

Spring中基于XML的AOP配置详解