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

Spring boot AOP通过XML配置文件声明的方法

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

Spring boot AOP通过XML配置文件声明的方法

通过 XML 配置文件声明

在前两篇博文和示例中,我们已经展示了如何通过注解配置去声明切面,下面我们看看如何在 XML 文件中声明切面。下面先列出 XML 中声明 AOP 的常用元素:

AOP配置元素 用途
aop:advisor 定义AOP通知器
aop:after 定义AOP后置通知(不管被通知的方法是否执行成功)
aop:after-returning 定义AOP返回通知
aop:after-throwing 定义AOP异常通知
aop:around 定义AOP环绕通知
aop:aspect 定义一个切面
aop:aspectj-autoproxy 启用@AspectJ注解驱动的切面
aop:before 定义一个AOP前置通知
aop:config 顶层的AOP配置元素。大多数的aop:*元素必须包含在aop:config元素内
aop:declare-parents 以透明的方式为被通知的对象引入额外的接口
aop:pointcut 定义一个切点

XML 配置文件中切点指示器

在XML配置文件中,切点指示器表达式与通过注解配置的写法基本一致,区别前面有提到,即XML文件中需要使用 “and”、“or”、“not”来表示 “且”、“或”、“非”的关系。

XML 文件配置 AOP 

新建OrderXmlAop.java:

package com.example.demo.aop;
 
public class OrderXmlAop {
 
 
 public void doBefore(){
  System.out.println("阿里阿塞哟!");
 }
 
 
 public void doAfter(){
  System.out.println("after!");
 }
 
 
 
  public void doAfterReturning(){
 
   System.out.println("返回通知:AfterReturning");
  }
 
 
 public void doAfterThrowing(){
  System.out.println("异常通知:AfterThrowing");
  }
}

在 Resource 目录下新建一个配置文件 aoporder.xml :



 
 
 
 
 
 
 
 
 
  
  
   
   
   
   
   
   
   
   
  
 

新建 TakeXmlController.java

package com.example.demo.controller;
 
import com.example.demo.entity.Response;
import com.example.demo.entity.ResponseResult;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.TakeawayService;
@RestController
@RequestMapping("/api")
 
public class TakeXmlController {
 
 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aoporder.xml");
 @RequestMapping("/orderxml")
 public ResponseResult Ordexml()
 {
  
  TakeawayService wmzService=(TakeawayService)context.getBean("wmzService");
  String wmz= wmzService.Order(12);
  System.out.println(wmz);
  TakeawayService zsService=(TakeawayService)context.getBean("zsService");
  String zs=zsService.Order(4396);
  System.out.println(zs);
  return Response.makeOKRsp(wmz+";"+zs);
 }
}

运行结果:

声明环绕通知

修改OrderXmlAop.java:

package com.example.demo.aop;
 
import org.aspectj.lang.ProceedingJoinPoint;
 
public class OrderXmlAop {
 
 
 public void doBefore(){
  System.out.println("阿里阿塞哟!");
 }
 
 
 public void doAfter(){
  System.out.println("after!");
 }
 
 
 
  public void doAfterReturning(){
 
   System.out.println("返回通知:AfterReturning");
  }
 
 
 public void doAfterThrowing(){
  System.out.println("异常通知:AfterThrowing");
  }
 
 
 public void doAround(ProceedingJoinPoint pj) {
  try {
   System.out.println("Around 调用方法前 ");
   pj.proceed();
   System.out.println("Around 调用方法后");
  } catch (Throwable throwable) {
   throwable.printStackTrace();
  }
 }
}

aoporder.xml:



 
 
 
 
 
 
 
 
 
  
  
   
   
   
   
   
   
   
   
   
   
  
 

运行结果:

结果和我们预期的一致,环绕通知通过xml配置成功。

XML 文件配置声明切点 

在上面的例子中,我们发现有切点表达式多次重复出现,那么可不可以和aspectj配置一样,单独声明切点,后面复用,答案是当然可以。如下修改aoporder.xml:



 
 
 
 
 
 
 
 
  
  
  
  
   
   
   
   
   
   
   
   
   
   
  
 

修改后执行结果:

XML文件配置为通知传递参数

修改OrderXmlAop.java

public String doAround(ProceedingJoinPoint pj,double price) {
  try {
   System.out.println("Around 调用方法前 ");
   pj.proceed();
   if(price>=4396)
   {
    System.out.println("zs下单超过了4399,赠送一份鲜果饮汇源牌饮料");
    return "爆浆牛丸和饮料";
   }
   System.out.println("Around 调用方法后");
  } catch (Throwable throwable) {
   throwable.printStackTrace();
  }
  return "爆浆牛丸";
 }

修改aoporder.xml



 
 
 
 
 
 
 
 
  
  
  
  
   
   
  
 

总结

本文主要通过XML配置文件使用 Spring AOP进行编程,和上一篇的注解方式两者联系起来对于刚入门的应该多多少少还是有点帮助的吧,针对于aop 通过三篇博客简单的描述,相信大家对此都有点印象了,记录了 AOP 的编程思想,然后介绍了 Spring 中 AOP 的相关概念,以及通过注解方式和XML配置文件两种方式使用 Spring AOP进行编程。所以对aop的博文就简单到这儿了,有人要问了,aop里面的代理啊还有各种各样的,如果真要吧aop重头到尾来一遍的话,这个系列可以单独提出来一个专栏了,所以后面的博文应该都是围绕连接数据库,记录日志,接入swagger文档等功能相继展开了。在此过程中,我有错误使用的地方,或者表达有问题,还请您及时告知,本人会在第一时间予以改正。最后在祝大家周末愉快,C Y L L

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

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

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