iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Spring AOP中如何实现自动代理
  • 274
分享到

Spring AOP中如何实现自动代理

springaop 2023-05-30 22:05:00 274人浏览 泡泡鱼
摘要

小编给大家分享一下spring aop中如何实现自动代理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!这里的自动代理,我讲的是自动代理bean对象,其实就是在xm

小编给大家分享一下spring aop中如何实现自动代理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframework.aop.framework.ProxyFactoryBean的bean。

总结了一下自己目前所学的知识。

发现有三种方式实现自动代理

用Spring一个自动代理类DefaultAdvisorAutoProxyCreator:

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" data-filtered="filtered"></bean>

例如:

原来不用自动代理的配置文件如下:

<!--?xml version="1.0" encoding="UTF-8"?--><beans xmlns="https://www.springframework.org/schema/beans" xmlns:context="Https://www.springframework.org/schema/context" xmlns:tx="https://www.springframework.org/schema/tx" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.3.xsd        https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.3.xsd        https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  <!-- 代理前原对象 -->  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>  <!-- 切面 = 切点+通知 -->  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">    <!-- 切点 -->    <property name="patterns">      <list>        <value>.*run.*</value>      </list>    </property>    <!-- 通知-由我们写,实际代理动作 -->    <property name="advice">      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>    </property>  </bean>  <!-- 代理工厂 -->  <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="personProxied">    <!-- 放入原型对象 -->    <property name="target" ref="person"></property>    <!-- 放入切面 -->    <property name="interceptorNames">      <list>        <value>advisor</value>      </list>    </property>  </bean></beans>

现在改用自动代理,如下配置:

<beans ...=""><!-- 代理前原对象 -->  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>  <!-- 切面 = 切点+通知 -->  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">    <!-- 切点 -->    <property name="patterns">      <list>        <value>.*run.*</value>      </list>    </property>    <!-- 通知-由我们写,实际代理动作 -->    <property name="advice">      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>    </property>  </bean>   <!-- 自动代理 -->  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean></beans>

测试方法

@Test//自动代理  public void demo4(){    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/4.xml");    //我们直接在这里获取Person对象就可以了,因为在最开始xml文件newPerson对象后,Spring就已经帮我们代理了!    Person p =ctx.getBean(Person.class);    p.run();    p.say();  }

相对于前面,也就是把代理工厂部分换成自动代理了。

演示结果:

Spring AOP中如何实现自动代理

自己写一个自动代理底层实现:

我们也可以写一个类,来实现DefaultAdvisorAutoProxyCreator自动代理的功能!

首先,我们需要实现一个接口,也就是BeanPostProcessor接口。

BeanPostProcessor接口作用是:如果我们需要在Spring容器完成Bean的实例化、配置和其他的初始化前后添加一些自己的逻辑处理,我们就可以定义一个或者多个BeanPostProcessor接口的实现,然后注册到容器中。

而我们想要在原型对象bean被创建之后就代理了,就必须在原来的容器中拿到原来的原型对象,需要拿到原来spring容器中的切面对象,这个时候,我们就需要原来的容器,这个时候就需要另一个接口,也就是ApplicationContextAware接口!

通过这2个接口,我们就可以实现自动代理了。

