亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

使用DefaultAdvisorAutoProxyCreator實(shí)現(xiàn)spring

系統(tǒng) 2506 0

DefaultAdvisorAutoProxyCreator這個(gè)類功能更為強(qiáng)大,這個(gè)類的奇妙之處是他實(shí)現(xiàn)了BeanProcessor接口,當(dāng)ApplicationContext讀如所有的Bean配置信息后,這個(gè)類將掃描上下文,尋找所有的Advistor(一個(gè)Advisor是一個(gè)切入點(diǎn)和一個(gè)通知的組成),將這些Advisor應(yīng)用到所有符合切入點(diǎn)的Bean中

業(yè)務(wù)接口:

?

package ?StaticAdvisorTest;

public ? interface ?Shopping? ... {
??
public ?String?buySomething(String?type);
??
public ?String?buyAnything(String?type);
??
public ?String?sellSomething(String?type);
??
public ?String?sellAnything(String?type);

}

?

業(yè)務(wù)實(shí)現(xiàn)類:

?

package ?AutoProxyTwo;

public ? class ?ShoppingImplA? implements ?Shopping? ... {
????
private ?Customer?customer;
????
public ?Customer?getCustomer()? ... {
????????
return ?customer;
????}

????
public ? void ?setCustomer(Customer?customer)? ... {
????????
this .customer? = ?customer;
????}

????
public ?String?buySomething(String?type)? ... {
????????System.out.println(
this .getCustomer().getName() + " ?bye? " + type + " ?success " );
????????
return ? null ;
????}

????
????
public ?String?buyAnything(String?type)? ... {
???????System.out.println(
this .getCustomer().getName() + " ?bye? " + type + " ?success " );
???????
return ? null ;

?????}

????
public ?String?sellAnything(String?type)? ... {
????????System.out.println(
this .getCustomer().getName() + " ?sell? " + type + " ?success " );
????????
return ? null ;
????}

????
public ?String?sellSomething(String?type)? ... {
?????????System.out.println(
this .getCustomer().getName() + " ?sell? " + type + " ?success " );
???????????
return ? null ;
????}


}



package ?AutoProxyTwo;

public ? class ?ShoppingImplB? implements ?Shopping? ... {
????
private ?Customer?customer;
????
public ?Customer?getCustomer()? ... {
????????
return ?customer;
????}

????
public ? void ?setCustomer(Customer?customer)? ... {
????????
this .customer? = ?customer;
????}

????
public ?String?buySomething(String?type)? ... {
????????System.out.println(
this .getCustomer().getName() + " ?bye? " + type + " ?success " );
????????
return ? null ;
????}

????
????
public ?String?buyAnything(String?type)? ... {
???????System.out.println(
this .getCustomer().getName() + " ?bye? " + type + " ?success " );
???????
return ? null ;

?????}

????
public ?String?sellAnything(String?type)? ... {
????????System.out.println(
this .getCustomer().getName() + " ?sell? " + type + " ?success " );
????????
return ? null ;
????}

????
public ?String?sellSomething(String?type)? ... {
?????????System.out.println(
this .getCustomer().getName() + " ?sell? " + type + " ?success " );
???????????
return ? null ;
????}


}

?

?通知:

?

package ?AutoProxyTwo;

import ?java.lang.reflect.Method;

import ?org.springframework.aop.MethodBeforeAdvice;
// 前置通知
public ? class ?WelcomeAdvice? implements ?MethodBeforeAdvice? ... {

????
public ? void ?before(Method?method,?Object[]?args,?Object?obj)
????????????
throws ?Throwable? ... {
????????
????????System.out.println(
" Hello?welcome?to?bye? " );

????}


}

?

配置文件:

我們配置一個(gè)advisor,方法和在我的blog關(guān)于靜態(tài)切入點(diǎn)的用正則表達(dá)式配置切入點(diǎn)相同,這里匹配的是業(yè)務(wù)實(shí)現(xiàn)類中所有型如:***sell***的方法

buyBean和sellBean是最為普通的IOC配置

重點(diǎn)在autoProxyCreator中,我們只需配置一個(gè)id和class,spring會(huì)自動(dòng)幫我們解析advisor,并將通知進(jìn)行切入

