欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 69pao强力打造免费高清 | 欧美精品免费线视频观看视频 | 国产精品极品美女自在线看免费一区二区 | 欧美日批视频 | 91精品国产免费久久久久久 | 国产肝交视频在线观看 | 久久艹免费视频 | A片扒开双腿进入做视频 | 欧美 日韩 中文字幕 | 热久久久 | 好吊日免费视频 | 伦理午夜电影免费观看 | 欧美精品一区二区三区在线播放 | 日日操日日干 | a级欧美片免费观看 | 欧美日韩在线一区二区三区 | 狠狠色噜噜综合社区 | 国产精品18hdxxxⅹ在线 | 国产精品毛片大码女人 | 天天摸日日操 | 黄色免费网站在线观看 | 亚洲一区在线观 | 精品欧美乱码久久久久久 | 国产免费A片好硬好爽好深小说 | 日本又黄又粗暴的gif动态图含羞 | 一级爱一级做a性视频 | 日本黄色网址免费 | 欧美日韩国产三级 | 国产精品免费观看 | 草草免费观看视频在线 | 91精品国产色综合久久 | 五月婷婷久 | 亚洲国产日韩在线观看 | 欧美激情黄色 | 春色www视频在线观看 | 久久精品一区二区免费播放 | 可以看av的网站 | 看片亚洲 | 午夜久久久久久久久久一区二区 | 广西美女一级毛片 | 日韩大片在线永久观看视频网站免费 |