欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

使用DefaultAdvisorAutoProxyCreator實現spring

系統 2303 0

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

業務接口:

?

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);

}

?

業務實現類:

?

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? " );

????}


}

?

配置文件:

我們配置一個advisor,方法和在我的blog關于靜態切入點的用正則表達式配置切入點相同,這里匹配的是業務實現類中所有型如:***sell***的方法

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

重點在autoProxyCreator中,我們只需配置一個id和class,spring會自動幫我們解析advisor,并將通知進行切入

<? 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 >
?
?
<!-- ?自動代理所有的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 > ?? <!-- ?業務實現方法名匹配? -->
?????
</ 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 >

?

測試程序:

需要注意的是,和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實現spring的自動代理


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久草这里只有精品 | 国产精品99久久久久久动医院 | 国产成人福利视频在线观看 | 精品综合在线 | 天天做天天爱天天爽综合区 | 麻豆资源 | 香蕉久久一区二区不卡无毒影院 | 很黄很粗很湿很刺激的视频 | 欧美国产中文字幕 | 日韩欧美一区二区三区四区 | 91九色论坛| 婷婷精品国产一区二区三区日韩 | 九九热视频在线观看 | 成人在线中文字幕 | 99精品视频在线这里只有 | 亚洲精品二三区 | 天天色天天综合 | 新神奇四侠免费完整版在线观看 | 91精品久久| 国产在线精品一区二区三区 | 久久久久在线观看 | 精品视频在线播放 | 成人毛片免费视频播放 | 亚洲黄色激情 | 精品欧美乱码久久久久久1区2区 | 欧美一级毛片在线看视频 | a在线视频观看 | 性香港xxxxx免费视频播放 | 瑟瑟视频在线 | 91视频网国产 | 欧美日韩亚洲一区二区三区在线观看 | 欧美 日韩 中文字幕 | 中文乱码一二三四有限公司 | 亚州精品天堂中文字幕 | 午夜影皖普通区 | 久久大综合| jizzjizz视频 | 免费成人在线观看 | 国产精品久久久久无毒 | 三级视频网站 | 国产精品揄拍一区二区久久 |