位置: IT常识 - 正文

Spring(十四):SpringAOP及AOP的三种实现方法(spring10)

编辑:rootadmin
一、什么是AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以 ...

推荐整理分享Spring(十四):SpringAOP及AOP的三种实现方法(spring10),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:spring 1,spring l,spring20134,spring20134,spring spring.,spring10,springv,spring20134,内容如对您有帮助,希望把文章链接给更多的朋友!

一、什么是AOP

AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

二、AOP的一些概念

1.Aspect(切面):切面是通知和切入点的结合。

2.Join point(连接点):与切入点匹配的执行点,例如执行方法或处理异常。在SpringAOP 中,连接点始终表示方法执行。

3.Advice(通知):在切面中需要完成的工作。

4.Pointcut(切入点):切入通知执行的位置。

5.Target(目标):被通知的对象。

6.proxy(代理):向目标对象应用通知之后创建的对象。

7.Introduction(引入):向现有类添加新的方法或属性。

8.Weaving(织入):将各个方面与其他应用程序类型或对象链接起来,以创建通知的对象。

三、AOP的三种实现方法

首先我们需要导入依赖:

     <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.9.1</version> </dependency>

1.通过SpringAPI接口进行实现

SpringAOP有五种通知方式,也有对应的接口:

Before:前置通知,在目标方法调用前通知,对应接口:MethodBeforeAdvice;

After:后置通知,在目标方法返回或异常后通知,对应接口:AfterReturningAdvice;

AfterReturning:后置返回通知,在目标方法返回后通知,对应接口:AfterReturningAdvice;

AfterThrowing:异常通知,在目标方法抛出异常后通知,对应接口:ThrowsAdvice;

Around:环绕通知:通知方法会将目标方法包裹起来,对应接口:MethodInterceptor;

Spring(十四):SpringAOP及AOP的三种实现方法(spring10)

我们下面以具体的例子来展示:

(1)定义一个接口

package com.jms.service;public interface UserService { void create(); void read(); void update(); void delete();}

(2)接口的实现类

package com.jms.service;public class UserServiceImpl implements UserService{ @Override public void create() { System.out.println("建立了一个用户信息"); } @Override public void read() { System.out.println("读取了一个用户信息"); } @Override public void update() { System.out.println("更新了一个用户信息"); } @Override public void delete() { System.out.println("删除了了一个用户信息"); }}

(3)建立两个类分别继承前置通知和后置返回通知的接口

package com.jms.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class beforeLog implements MethodBeforeAdvice { @Override //method:要执行目标对象的方法 //args:参数 //target:目标对象 public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行..."); }}package com.jms.log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class afterLog implements AfterReturningAdvice{ //returnValue:返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("[Debug]" + target.getClass().getName() + "的" + method.getName() + "执行完成,返回了" + returnValue); }}

(4)xml配置文件

注册bean,并且配置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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.jms.service.UserServiceImpl"/> <bean id="beforeLog" class="com.jms.log.beforeLog"/> <bean id="afterLog" class="com.jms.log.afterLog"/> <!--方法一:使用SpringAPI接口--> <!--aop配置--> <aop:config> <!--pointcut:切入点 expression:表达式 --> <aop:pointcut id="pointcut" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/> <!--执行环绕增加--> <aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config></beans>

(5)测试

@Test public void test1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); /* UserServiceImpl userService = applicationContext.getBean("userService", UserServiceImpl.class)这样执行会报错 因为动态代理代理的是接口,所以必须获取接口 */ UserService userService = applicationContext.getBean("userService", UserService.class); userService.create(); }

测试结果如下:

2.自定义类、自定义切面实现

接口以及实现类都与上面相同

(1)自定义切面类

package com.jms.diy;public class diyAspect { public void before() { System.out.println("[Debug]方法执行..."); } public void after() { System.out.println("[Debug]方法执行完成"); }}

切面类中自定义通知方法

(2)xml配置文件

注入切面类的Bean,配置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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="com.jms.service.UserServiceImpl"/> <!--方法二:自定义类,自定义切面--> <bean id="diyAspect" class="com.jms.diy.diyAspect"/> <aop:config> <!--自定义切面--> <aop:aspect ref="diyAspect"> <!--切入点--> <aop:pointcut id="pointcut1" expression="execution(* com.jms.service.UserServiceImpl.*(..))"/> <aop:after method="after" pointcut-ref="pointcut1"/> <aop:before method="before" pointcut-ref="pointcut1"/> </aop:aspect> </aop:config></beans>

