欧美三区_成人在线免费观看视频_欧美极品少妇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條評論
主站蜘蛛池模板: 狠狠色噜噜狠狠狠狠米奇777 | 亚洲av毛片久久久久 | 国产女人成人精品视频 | 成年人网站在线免费观看 | 久久精品这里是免费国产 | 色欧美片视频在线观看 | 久草热线视频 | 日本中文在线观看 | www.com黄| 久久亚洲私人国产精品 | 成人亚洲网 | 日本成日本片人免费 | 日本高清在线观看视频www | 欧美日韩一区二区三在线 | 欧美国产成人一区二区三区 | 成人一级片 | 亚洲91在线| 2019中文字幕视频 | 精品欧美小视频在线观看 | 国模无水印一区二区三区 | 九九九久久久久久久爱 | 青娱乐综合网 | 欧美1区2区3区 | 午夜精品一区二区三区在线观看 | 国产精品手机在线 | 中国欧美日韩一区二区三区 | 99这里只有精品视频 | 好看的91视频 | 99精品免费视频 | 六月色婷 | 欧美卡一卡二卡新区网站 | 精品在线一区二区 | 国产肝交视频在线观看 | 午夜宫电影| 亚洲日韩色图 | 国产午夜精品一区二区三区在线观看 | 亚洲欧美综合久久 | 一级毛片看真人在线视频 | 欧美二区在线 | 欧美中文字幕 | 丝袜诱惑中文字幕 |