位置: 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伪静态规则)

  • 企业自建房产缴契税吗
  • 已经认证抵扣的发票还能作废吗
  • 小区业委会是否可以进行经营活动?
  • 发票领购簿长什么样
  • 计提社保贷方科目是什么
  • 已过期增值税专票怎么开
  • 基金投资债券会赔吗
  • 付款单是发票吗
  • 水利建设工程
  • 剩余材料入库的会计分录怎么做?
  • 个人所得税免征项目有哪些
  • 业务宣传费和广告费有什么区别
  • 财政补助结转余额在借方还是贷方
  • 卷烟消费税税率表
  • 受托加工物资产生的成本怎么做会计核算?
  • 应收账款 转让
  • 自有房屋的装修费计入长期待摊
  • 园林绿化工程公司电话
  • 房地产预缴所得税的计税基础
  • 政府专项基金是什么
  • ae应用程序无法启动
  • 股权转让有哪些股
  • 电脑右键一直转圈
  • php二维数组的遍历
  • 其他应收款和其他应付款常常被用作抵消
  • 融资租赁怎么做到表外
  • 供热企业有哪些
  • php中删除文件的函数
  • anaconda3.5.2安装教程
  • JavaScript(WebAPI)+具体案例
  • 没有计提坏账如何披露
  • 长期待摊费用装修费摊销年限
  • java 泛型方法
  • 汇算清缴有问题怎么办
  • python中的count函数
  • 缴纳税款滞纳金会计处理
  • 异地提供建筑服务预缴
  • 个体户转账到法人账户要交税吗
  • 大公司要求小规模公司交税
  • 税控盘怎么看是否清盘
  • 一次性发放年终奖金怎么扣除个人所得税144000有何意义
  • db2profile
  • 发表文章开什么发票
  • 固定资产出售账面价值计入什么科目
  • 同一张发票报销两次会被发现吗
  • 企业购进的固定资产
  • 公摊水电费计入什么科目
  • 公益性捐赠全额扣除,企业所得税
  • 汽车三产件
  • 残保金工资总额是按计提还是发放
  • 先计提所得税还是先出报表
  • 委托收款的流程
  • 应收账款如何记账
  • 建筑企业人工费计入什么科目
  • 怎么做待摊费用
  • mysql使用语句
  • win2003加速开机
  • linux如何替换
  • shell多进程并行返回值
  • 苹果Mac OS X 10.13系统怎么调整设置窗口透明度?
  • mom.exe是什么程序
  • js观察者模式和发布订阅
  • python怎么输出日志
  • unity平移场景视图怎样操作?
  • ReactNative之FlatList的具体使用方法
  • linux 映射 fn键
  • 自制u盘杀手
  • node 连接sqlserver
  • javascript cookies
  • 安卓api中文手册
  • python颜色表
  • javascripvoid
  • 如何python
  • 3、BluetoothChat之BluetoothChatService.java
  • 对税务机关的意见建议怎么写
  • 计算消费税为什么要除1-比例税率
  • 黑龙江国税电子税务局官网登录
  • 车辆购置税纳税申报表下载
  • 买高档手表
  • 预缴的税额可以用留底抵扣吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设