最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring AOP攔截-三種方式實現(xiàn)自動代理詳解

 更新時間:2017年11月09日 14:37:30   作者:大學(xué)之旅_諳憶  
這篇文章主要介紹了Spring AOP攔截-三種方式實現(xiàn)自動代理詳解,還是比較不錯的,這里分享給大家,供需要的朋友參考。

這里的自動代理,我講的是自動代理bean對象,其實就是在xml中讓我們不用配置代理工廠,也就是不用配置class為org.springframework.aop.framework.ProxyFactoryBean的bean。

總結(jié)了一下自己目前所學(xué)的知識。

發(fā)現(xiàn)有三種方式實現(xiàn)自動代理

用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>
  <!-- 切面 = 切點(diǎn)+通知 -->
  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">
    <!-- 切點(diǎn) -->
    <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>

現(xiàn)在改用自動代理,如下配置:

<beans ...="">
<!-- 代理前原對象 -->
  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>
  <!-- 切面 = 切點(diǎn)+通知 -->
  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">
    <!-- 切點(diǎn) -->
    <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就已經(jīng)幫我們代理了!
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

相對于前面,也就是把代理工廠部分換成自動代理了。

演示結(jié)果:

自己寫一個自動代理底層實現(xiàn):

我們也可以寫一個類,來實現(xiàn)DefaultAdvisorAutoProxyCreator自動代理的功能!

首先,我們需要實現(xiàn)一個接口,也就是BeanPostProcessor接口。

BeanPostProcessor接口作用是:如果我們需要在Spring容器完成Bean的實例化、配置和其他的初始化前后添加一些自己的邏輯處理,我們就可以定義一個或者多個BeanPostProcessor接口的實現(xiàn),然后注冊到容器中。

而我們想要在原型對象bean被創(chuàng)建之后就代理了,就必須在原來的容器中拿到原來的原型對象,需要拿到原來spring容器中的切面對象,這個時候,我們就需要原來的容器,這個時候就需要另一個接口,也就是ApplicationContextAware接口!

通過這2個接口,我們就可以實現(xiàn)自動代理了。

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創(chuàng)建之前調(diào)用
  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName)
      throws BeansException {
    return bean;//在這里,我們直接放行
  }
  //bean創(chuàng)建之后調(diào)用
  @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 class="cn.hncu.xmlImpl.Person" id="person"></bean>
 
  <!-- 切面 = 切點(diǎn)+通知 -->
  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">
    <!-- 切點(diǎn) -->
    <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//自己實現(xiàn)的自動代理
  public void demo5(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/5.xml");
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

測試結(jié)果就不上圖了,和前面是一樣的。

其實很多時候,我們?nèi)绻约喝ゾ氁幌碌讓?,對上層的框架更好理解?/p>

還有一種方法。

使用aop標(biāo)簽配自動代理

需要在beans加一個命名空間

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

還需要配xsi:schemaLocation,為aop加一個網(wǎng)絡(luò)地址。

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標(biāo)簽實現(xiàn)自動代理 -->
  </aop:aspectj-autoproxy>
 
  <!-- 代理前原對象 -->
  <bean class="cn.hncu.xmlImpl.Person" id="person"></bean>
 
  <!-- 切面 = 切點(diǎn)+通知 -->
  <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="advisor">
    <!-- 切點(diǎn) -->
    <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();
  }

測試結(jié)果:

個人覺得能學(xué)會使用一種就OK了,不用全部記下來,為了學(xué)習(xí),都了解一下就好,別人寫出來,能看懂就好。

哈哈,其實底層學(xué)好了,自己寫的時候,就算不會用Spring的自動代理,自己寫出來底層也是蠻好的嘛

總結(jié)

以上本文關(guān)于Spring AOP攔截-三種方式實現(xiàn)自動代理詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:快速理解Java設(shè)計模式中的組合模式、Java編程接口調(diào)用的作用及代碼分享、淺談Java實體對象的三種狀態(tài)以及轉(zhuǎn)換關(guān)系等,有什么問題可以隨時留言,小編會及時回復(fù)大家的。感謝朋友們對本站的支持!