<? xml?version="1.0"?encoding="UTF-8" ?>
<! DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd"? >
< beans >
?
< bean? id ="WelcomeAdvice" ?class ="AutoProxyTwo.WelcomeAdvice" >
?
</ bean >
?
?
<!-- ?自動(dòng)代理所有的advisor? -->
?
< bean? id ="autoProxyCreator" ?class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" >
?
</ bean >
???
???
???
< bean? id ="advisor" ?class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
?????
< property? name ="pattern" >
???????
< value > .*sell.+ </ value > ?? <!-- ?業(yè)務(wù)實(shí)現(xiàn)方法名匹配? -->
?????
</ property >
?????
< property? name ="advice" >
???????
< ref? bean ="WelcomeAdvice" />
?????
</ property >
???
</ bean >
???
???
??
< bean? id ="buyBean" ?class ="AutoProxyTwo.ShoppingImplA" >
????
< property? name ="customer" >
??????
< ref? bean ="customer" />
????
</ property >
???
</ bean >
??
< bean? id ="sellBean" ?class ="AutoProxyTwo.ShoppingImplB" >
????
< property? name ="customer" >
??????
< ref? bean ="customer" />
????
</ property >
???
</ bean >


< bean? id ="customer" ?class ="AutoProxyTwo.Customer" >
???
< constructor-arg? index ="0" >
?????
< value > gaoxiang </ value >
???
</ constructor-arg >
????
< constructor-arg? index ="1" >
?????
< value > 26 </ value >
???
</ constructor-arg >
?
</ bean >


</ beans >

?

測(cè)試程序:

需要注意的是,和BeanNameAutoProxyCreator相同,我們需要用ApplicationContext獲得Bean

package ?AutoProxyTwo;

import ?java.io.File;

import ?org.springframework.beans.factory.BeanFactory;
import ?org.springframework.beans.factory.xml.XmlBeanFactory;
import ?org.springframework.context.ApplicationContext;
import ?org.springframework.context.support.FileSystemXmlApplicationContext;
import ?org.springframework.core.io.FileSystemResource;
public ? class ?TestAdvisor? ... {

????
public ? static ? void ?main(String[]?args)? ... {

????????String?filePath
= System.getProperty( " user.dir " ) + File.separator + " AutoProxyTwo " + File.separator + " hello.xml " ;
????????
????????BeanFactory?factory
= new ?XmlBeanFactory( new ?FileSystemResource(filePath));
????????ApplicationContext?ctx
= new ?FileSystemXmlApplicationContext(filePath);
????????Shopping?shoppingA
= null ;
????????Shopping?shoppingB
= null

使用DefaultAdvisorAutoProxyCreator實(shí)現(xiàn)spring的自動(dòng)代理


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 亚洲精品一区二区三区福利 | 国产高清精品在线 | 97国产在线播放 | 777777农村一级毛片 | 91www成人久久| 久草在线新免费 | 美女黄频网站 | 亚洲第成色999久久网站 | 亚洲 欧美 国产另类首页 | 色综合免费视频 | 天天夜干 | 四虎影视免费在线观看 | 欧美午夜艳片欧美精品 | 欧美精品成人 | 久久2019| 色精品一区二区三区 | 欧美日韩在线网站 | 日韩精品中文字幕久久 | 美女黄色免费在线观看 | 久久99精品久久久久久水蜜桃 | 欧美 日产 国产精品 | 狠狠干天天 | 天天欲色成人综合网站 | 天天骑天天射 | 四虎www成人影院免费观看 | 四虎永久在线精品免费影视 | 真人实干一级毛片aa免费 | 日韩欧美色 | www深夜视频在线观看高清 | 日日狠狠 | 国产亚洲午夜精品a一区二区 | 亚洲国产一区二区三区四区 | 欧美精品啪啪 | 久草视频精品在线 | 久草在线视频资源站 | 牛牛影视成人午夜影视 | 99热影视| 久久r这里只有精品 | 久久久综合香蕉尹人综合网 | 中文字幕在线观看日韩 | 亚洲黄色视屏 |