成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SpringAOP中如何實(shí)現(xiàn)自動代理

小編給大家分享一下Spring AOP中如何實(shí)現(xiàn)自動代理,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司是專業(yè)的常寧網(wǎng)站建設(shè)公司,常寧接單;提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行常寧網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

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

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

用Spring一個(gè)自動代理類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>
    <!-- 通知-由我們寫,實(shí)際代理動作 -->
    <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>
    <!-- 通知-由我們寫,實(shí)際代理動作 -->
    <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對象就可以了,因?yàn)樵谧铋_始xml文件newPerson對象后,Spring就已經(jīng)幫我們代理了!
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

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

演示結(jié)果:

Spring AOP中如何實(shí)現(xiàn)自動代理

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

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

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

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

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

通過這2個(gè)接口,我們就可以實(shí)現(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>
    <!-- 通知-由我們寫,實(shí)際代理動作 -->
    <property name="advice">
      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>
    </property>
  </bean>
 
 
  <!-- 自己寫的自動代理 -->
  <bean class="cn.hncu.xmlImpl.MyAutoProxy"></bean>
</beans...>

測試方法:

@Test//自己實(shí)現(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é)果就不上圖了,和前面是一樣的。

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

還有一種方法。

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

需要在beans加一個(gè)命名空間

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

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

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

我們需要一個(gè)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)簽實(shí)現(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>
    <!-- 通知-由我們寫,實(shí)際代理動作 -->
    <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é)果:

Spring AOP中如何實(shí)現(xiàn)自動代理

以上是“Spring AOP中如何實(shí)現(xiàn)自動代理”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站標(biāo)題:SpringAOP中如何實(shí)現(xiàn)自動代理
URL分享:http://jinyejixie.com/article24/pocjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、標(biāo)簽優(yōu)化、小程序開發(fā)網(wǎng)站內(nèi)鏈、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
海宁市| 日土县| 夏邑县| 梨树县| 元阳县| 古蔺县| 潜江市| 延边| 建宁县| 三穗县| 文水县| 宝兴县| 沧源| 英山县| 荆州市| 湘潭县| 商洛市| 习水县| 敦煌市| 临沧市| 明水县| 明光市| 藁城市| 定西市| 林芝县| 平顶山市| 南召县| 讷河市| 会宁县| 庐江县| 西安市| 榕江县| 昌图县| 平邑县| 双辽市| 台江县| 湘西| 黑山县| 枞阳县| 资兴市| 泰安市|