首页 > 其他分享 >基于基于XML配置方式声明切面

基于基于XML配置方式声明切面

时间:2023-04-27 22:04:07浏览次数:30  
标签:XML 基于 void jar System 切面 println public out


知识点:

//普通的java 类 


public class LogPrint { 

 public void doAccessCheck() {}定义前置通知 

 public void doReturnCheck() {}定义后置通知 

 public void doExceptionAction() {}定义例外通知 

 public void doReleaseAction() {}定义最终通知 

//环绕通知必须要有ProceedingJoinPoint pjp 参数和调用pjp.proceed();方法 

 public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { 

 return pjp.proceed();环绕通知 

 } 

} 


xml文件的配置: 


<bean id="orderservice" class="cn.itcast.service.OrderServiceBean"/> 

<bean id="log" class="cn.itcast.service.LogPrint"/> 

<aop:config> 

 <aop:aspect id="myaop" ref="log"> 

 <aop:pointcut id="mycut" expression="execution(* cn.itcast.service..*.*(..))"/> 

 <aop:before pointcut-ref="mycut" method="doAccessCheck"/> 

 <aop:after-returning pointcut-ref="mycut" method="doReturnCheck "/> 

 <aop:after-throwing pointcut-ref="mycut" method="doExceptionAction"/> 

 <aop:after pointcut-ref="mycut" method=“doReleaseAction"/> 

 <aop:around pointcut-ref="mycut" method="doBasicProfiling"/> 

 </aop:aspect> 

</aop:config> 



】 

准备工作: 

【 

dist\spring.jar 

lib\jakarta-commons\commons-logging.jar 

如果使用了切面编程(AOP),还需要下列jar文件 

lib/aspectj/aspectjweaver.jar和aspectjrt.jar 

lib/cglib/cglib-nodep-2.1_3.jar 



】 


照样实现步骤如下: 


第一步:编写业务层代码 


PersonServiceBean。java类 IPersonServiceBean。java接口 


public interface IPersonServiceBean { 


 public abstract void save(String name); 


 public abstract String update(String name); 


} 



public class PersonServiceBean implements IPersonServiceBean { 


 public void save(String name) 

 { 

 throw new IllegalArgumentException("抛出异常"); 

// System.out.println("save is invoke"); 

 } 


 public String update(String name) 

 { 


 System.out.println("update is invoke"); 

 return "Sueccess"; 

 } 

} 


第二步: 编写拦截器类代码 (这里是基于xml配置文件进行拦截) 

public class MyItercepterByXml { 


 public void doAccessCheck() { 

 // 在执行拦截方法前调用可得到输入参数 

 System.out.println("exctution 前置通知"); 

 } 


 public void doReturnCheck() { 

 // 在执行拦截方法后调用可得到返回参数 

 System.out.println("exctution 后置通知"); 

 } 


 public void doExceptionAction() { 

 System.out.println("exctution 异常通知"); 

 } 


 public void doReleaseAction() { 

 System.out.println("exctution 最终通知"); 

 } 


 public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { 

 System.out.println("exctution 开始环绕测试"); 

 // 必须调用下面的方法 

 Object object = pjp.proceed(); 

 System.out.println("exctution 结束环绕测试"); 

 return object; 

 } 


} 


第三步:编写配置文件(这里使用xml文件把bean交个spring管理也可以同个类路径扫描) 


<?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:context="http://www.springframework.org/schema/context" 

 xmlns:aop="http://www.springframework.org/schema/aop" 

 xsi:schemaLocation="http://www.springframework.org/schema/beans 

 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 

 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 

 <aop:aspectj-autoproxy/> 


 <bean id="personService" class="com.liyong.serviceBean.Imp.PersonServiceBean"/> 

 <!-- <bean id="myItercepter" class="com.liyong.Itecepter.MyItercepter"/> --> 

 <bean id="myItercepter" class="com.liyong.Itecepter.MyItercepterByXml"/> 

 <aop:config> 

 <aop:aspect id="myaspect" ref="myItercepter"> 

 <aop:pointcut id="myAnyMethod" expression="execution (* com.liyong.serviceBean.Imp.PersonServiceBean.*(..))"/> 

 <!-- 

 注意:不要写成下面的形式 

 <aop:before pointcut="myAnyMethod" method="doAccessCheck"/> 

 --> 

 <aop:before pointcut-ref="myAnyMethod" method="doAccessCheck"/> 

 <aop:after-throwing pointcut-ref="myAnyMethod" method="doExceptionAction"/> 

 <aop:after-returning pointcut-ref="myAnyMethod" method="doReturnCheck"/> 

 <aop:after pointcut-ref="myAnyMethod" method="doReleaseAction"/> 

 <aop:around pointcut-ref="myAnyMethod" method="doBasicProfiling"/> 

 </aop:aspect> 

 </aop:config> 