(3)测试如下

3.通过注解实现

这种实现其实是第二种的注解方式

(1)自定义切面类

package com.jms.diy;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.stereotype.Component;@Aspect@Componentpublic class annotationAspect { @Before("execution(* com.jms.service.UserServiceImpl.*(..))") public void before() { System.out.println("[Debug]方法前置增强"); } @After("execution(* com.jms.service.UserServiceImpl.*(..))") public void after() { System.out.println("[Debug]方法后置增强"); }}

(2)此处还是采用xml配置,也可以采用java类配置

<?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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean id="userService" class="com.jms.service.UserServiceImpl"/> <!--方法三:注解--> <!--增加注解支持--> <context:annotation-config/> <context:component-scan base-package="com.jms"/> <aop:aspectj-autoproxy/></beans>

(3)测试

(本文仅作个人学习记录用,如有纰漏敬请指正)

本文链接地址:https://www.jiuchutong.com/zhishi/313186.html 转载请保留说明!

上一篇:使用VitePress搭建及部署vue组件库文档(virtono搭建教程)

下一篇:帝国伪静态Apache的Rewrite如何设置(帝国cms伪静态规则)

  • 残保金计入管理费用还是营业税金及附加
  • 公司注册认缴和实缴的区别
  • 市内打车费计入差旅费吗
  • 增值税减免所得税填在哪一栏
  • 鉴证服务的税率有哪些
  • 海关进口增值税专用缴款书如何抵扣
  • 资产减值损失结转到哪个科目
  • 坏账准备递延所得税资产怎么算
  • 合伙企业的合伙协议
  • 收到员工归还借款属于现金流量表
  • 科研项目间接费用会计分录
  • 公司缴纳个人所得税会计分录
  • 所得税汇算清缴调整项目
  • 将自建的厂房对外转让需要缴纳增值税吗
  • 汇算清缴补交所得税的账务处理
  • 怎么用手撕胶带图解
  • 通讯费税前扣除填报怎么填
  • 劳务费发票税率是多少
  • 小规模附加税减免政策2023
  • 结转成本计算方法
  • 小规模免增值税印花税用交吗
  • 公司对公账户每年费用
  • 税收滞纳金变化
  • 个人租赁汽车给公司怎么开发票
  • 存货跌价准备的金额
  • 注册公司需要注册资金吗
  • 工商年报中生育保险本期实际缴费金额
  • 间断性断网
  • 在卸载程序里找不到软件怎么卸载
  • 在win7系统中文件属性有哪些
  • PHP:pg_field_table()的用法_PostgreSQL函数
  • 增值税专用发票几个点
  • PHP:iconv_mime_decode_headers()的用法_iconv函数
  • 折旧提取后资金如何处理
  • php模板源码
  • 材料成本差异的会计分录
  • 如何查询数据库表空间
  • vue的mvvm模型
  • vue axios.all
  • 电子客票行程单怎么获取
  • 小微企业声明函去哪个部门开
  • 企业纳税人应缴的税额
  • 资产负债表所有者权益怎么算
  • 存货盘亏计入什么收入
  • 融资租赁首付租金会计账务处理
  • 百旺税控盘会自动清卡吗
  • 家电销售的税率
  • 信用减值损失和公允价值变动的区别
  • 什么是电子银行服务
  • 诉讼期间的利息法院支不支持
  • 筹建期间发生的所有费用
  • 进项税额转出多了怎么调整
  • 现金支付的范围包括
  • 公司员工报销车费还需要签协议吗
  • 集团对子公司的管理办法
  • 发票要不回来怎么办
  • 年化利率是什么意思
  • AppleMobileDeviceService.exe是什么进程?AppleMobileDeviceService.exe是病毒吗?
  • win8玩英雄联盟
  • 逼死win8的节奏 Win7升级至Win9仅需30美元 自动升级不需要重新安装
  • windows个性化定制包含
  • 图形工具的作用
  • linux 对比文件
  • centos操作命令
  • 忘记密码 ?
  • windowxp操作系统
  • mac如何查看隐藏的文件夹
  • win10自定义功能键
  • mac真好用
  • securecrt设置英文
  • win7右下角无线网络连接图标不见了
  • Node.js中的construct
  • nodejs事件循环和js事件循环
  • 如何利用百度地图定位
  • jquery日期控件 datepicker
  • python自动翻译小工具
  • windows中的linux
  • 供给决定需求,需求影响供给
  • 攸县丧葬
  • 金税盘和uk
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设