当前位置: 首页 > news >正文

【框架】[Spring]AOP拦截-三种方式实现自动代理

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自【大学之旅_谙忆的博客】

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

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

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

用Spring一个自动代理类DefaultAdvisorAutoProxyCreator:

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

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

<?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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

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

    <!-- 代理工厂 -->
    <bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 放入原型对象 -->
        <property name="target" ref="person"></property>

        <!-- 放入切面 -->
        <property name="interceptorNames">
            <list>
                <value>advisor</value>
            </list>
        </property>
    </bean>


</beans>

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

<beans ...>
<!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!-- 切点 -->
        <property name="patterns">
            <list>
                <value>.*run.*</value>
            </list>
        </property>
        <!-- 通知-由我们写,实际代理动作 -->
        <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></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();
    }

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

演示结果:

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

我们也可以写一个类,来实现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;
    }

}

5.xml

<beans...>
<!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!-- 切点 -->
        <property name="patterns">
            <list>
                <value>.*run.*</value>
            </list>
        </property>
        <!-- 通知-由我们写,实际代理动作 -->
        <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></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="http://www.springframework.org/schema/aop"

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

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

我们需要一个aspectjweaver-jar包:
下载地址:
http://mvnrepository.com/artifact/org.aspectj

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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">
    <!-- 利用sop标签实现自动代理 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!-- 切点 -->
        <property name="patterns">
            <list>
                <value>.*run.*</value>
            </list>
        </property>
        <!-- 通知-由我们写,实际代理动作 -->
        <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></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();
    }

测试结果:

个人觉得能学会使用一种就OK了,不用全部记下来,为了学习,都了解一下就好,别人写出来,能看懂就好。
哈哈,其实底层学好了,自己写的时候,就算不会用Spring的自动代理,自己写出来底层也是蛮好的嘛

本文章由[谙忆]编写, 所有权利保留。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自【大学之旅_谙忆的博客】

相关文章:

  • Linux下,基于EETI触屏控制器的触屏失灵解决方法
  • jquery的height()和javascript的height总结,js获取屏幕高度
  • 搭建NPM私服
  • java自动关闭资源
  • Spring+SpringMvc+Mybatis框架集成搭建教程四(项目部署及测试)
  • JS继承之寄生类继承
  • protobuf-c的使用(二)使用
  • 音视频同步(播放)原理
  • AutoCAD 2014 新特性概览
  • jvm实例,tomcat容器,spring容器,在内存中的关系
  • uboot在s3c2440上的移植(4)
  • Xamarin调用谷歌地图以及百度地图api key申请时的SHA1值
  • Js 不支持函数的重载
  • 代理转发工具汇总分析
  • 删除Myeclipse中废弃的workspace记录
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • android 一些 utils
  • django开发-定时任务的使用
  • Git学习与使用心得(1)—— 初始化
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • JavaScript函数式编程(一)
  • Java知识点总结(JavaIO-打印流)
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Python学习之路13-记分
  • spring cloud gateway 源码解析(4)跨域问题处理
  • Spring框架之我见(三)——IOC、AOP
  • storm drpc实例
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • 看域名解析域名安全对SEO的影响
  • 算法---两个栈实现一个队列
  • 问题之ssh中Host key verification failed的解决
  • 一文看透浏览器架构
  • 一些css基础学习笔记
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • ​一些不规范的GTID使用场景
  • ​中南建设2022年半年报“韧”字当头,经营性现金流持续为正​
  • !!Dom4j 学习笔记
  • #我与Java虚拟机的故事#连载11: JVM学习之路
  • (LNMP) How To Install Linux, nginx, MySQL, PHP
  • (独孤九剑)--文件系统
  • (二十四)Flask之flask-session组件
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)ssm基于jsp的在线点餐系统 毕业设计 111016
  • (附源码)计算机毕业设计ssm高校《大学语文》课程作业在线管理系统
  • (附源码)计算机毕业设计ssm基于Internet快递柜管理系统
  • (亲测成功)在centos7.5上安装kvm,通过VNC远程连接并创建多台ubuntu虚拟机(ubuntu server版本)...
  • (三) diretfbrc详解
  • (四)图像的%2线性拉伸
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
  • (转)程序员技术练级攻略
  • (转载)Linux网络编程入门
  • ***监测系统的构建(chkrootkit )
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .Net 4.0并行库实用性演练