package cn.hncu.xmlImpl;import org.springframework.aop.Advisor;import org.springframework.aop.framework.ProxyFactoryBean;import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class MyAutoProxy implements BeanPostProcessor,ApplicationContextAware{  private ApplicationContext applicationContext=null;  //bean创建之前调用  @Override  public Object postProcessBeforeInitialization(Object bean, String beanName)      throws BeansException {    return bean;//在这里,我们直接放行  }  //bean创建之后调用  @Override  public Object postProcessAfterInitialization(Object bean, String beanName)      throws BeansException {    ProxyFactoryBean factory = new ProxyFactoryBean();    //把原型对象放入代理工厂    factory.setTarget(bean);    //在这里    Advisor adv = applicationContext.getBean(Advisor.class);    factory.addAdvisor(adv);    //返回被代理后的对象    return factory.getObject();  }  //拿到原来的spring中的容器  @Override  public void setApplicationContext(ApplicationContext applicationContext)      throws BeansException {    this.applicationContext=applicationContext;  }}

xml

<beans...><!-- 代理前原对象 -->  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>   <!-- 切面 = 切点+通知 -->  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">    <!-- 切点 -->    <property name="patterns">      <list>        <value>.*run.*</value>      </list>    </property>    <!-- 通知-由我们写,实际代理动作 -->    <property name="advice">      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>    </property>  </bean>    <!-- 自己写的自动代理 -->  <bean class="cn.hncu.xmlImpl.MyAutoProxy"></bean></beans...>

测试方法:

@Test//自己实现的自动代理  public void demo5(){    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/5.xml");    Person p =ctx.getBean(Person.class);    p.run();    p.say();  }

测试结果就不上图了,和前面是一样的。

其实很多时候,我们如果自己去练一下底层,对上层的框架更好理解。

还有一种方法。

使用aop标签配自动代理

需要在beans加一个命名空间

xmlns:aop=https://www.springframework.org/schema/aop

还需要配xsi:schemaLocation,为aop加一个网络地址。

https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4.3.xsd

我们需要一个aspectjweaver-jar包:

xml配置文件:

<!--?xml version="1.0" encoding="UTF-8"?--><beans xmlns="https://www.springframework.org/schema/beans" xmlns:aop="https://www.springframework.org/schema/aop" xmlns:context="https://www.springframework.org/schema/context" xmlns:tx="https://www.springframework.org/schema/tx" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.3.xsd        https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.3.xsd        https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.3.xsd        https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">  <!-- 利用sop标签实现自动代理 -->  </aop:aspectj-autoproxy>   <!-- 代理前原对象 -->  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>   <!-- 切面 = 切点+通知 -->  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">    <!-- 切点 -->    <property name="patterns">      <list>        <value>.*run.*</value>      </list>    </property>    <!-- 通知-由我们写,实际代理动作 -->    <property name="advice">      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>    </property>  </bean> </beans>

测试方法:

@Test//自动代理  public void demo6(){    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/6.xml");    Person p =ctx.getBean(Person.class);    p.run();    p.say();  }

测试结果:

Spring AOP中如何实现自动代理

以上是“Spring AOP中如何实现自动代理”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网精选频道!

--结束END--

本文标题: Spring AOP中如何实现自动代理

本文链接: https://www.lsjlt.com/news/221680.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring AOP中如何实现自动代理
    小编给大家分享一下Spring AOP中如何实现自动代理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!这里的自动代理,我讲的是自动代理bean对象,其实就是在xm...
    99+
    2023-05-30
    spring aop
  • Spring-AOP自动创建代理之BeanNameAutoProxyCreator实例
    实例 代码已托管到Github—> https://github.com/yangshangwei/SpringMaster 在 Spring-AOP 静态普通方法名匹配...
    99+
    2024-04-02
  • Spring-AOP怎么自动创建代理
    本篇内容主要讲解“Spring-AOP怎么自动创建代理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring-AOP怎么自动创建代理”吧!实例代码已托管到Github—> https:...
    99+
    2023-06-20
  • Spring AOP原理及动态代理
    目录一、什么是代理1、静态代理2、动态代理二、模拟Spring AOP场景一、什么是代理 指为一个目标对象提供一个代理对象, 并由代理对象控制对目标对象的引用. 使用代理对象, 是为...
    99+
    2024-04-02
  • 使用Spring AOP 如何实现自定义注解
    这期内容当中小编将会给大家带来有关使用Spring AOP 如何实现自定义注解,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。在Maven中加入以下以依赖:<!-- Spring AOP + Aspe...
    99+
    2023-05-31
    springaop 注解
  • 如何在Spring框架中实现动态代理
    这篇文章给大家介绍如何在Spring框架中实现动态代理,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。动态代理,是一种通过运行时操作字节码,以达到增强类的功能的技术,也是Spring AOP操作的基础,关于AOP的内容,...
    99+
    2023-05-31
    spring 动态代理
  • 如何理解Spring AOP的实现机制
    这篇文章将为大家详细讲解有关如何理解Spring AOP的实现机制,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。AOP(Aspect Orient Programming),一般称为面向切面...
    99+
    2023-06-16
  • spring aop底层原理及如何实现
    目录前言 使用 源码分析 总结 前言 相信每天工作都要用spring框架的大家一定使用过spring aop,aop的概念是面向切面编程,相对与传统的面向对象编程oop,aop更关...
    99+
    2024-04-02
  • Spring源码如何实现动态代理
    小编给大家分享一下Spring源码如何实现动态代理,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!注:这里不阐述Spring和AOP的一些基本概念和用法,直接进入正题。流程  Spring所管理的对象大体会经过确定实例化对象...
    99+
    2023-06-14
  • Spring如何基于XML实现Aop
    本篇内容介绍了“Spring如何基于XML实现Aop”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!目录项目结构具体步骤创建maven 项目 ...
    99+
    2023-06-20
  • Spring aop 如何通过获取代理对象实现事务切换
    Spring aop 获取代理对象实现事务切换 在项目中,涉及到同一个类中一个方法调用另外一个方法,并且两个方法的事务不相关, 这里面涉及到一个事务切换的问题,一般的方法没问题,根据...
    99+
    2024-04-02
  • Spring如何实现自动装配
    这篇文章将为大家详细讲解有关Spring如何实现自动装配,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Spring之自动装配,所谓自动装配,从字面解读,我想应该不难理解吧(当然不是诱导读者去咬文嚼字)。那...
    99+
    2023-06-17
  • 动态代理模拟实现aop的示例
    AOP实现起来代码相当简单.主要核心是动态代理和反射.一.接口类:public interface MethodDao { public void sayHello(); }...
    99+
    2023-05-30
    动态代理模拟实现aop
  • 如何进行Spring源码剖析AOP实现原理
    今天就跟大家聊聊有关如何进行Spring源码剖析AOP实现原理,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。前言前面写了六篇文章详细地分析了Spring Bean加载流程,这部分完了...
    99+
    2023-06-02
  • spring boot拦截器如何使用spring AOP实现
    本篇文章为大家展示了spring boot拦截器如何使用spring AOP实现,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。在spring boot中,简单几步,使用spring AOP实现一个拦...
    99+
    2023-05-31
    springboot spring aop 拦截器
  • Spring AOP实现原理以及如何进行CGLIB应用
    本篇文章给大家分享的是有关Spring AOP实现原理以及如何进行CGLIB应用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。简介: AOP(Aspect Orien...
    99+
    2023-06-17
  • Spring中AOP概念与两种动态代理模式原理详解
    目录1.概念1.AOP技术简介2.AOP的优势3.Spring AOP术语4.AOP 开发明确的事项 2.AOP底层实现1.AOP 的动态代理技术:3.基于cglib的动态...
    99+
    2024-04-02
  • Spring AOP 实现自定义注解的示例
    目录1. 注解如下:2. 切面自工作后,除了一些小项目配置事务使用过 AOP,真正自己写 AOP 机会很少,另一方面在工作后还没有写过自定义注解,一直很好奇注解是怎么实现他想要的功能...
    99+
    2024-04-02
  • Spring Core动态代理的实现代码
    目录1.设计原理2.ProxyFactory (Spring-Core)2.1 JdkDynamicAopProxy2.2 CglibAopProxy2.3 主要源码部分1.设计原理...
    99+
    2024-04-02
  • SpringBoot/Spring AOP默认动态代理方式是什么
    这篇文章主要介绍“SpringBoot/Spring AOP默认动态代理方式是什么”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringBoot/Spring AOP默认动态...
    99+
    2023-07-05
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作