相關(guān)文章

  • java常用的加密解決方案分享

    java常用的加密解決方案分享

    這篇文章全面介紹了Java中加解密技術(shù)的應(yīng)用,包括哈希函數(shù)、對稱加密、非對稱加密、消息認(rèn)證碼和數(shù)字簽名等,它詳細(xì)解釋了每種技術(shù)的工作原理,并提供了相應(yīng)的Java代碼示例,文章還強(qiáng)調(diào)了密鑰管理的重要性,并提出了在實際應(yīng)用中遵循的最佳實踐
    2025-01-01
  • Java實現(xiàn)的迷宮游戲

    Java實現(xiàn)的迷宮游戲

    這篇文章主要介紹了如何用Java實現(xiàn)一個迷宮游戲,本倉庫代碼是經(jīng)過eclipse編譯運(yùn)行過的,一般情況下將本倉庫代碼下載下來之后,使用eclipse編譯直接可以運(yùn)行。
    2021-04-04
  • Mybatis實現(xiàn)批量操作8種小結(jié)

    Mybatis實現(xiàn)批量操作8種小結(jié)

    本文對Mybatis的五種批處理方式進(jìn)行了性能測試,包括批量新增和批量修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • Java反射機(jī)制的講解

    Java反射機(jī)制的講解

    今天小編就為大家分享一篇關(guān)于Java反射機(jī)制的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • Java基礎(chǔ)知識精通注釋與數(shù)據(jù)類型及常量與變量

    Java基礎(chǔ)知識精通注釋與數(shù)據(jù)類型及常量與變量

    本文給大家介紹了Java的注釋與數(shù)據(jù)類型和常量變量,這些都是最基礎(chǔ)的知識,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • java中獲取類資源的方法總結(jié)

    java中獲取類資源的方法總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于java中獲取類資源的方法總結(jié),需要的朋友們可以學(xué)習(xí)參考下。
    2020-02-02
  • Java線程池必知必會知識點(diǎn)總結(jié)

    Java線程池必知必會知識點(diǎn)總結(jié)

    這篇文章主要給大家介紹了關(guān)于Java線程池必知必會知識點(diǎn)的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02
  • Java8中新判空方法之Optional類的使用詳解

    Java8中新判空方法之Optional類的使用詳解

    Opitonal類就是Java提供的為了解決大家平時判斷對象是否為空用的。本文將通過示例為大家講解一下Optional類的使用,感興趣的可以收藏一下
    2022-12-12
  • java實現(xiàn)字符串轉(zhuǎn)String數(shù)組的方法示例

    java實現(xiàn)字符串轉(zhuǎn)String數(shù)組的方法示例

    這篇文章主要介紹了java實現(xiàn)字符串轉(zhuǎn)String數(shù)組的方法,涉及java字符串的遍歷、分割、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • SpringBoot?整合?Elasticsearch?實現(xiàn)海量級數(shù)據(jù)搜索功能

    SpringBoot?整合?Elasticsearch?實現(xiàn)海量級數(shù)據(jù)搜索功能

    這篇文章主要介紹了SpringBoot?整合?Elasticsearch?實現(xiàn)海量級數(shù)據(jù)搜索,本文主要圍繞?SpringBoot?整合?ElasticSearch?接受數(shù)據(jù)的插入和搜索使用技巧,在實際的使用過程中,版本號尤其的重要,不同版本的?es,對應(yīng)的?api?是不一樣,需要的朋友可以參考下
    2022-07-07

最新評論

天气| 凤山市| 盐城市| 鄄城县| 沧州市| 兴和县| 蓝田县| 巨野县| 德庆县| 永新县| 安庆市| 舒城县| 宜良县| 上林县| 正阳县| 建平县| 湾仔区| 东光县| 锡林郭勒盟| 怀来县| 宜都市| 武安市| 梨树县| 当涂县| 墨江| 山东| 城市| 宁阳县| 句容市| 瓦房店市| 马关县| 淳化县| 安义县| 石台县| 惠东县| 望都县| 清徐县| 色达县| 文水县| 筠连县| 五峰|