电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Spring boot AOP通過XML配置文件聲明的方法

瀏覽:49日期:2023-05-09 17:24:15

通過 XML 配置文件聲明

在前兩篇博文和示例中,我們已經(jīng)展示了如何通過注解配置去聲明切面,下面我們看看如何在 XML 文件中聲明切面。下面先列出 XML 中聲明 AOP 的常用元素:

AOP配置元素 用途 aop:advisor 定義AOP通知器 aop:after 定義AOP后置通知(不管被通知的方法是否執(zhí)行成功) aop:after-returning 定義AOP返回通知 aop:after-throwing 定義AOP異常通知 aop:around 定義AOP環(huán)繞通知 aop:aspect 定義一個切面 aop:aspectj-autoproxy 啟用@AspectJ注解驅(qū)動的切面 aop:before 定義一個AOP前置通知 aop:config 頂層的AOP配置元素。大多數(shù)的aop:*元素必須包含在aop:config元素內(nèi) aop:declare-parents 以透明的方式為被通知的對象引入額外的接口 aop:pointcut 定義一個切點

XML 配置文件中切點指示器

在XML配置文件中,切點指示器表達式與通過注解配置的寫法基本一致,區(qū)別前面有提到,即XML文件中需要使用 “and”、“or”、“not”來表示 “且”、“或”、“非”的關系。

XML 文件配置 AOP

新建OrderXmlAop.java:

package com.example.demo.aop; public class OrderXmlAop { /** * @description 在連接點執(zhí)行之前執(zhí)行的通知 */ public void doBefore(){ System.out.println('阿里阿塞喲!'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(返回通知和異常通知的異常) */ public void doAfter(){ System.out.println('after!'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(返回通知) */ public void doAfterReturning(){ System.out.println('返回通知:AfterReturning'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(異常通知) */ public void doAfterThrowing(){ System.out.println('異常通知:AfterThrowing'); }}

在 Resource 目錄下新建一個配置文件 aoporder.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'> <bean class='com.example.demo.service.impl.WMZServiceImpl'></bean> <bean class='com.example.demo.service.impl.ZSServiceImpl'></bean> <!-- 切面類 --> <bean class='com.example.demo.aop.OrderXmlAop'></bean> <!-- Aop配置 --> <aop:config proxy-target-class='true'> <!-- 切面 --> <aop:aspect ref='OrderXmlAop'> <!-- 前置通知: 在目標方法調(diào)用前執(zhí)行 --> <aop:before pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doBefore'/> <!-- 后置通知: --> <aop:after pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfter'/> <!-- 返回后通知 --> <aop:after-returning pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfterReturning'/> <!-- 異常通知 --> <aop:after-throwing pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfterThrowing'/> </aop:aspect> </aop:config></beans>

新建 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() { /** ** 注意 此處的getBean(name)中的name 必須要和aoporder.xml 配置的bean節(jié)點上的id 保持一致 * 如: <bean class='com.example.demo.service.impl.WMZServiceImpl'></bean> * TakeawayService wmzService=(TakeawayService)context.getBean('wmzService'); */ 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); }}

運行結果:

Spring boot AOP通過XML配置文件聲明的方法

聲明環(huán)繞通知

修改OrderXmlAop.java:

package com.example.demo.aop; import org.aspectj.lang.ProceedingJoinPoint; public class OrderXmlAop { /** * @description 在連接點執(zhí)行之前執(zhí)行的通知 */ public void doBefore(){ System.out.println('阿里阿塞喲!'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(返回通知和異常通知的異常) */ public void doAfter(){ System.out.println('after!'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(返回通知) */ public void doAfterReturning(){ System.out.println('返回通知:AfterReturning'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(異常通知) */ public void doAfterThrowing(){ System.out.println('異常通知:AfterThrowing'); } /** * @description 在連接點執(zhí)行之后執(zhí)行的通知(異常通知) */ public void doAround(ProceedingJoinPoint pj) { try { System.out.println('Around 調(diào)用方法前 '); pj.proceed(); System.out.println('Around 調(diào)用方法后'); } catch (Throwable throwable) { throwable.printStackTrace(); } }}

aoporder.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'> <bean class='com.example.demo.service.impl.WMZServiceImpl'></bean> <bean class='com.example.demo.service.impl.ZSServiceImpl'></bean> <!-- 切面類 --> <bean class='com.example.demo.aop.OrderXmlAop'></bean> <!-- Aop配置 --> <aop:config proxy-target-class='true'> <!-- 切面 --> <aop:aspect ref='OrderXmlAop'> <!-- 環(huán)繞通知 --> <aop:around pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAround'/> <!-- 前置通知: 在目標方法調(diào)用前執(zhí)行 --> <aop:before pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doBefore'/> <!-- 后置通知: --> <aop:after pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfter'/> <!-- 返回后通知 --> <aop:after-returning pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfterReturning'/> <!-- 異常通知 --> <aop:after-throwing pointcut='execution(public * com.example.demo.service.TakeawayService.*(..)))' method='doAfterThrowing'/> </aop:aspect> </aop:config></beans>

運行結果:

Spring boot AOP通過XML配置文件聲明的方法

結果和我們預期的一致,環(huán)繞通知通過xml配置成功。

XML 文件配置聲明切點

在上面的例子中,我們發(fā)現(xiàn)有切點表達式多次重復出現(xiàn),那么可不可以和aspectj配置一樣,單獨聲明切點,后面復用,答案是當然可以。如下修改aoporder.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'> <bean class='com.example.demo.service.impl.WMZServiceImpl'></bean> <bean class='com.example.demo.service.impl.ZSServiceImpl'></bean> <!-- 切面類 --> <bean class='com.example.demo.aop.OrderXmlAop'></bean> <!-- Aop配置 --> <aop:config proxy-target-class='true'> <!-- 切點 --> <aop:pointcut expression='execution(public * com.example.demo.service.TakeawayService.*(..)))'/> <!-- 切面 --> <aop:aspect ref='OrderXmlAop'> <!-- 環(huán)繞通知 --> <aop:around pointcut-ref='point' method='doAround'/> <!-- 前置通知: 在目標方法調(diào)用前執(zhí)行 --> <aop:before pointcut-ref='point' method='doBefore'/> <!-- 后置通知: --> <aop:after pointcut-ref='point' method='doAfter'/> <!-- 返回后通知 --> <aop:after-returning pointcut-ref='point' method='doAfterReturning'/> <!-- 異常通知 --> <aop:after-throwing pointcut-ref='point' method='doAfterThrowing'/> </aop:aspect> </aop:config></beans>

修改后執(zhí)行結果:

Spring boot AOP通過XML配置文件聲明的方法

XML文件配置為通知傳遞參數(shù)

修改OrderXmlAop.java

public String doAround(ProceedingJoinPoint pj,double price) { try { System.out.println('Around 調(diào)用方法前 '); pj.proceed(); if(price>=4396) { System.out.println('zs下單超過了4399,贈送一份鮮果飲匯源牌飲料'); return '爆漿牛丸和飲料'; } System.out.println('Around 調(diào)用方法后'); } catch (Throwable throwable) { throwable.printStackTrace(); } return '爆漿牛丸'; }

修改aoporder.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'> <bean class='com.example.demo.service.impl.WMZServiceImpl'></bean> <bean class='com.example.demo.service.impl.ZSServiceImpl'></bean> <!-- 切面類 --> <bean class='com.example.demo.aop.OrderXmlAop'></bean> <!-- Aop配置 --> <aop:config proxy-target-class='true'> <!-- 切點 --> <aop:pointcut expression='execution(com.example.demo.service.TakeawayService.Order(double)) and args(price) and bean(zsService)'/> <!-- 切面 --> <aop:aspect ref='OrderXmlAop'> <!-- 環(huán)繞通知 --> <aop:around pointcut-ref='point' method='doAround'/> </aop:aspect> </aop:config></beans>

總結

本文主要通過XML配置文件使用 Spring AOP進行編程,和上一篇的注解方式兩者聯(lián)系起來對于剛?cè)腴T的應該多多少少還是有點幫助的吧,針對于aop 通過三篇博客簡單的描述,相信大家對此都有點印象了,記錄了 AOP 的編程思想,然后介紹了 Spring 中 AOP 的相關概念,以及通過注解方式和XML配置文件兩種方式使用 Spring AOP進行編程。所以對aop的博文就簡單到這兒了,有人要問了,aop里面的代理啊還有各種各樣的,如果真要吧aop重頭到尾來一遍的話,這個系列可以單獨提出來一個專欄了,所以后面的博文應該都是圍繞連接數(shù)據(jù)庫,記錄日志,接入swagger文檔等功能相繼展開了。在此過程中,我有錯誤使用的地方,或者表達有問題,還請您及時告知,本人會在第一時間予以改正。最后在祝大家周末愉快,C Y L L

標簽: Spring
相關文章:
主站蜘蛛池模板: 成都办公室装修-办公室设计-写字楼装修设计-厂房装修-四川和信建筑装饰工程有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 石家庄网站建设|石家庄网站制作|石家庄小程序开发|石家庄微信开发|网站建设公司|网站制作公司|微信小程序开发|手机APP开发|软件开发 | ◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司 | 明渠式紫外线杀菌器-紫外线消毒器厂家-定州市优威环保 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 定制/定做衬衫厂家/公司-衬衫订做/订制价格/费用-北京圣达信 | 铝箔袋,铝箔袋厂家,东莞铝箔袋,防静电铝箔袋,防静电屏蔽袋,防静电真空袋,真空袋-东莞铭晋让您的产品与众不同 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 钢板仓,大型钢板仓,钢板库,大型钢板库,粉煤灰钢板仓,螺旋钢板仓,螺旋卷板仓,骨料钢板仓 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 华禹护栏|锌钢护栏_阳台护栏_护栏厂家-华禹专注阳台护栏、楼梯栏杆、百叶窗、空调架、基坑护栏、道路护栏等锌钢护栏产品的生产销售。 | 河南彩印编织袋,郑州饲料编织袋定制,肥料编织袋加工厂-盛军塑业 河南凯邦机械制造有限公司 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 科箭WMS仓库管理软件-TMS物流管理系统-科箭SaaS云服务 | 体坛网_体坛+_体坛周报新闻客户端 | 外观设计_设备外观设计_外观设计公司_产品外观设计_机械设备外观设计_东莞工业设计公司-意品深蓝 | 爱德华真空泵油/罗茨泵维修,爱发科-比其尔产品供应东莞/杭州/上海等全国各地 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 杭州双螺杆挤出机-百科| 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 雷蒙磨,雷蒙磨粉机,雷蒙磨机 - 巩义市大峪沟高峰机械厂 | 电伴热系统施工_仪表电伴热保温箱厂家_沃安电伴热管缆工业技术(济南)有限公司 | 【电子厂招聘_普工招工网_工厂招聘信息平台】-工立方打工网 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | 卫生人才网-中国专业的医疗卫生医学人才网招聘网站! | 在线PH计-氧化锆分析仪-在线浊度仪-在线溶氧仪- 无锡朝达 | 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | 企小优-企业数字化转型服务商_网络推广_网络推广公司 | 自动检重秤-动态称重机-重量分选秤-苏州金钻称重设备系统开发有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 山东限矩型液力偶合器_液力耦合器易熔塞厂家-淄博市汇川源机械厂 | 网站建设-临朐爱采购-抖音运营-山东兆通网络科技 | 除甲醛公司-甲醛检测-广西雅居环境科技有限公司 | 耐高温硅酸铝板-硅酸铝棉保温施工|亿欧建设工程 | 光伏家 - 太阳能光伏发电_分布式光伏发电_太阳能光伏网 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 余姚生活网_余姚论坛_余姚市综合门户网站 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 |