</beans> 


第四步:编写单元测试 


public class JunitTest { 

 @Test 

 public void TestAOP() 

 { 

 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 

 IPersonServiceBean personServiceBean =(IPersonServiceBean)context.getBean("personService"); 

// personServiceBean.save("liyong"); 

 personServiceBean.update("xxx"); 

 } 


} 


第五步:运行单元测试 


..................

标签:XML,基于,void,jar,System,切面,println,public,out
From: https://blog.51cto.com/u_16091571/6232144

相关文章

  • 使用Spring进行面向切面(AOP)编程
    基础知识:【要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework......
  • 编程用JAVA解析XML的方式
    用SAX方式解析XML,XML文件如下:<?xmlversion="1.0"encoding="gb2312"?><person><name>王小明</name><college>信息学院</college><telephone>6258113</telephone><notes>男,1955年生,博士,95年调入海南大学......
  • 基于chatGPT的问答机器人开发-qdrant向量数据库的集合功能封装
    之前一直再开发基于GPT的问答知识库机器人,主要是靠qdrant的向量搜索,搜索出相关的条目,然后发送给GPT回答qdrant向量数据库有集合的概念,相当于表,每个集合里面可以创建多个向量数据。那么针对集合的操作就是下面这样的,集合列表,创建集合,删除集合 集合列表接口:http://127.0.0.1:80......
  • 基于Linux系统的PXE搭建方法
    本文分享自天翼云开发者社区《基于Linux系统的PXE搭建方法》,作者:t***n 一、底层环境准备1、安装RedHat7.6系统2、关闭防火墙和Selinuxsystemctlstopfirewalldchkconfigfirewalldoffvim/etc/sysconfig/selinux    修改SELINUX=disabled3、配置本地yum源vim/e......
  • 基于ISO 21434的汽车网络安全实践
    “转载自维克多汽车技术(上海)有限公司,作者VectorChina”商业领域的IT系统和嵌入式产品的IT系统正在融合为一种多功能系统。相应地,关注汽车网络安全的ISO21434标准应运而生。该标准的意义在于提供了一个指南,可用于降低产品、项目和组织中存在的安全风险。为了有效实施ISO21434标......
  • 从零开始基于Archlinux 安装 containerd + k8s
    下载ISO文件:https://mirrors.tuna.tsinghua.edu.cn/archlinux/iso/latest/目录1.准备工作2.磁盘管理2.1磁盘分区2.2磁盘格式化2.3磁盘挂载3.安装系统3.1安装系统文件3.2配置fstab3.3配置系统3.4安装引导程序3.5安装OpenSSH3.6主机名3.7设置root密码3.8网络配置3.......
  • 基于C++的AI贪吃蛇
    访问【WRITE-BUG数字空间】_[内附完整源码和文档]用C++做了个有AI功能的贪吃蛇小游戏,希望大家enjoyit.总体概况开发环境:VIsualStudio2017开发语言:C++和少许WindowsAPI运行环境:Windows1001初始化工作-游戏设置游戏设置和相关初始化放在了一个类里面,并进行了静态声明。主要设......
  • 基于Java开发支持全文检索、工作流审批、知识图谱的应用系统
    一、项目介绍一款全源码,可二开,可基于云部署、私有部署的企业级知识库云平台,应用在需要进行常用文档整理、分类、归集、检索的地方,适合知识密集型单位/历史文档丰富的单位,或者大型企业、集团。为什么建立知识库平台?二、项目所用技术springboot+vue+tinyMce+activiti+elastics......
  • SpringBoot SpringSecurity 介绍(基于内存的验证)
    SpringBoot集成SpringSecurity+MySQL+JWT附源码,废话不多直接盘SpringBoot已经为用户采用默认配置,只需要引入pom依赖就能快速启动SpringSecurity。目的:验证请求用户的身份,提供安全访问优势:基于Spring,配置方便,减少大量代码内置访问控制方法permitAll()表示所匹配的......
  • m十字路口多功能控制交通系统,包括基于遗传算法优化的红绿灯时长模糊控制器和基于BP神
    1.算法仿真效果matlab2022a仿真结果如下:        2.算法涉及理论知识概要单十字路口:           其中第一级控制为两个并行模块:绿灯交通强度控制模块与红灯交通强度控制模块。绿灯交通强度控制模块的输入为绿灯相位的排队长度与入口流量,......