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

使用BeanNameAutoProxyCreator實現spring的自動

系統 1731 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條評論
主站蜘蛛池模板: 97超碰免费| 97久久精品午夜一区二区 | 99精品国产在热久久 | 色综合久久天天综合网 | 青青在线香蕉精品视频免费看 | 久久99热久久精品在线6 | v片在线看 | 青青草原在线视频免费观看 | 99在线精品视频在线观看 | 亚洲精品美女久久久 | 欧美日韩视频在线 | 国产伊人网 | 久久亚洲成人网 | 国产精品亚洲一区 | 日韩欧美h | 久久精品国产欧美成人 | 色噜噜狠狠色综合欧洲 | 91短视频在线免费观看 | a级毛片免费高清视频 | 999精品视频在线观看 | 亚洲视频在线视频 | 宣言个人资料 | 久久一区| 欧美成人三级一区二区在线观看 | 国产第一页浮力 | 精品国产一区二区三区免费 | 国产伊人精品 | 啪啪伊人网| 免费乱理伦片在线观看八戒 | 国产福利视频在线 | 国产成人精品影院狼色在线 | 久久99国产精品成人欧美 | 国产成人免费全部网站 | 亚洲欧美自拍另类图片色 | 男女激情网址 | 欧美精品一区二区三区在线 | 欧美老妇交乱视频 | 免费中日高清无专码有限公司 | 好吊在线 | 亚洲综合色视频在线观看 | 国产三级做爰在线观看∵ |