一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring AOP攔截-三種方式實(shí)現(xiàn)自動(dòng)代理詳解

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

2021-02-02 11:38大學(xué)之旅_諳憶 Java教程

這篇文章主要介紹了Spring AOP攔截-三種方式實(shí)現(xiàn)自動(dòng)代理詳解,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。

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

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

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

Spring一個(gè)自動(dòng)代理類(lèi)DefaultAdvisorAutoProxyCreator:

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

例如:

原來(lái)不用自動(dòng)代理的配置文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!--?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">
  <!-- 代理前原對(duì)象 -->
  <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>
    <!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 -->
    <property name="advice">
      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>
    </property>
  </bean>
  <!-- 代理工廠 -->
  <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="personProxied">
    <!-- 放入原型對(duì)象 -->
    <property name="target" ref="person"></property>
    <!-- 放入切面 -->
    <property name="interceptorNames">
      <list>
        <value>advisor</value>
      </list>
    </property>
  </bean>
</beans>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<beans ...="">
<!-- 代理前原對(duì)象 -->
  <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>
    <!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 -->
    <property name="advice">
      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>
    </property>
  </bean>
 
  <!-- 自動(dòng)代理 -->
  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>

測(cè)試方法

?
1
2
3
4
5
6
7
8
@Test//自動(dòng)代理
  public void demo4(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/4.xml");
    //我們直接在這里獲取Person對(duì)象就可以了,因?yàn)樵谧铋_(kāi)始xml文件newPerson對(duì)象后,Spring就已經(jīng)幫我們代理了!
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

相對(duì)于前面,也就是把代理工廠部分換成自動(dòng)代理了。

演示結(jié)果:

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

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

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

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

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

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

通過(guò)這2個(gè)接口,我們就可以實(shí)現(xiàn)自動(dòng)代理了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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();
    //把原型對(duì)象放入代理工廠
    factory.setTarget(bean);
    //在這里
    Advisor adv = applicationContext.getBean(Advisor.class);
    factory.addAdvisor(adv);
    //返回被代理后的對(duì)象
    return factory.getObject();
  }
  //拿到原來(lái)的spring中的容器
  @Override
  public void setApplicationContext(ApplicationContext applicationContext)
      throws BeansException {
    this.applicationContext=applicationContext;
  }
}

5.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<beans...>
<!-- 代理前原對(duì)象 -->
  <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>
    <!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 -->
    <property name="advice">
      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>
    </property>
  </bean>
 
 
  <!-- 自己寫(xiě)的自動(dòng)代理 -->
  <bean class="cn.hncu.xmlImpl.MyAutoProxy"></bean>
</beans...>

測(cè)試方法:

?
1
2
3
4
5
6
7
@Test//自己實(shí)現(xiàn)的自動(dòng)代理
  public void demo5(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/5.xml");
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

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

其實(shí)很多時(shí)候,我們?nèi)绻约喝ゾ氁幌碌讓樱瑢?duì)上層的框架更好理解。

還有一種方法。

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

需要在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配置文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!--?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)自動(dòng)代理 -->
  </aop:aspectj-autoproxy>
 
  <!-- 代理前原對(duì)象 -->
  <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>
    <!-- 通知-由我們寫(xiě),實(shí)際代理動(dòng)作 -->
    <property name="advice">
      <bean class="cn.hncu.xmlImpl.AroundAdvice" id="advice"></bean>
    </property>
  </bean>
 
</beans>

測(cè)試方法:

?
1
2
3
4
5
6
7
@Test//自動(dòng)代理
  public void demo6(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/6.xml");
    Person p =ctx.getBean(Person.class);
    p.run();
    p.say();
  }

測(cè)試結(jié)果:

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

個(gè)人覺(jué)得能學(xué)會(huì)使用一種就OK了,不用全部記下來(lái),為了學(xué)習(xí),都了解一下就好,別人寫(xiě)出來(lái),能看懂就好。

哈哈,其實(shí)底層學(xué)好了,自己寫(xiě)的時(shí)候,就算不會(huì)用Spring的自動(dòng)代理,自己寫(xiě)出來(lái)底層也是蠻好的嘛

總結(jié)

以上本文關(guān)于Spring AOP攔截-三種方式實(shí)現(xiàn)自動(dòng)代理詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對(duì)本站的支持!

原文鏈接:https://www.2cto.com/kf/201609/545023.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无码人妻丰满熟妇啪啪网不卡 | 北条麻妃黑人 | 艾秋麻豆果冻传媒老狼仙踪林 | 亚洲 日韩 国产 制服 在线 | 好大好硬好深好爽想要之黄蓉 | 香蕉久久ac一区二区三区 | 国内精品91久久久久 | 亚洲性爱区 | 精品女同一区二区三区免费站 | 国产一区国产二区国产三区 | 成品人视频免费观看 | 2020年国产精品午夜福利在线观看 | 国内精品国语自产拍在线观看55 | 高清一区二区 | 日韩在线观看网站 | 莫莉瑞典1977k | 爱情岛论坛亚洲品质自拍视频 | 狠狠色96视频 | 国产精品久久久99 | 欧美日韩精品一区二区三区视频播放 | 99久久精品自在自看国产 | 国产精品酒店视频免费看 | 操破苍穹小说 | 91欧美国产| 99精彩视频在线观看 | 精品国产一级毛片大全 | 日韩在线免费 | 日韩欧美中文字幕一区二区三区 | 美女被无套进入 | 艾秋麻豆果冻剧传媒在线播放 | 青青青国产精品国产精品久久久久 | 久久噜国产精品拍拍拍拍 | 果冻传媒91 | 2019天天干天天操 | 美女脱了内裤让男生玩屁股 | 国产一区风间由美在线观看 | 日本黄色大片免费观看 | 久久囯产精品777蜜桃传媒 | 久久久久嫩草影院精品 | 性做久久久久久 | 国模大胆一区二区三区 |