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

使用BeanNameAutoProxyCreator實現spring的自動

系統 1891 0

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

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

業務接口

?

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


}

?業務實現類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 ;
????}


}

?

?業務實現類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容易自動代理,執行相應的切面通知

?

<? 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執行的方法已經進行了切面處理

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

ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);

而不能用

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

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

?

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實現spring的自動代理


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产亚洲欧美成人久久片 | 黄色毛片网站 | 两个人高清视频图片中文字幕 | 99ri精品视频在线观看播放 | 日本大片免a费观看在线 | 天天干天天操天天爱 | 久久99精品亚洲热综合 | 野外一级毛片黑人 | 亚洲精品99久久久久久 | 久久99精品视免费看 | 欧美亚洲高清日韩成人 | 国自产拍在线天天更新91 | 奇米线在人线免费视频 | 国产欧美精品一区二区三区四区 | 日韩欧美在线观看成人 | 国产视频手机在线观看 | 97视频在线观看免费视频 | 亚洲激情网站 | 黄色影院在线观看视频 | 日本一级大黄毛片免费基地 | 国产成人视屏 | 999热在线精品观看全部 | 最新国产三级久久 | 成人国产亚洲欧美成人综合网 | 日本aⅴ永久免费网站www | 久久福利青草精品资源站免费 | 97se色综合一区二区二区 | 久久亚洲高清观看 | 日本精品二区 | 欧美在线视频在线观看 | 成人欧美视频在线观看 | 欧美激情一区 | 久久日韩精品激情 | 亚欧精品在线观看 | 欧美兽皇video | 99久久国产免费中文无字幕 | 精品国产日韩亚洲一区在线 | 国产毛片一级国语版 | 国产男女猛视频在线观看网站 | 好吊顶色 | 亚洲瑟瑟 |