栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中基于XML的AOP配置详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中基于XML的AOP配置详解

1. 准备工作

1.1 创建工程 day03_eesy_03SpringAOP

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



 4.0.0

 com.itheima
 day03_eesy_03springAOP
 1.0-SNAPSHOT

 jar

 
 
  org.springframework
  spring-context
  5.2.4.RELEASE
 

 
  org.aspectj
  aspectjweaver
  1.9.5
 
 

说明: 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的约束,后添加配置




 
 

 

 
 

 
 
 
 
  
  
 
 

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

3. 创建测试类AOPTest.java

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. 运行结果

5. 目录结构

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

6.1 介绍



6.2 在bean.xml中表示




 
 

 

 
 

 
 
 
 
  
  
 
 

6.3 在测试类AOPTest.java中测试


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 运行结果

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中进行配置




 
 


 
 

 
 
 
 
  
  

  
  

  
  

  
  
 
 

7.3 在测试类AOPTest.java中运行


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 运行结果

8. 通用化切入点表达式

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

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

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




 
 


 
 

 
 
 
 
  
  

  
  

  
  

  
  


  
  
 
 

运行结果:

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

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




 
 


 
 

 
 

 
 
 
 
 
  
  

  
  

  
  

  
  
 
 

运行结果:

此时所有的切面都能使用该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中完成配置




 
 


 
 

 
 
 
 

 
 
  
  
 
 

9.3 此时运行结果

9.4 问题分析

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

原因:

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

9.5 完善aroundPringLog方法

Logger.java


public class Logger {
 
 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 运行结果

9.7 目录结构

到此这篇关于Spring中基于XML的AOP配置的文章就介绍到这了,更多相关Sprin AOP配置内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/130830.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号