日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

使用BeanNameAutoProxyCreator實現(xiàn)spring的自動

系統(tǒng) 1907 0

提到代理,我們可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames實現(xiàn),但如果需要代理的bean很多,無疑會對spring配置文件的編寫帶來繁重的工作

Spring為我們提供了,根據(jù)beanName匹配后進行自動代理的解決方法

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

?

package ?AutoProxyOne;

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ù)實現(xiàn)類A,作為配置文件中的buyBean:

?

package ?AutoProxyOne;

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 ;
????}


}

?

?業(yè)務(wù)實現(xiàn)類B,作為配置文件中的sellBean:

?

package ?AutoProxyOne;

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 ?AutoProxyOne;

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

????}


}

?

配置文件:

其中beanNames為buy*,意味著所有以buy開頭的bean,都被spring容易自動代理,執(zhí)行相應(yīng)的切面通知

?

<? 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 ="AutoProxyOne.WelcomeAdvice" >
?
</ bean >
?
?
< bean?? class ="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
???
< property? name ="beanNames" >
?????
< list >
???????
< value > buy* </ value >
?????
</ list >
???
</ property >
???
< property? name ="interceptorNames" >
?????
< list >
????????
< value > WelcomeAdvice </ value >
?????
</ list > ?
???
</ property >

?
</ bean >
???
??
< bean? id ="buyBean" ?class ="AutoProxyOne.ShoppingImplA" >
????
< property? name ="customer" >
??????
< ref? bean ="customer" />
????
</ property >
???
</ bean >
?
< bean? id ="sellBean" ?class ="AutoProxyOne.ShoppingImplB" >
????
< property? name ="customer" >
??????
< ref? bean ="customer" />
????
</ property >
???
</ bean >


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


</ beans >

?

測試代碼:

在測試代碼中,我們的buyBean打印兩條買的信息,sellBean打印兩條賣的信息,可以看到buyBean執(zhí)行的方法已經(jīng)進行了切面處理

需要注意的是,如果使用自動代碼,則獲得Spring Bean工廠要用

ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);

而不能用

BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));

原因我想是因為BeanFactory在初始化時并不實例化單例的Bean,而ApplicationContext則在初始化時候全部實例化了Bean,自動代理需要在初始化時候定義好代理關(guān)系

?

package ?AutoProxyOne;

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;


import ?org.springframework.aop.support.RegexpMethodPointcutAdvisor;
public ? class ?TestAdvisor? ... {

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

????????String?filePath
= System.getProperty( " user.dir " ) + File.separator + " AutoProxyOne " + File.separator + " hello.xml " ;
????????
????????BeanFactory?factory
= new ?XmlBeanFactory( new ?FileSystemResource(filePath));
????????ApplicationContext?ctx
= new ?FileSystemXmlA

使用BeanNameAutoProxyCreator實現(xiàn)spring的自動代理


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 大余县| 隆子县| 桑日县| 额济纳旗| 昌江| 嫩江县| 济宁市| 澄城县| 红河县| 盐津县| 剑川县| 鹤峰县| 福海县| 江达县| 英山县| 盘山县| 彰化市| 永兴县| 兴化市| 四会市| 图们市| 遵义县| 凉山| 新安县| 靖安县| 贞丰县| 咸阳市| 临海市| 攀枝花市| 山阳县| 巫山县| 海原县| 中卫市| 若尔盖县| 万年县| 土默特右旗| 永安市| 封丘县| 沙田区| 韩城市| 兴隆县|