五大"集合"的注入方式與SpringAOP的兩種利用XML文件的動態(tài)代理方式

#千鋒#

一.五大"集合"的注入方式

都是利用xml文件進行注入:

1.數(shù)組 arrays

<property name="arrays">

????<array>

????????<value>hjx</value>

????????<value>hjx</value><!--允許重復-->

? ? ? ? <value>zzy</value>

????????<value>dl</value>

????????<value>sc</value>

????????<value>shx</value>

????</array>

</property>

2.List集合

<property name="list">

????<list>

????????<value>zhugedali</value>

????????<value>chengguo</value>

????????<value>chengguo</value><!--允許重復-->

? ? ? ? <ref bean="obj"/><!--放對象的方式-->

? ? ? ? <ref bean="stu"/>

????</list>

</property>

3.Set集合

<property name="set">

????<set>

????????<value>suk</value>

????????<value>suk</value><!--不允許重復-->

? ? ? ? <value>zbj</value>

????????<value>tx</value>

????????<value>shs</value>

????</set>

</property>

4.Map集合

<property name="map">

????????<map>

????????<entry key="jack" value="杰克"/>

????????<entry key="jerry" value="杰瑞"/>

????????<entry key="tom" value="湯姆"/>

????</map>

</property>

5.Properties:就是使用連接池時使用的,以 key=value的形式存值

<property name="prop">

????<props>

????????<prop key="url">jdbc:mariadb://localhost:3306/mydb</prop>

????????<prop key="driver">org.mariadb.jdbc.Driver</prop>

????????<prop key="userName">root</prop>

????????<prop key="password">hjx123</prop>

????</props>

</property>

二.SpringAOP的兩種利用xml文件配置動態(tài)代理

第一種:可以通過設(shè)置aop切點里的參數(shù)來指定返回類型,指定包,指定類,指定方法,指定參數(shù)列表來進行代理

1.先對bean.xml文件進行處理,將beans.xml里的beans頭部改為:

<beans xmlns="http://www.springframework.org/schema/beans"

? ? ? ???????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

? ? ? ???????? xmlns:aop="http://www.springframework.org/schema/aop"

? ? ? ???????? xsi:schemaLocation="http://www.springframework.org/schema/beans

????????????????http://www.springframework.org/schema/beans/spring-beans.xsd

? ? ? ? ? ? ? ? http://www.springframework.org/schema/aop

? ? ? ? ? ? ? ? ?http://www.springframework.org/schema/aop/spring-aop.xsd">

2.在beans內(nèi)部先加上:

<bean id="us" class="com.qianfeng.aop03.UserServiceImpl" />

<bean id="my" class="com.qianfeng.aop03.MyAspect"/>

重點步驟:

3.添加依賴包:不然程序會報錯:ClassNotFundException 類找不到類

<dependency>

????<groupId>org.aspectj</groupId>

????<artifactId>com.springsource.org.aspectj.weaver</artifactId>

????<version>1.6.8.RELEASE</version>

</dependency>

4.將proxy-target-class定義為true,表示強制使用cglib的方式來實現(xiàn)動態(tài)代理:

<aop:config proxy-target-class="true">

5.定義一個aop切點:

<aop:pointcut id="pt" expression="execution(* com.qianfeng.aop04.*.*(..))"/>

6.通知 將MyAsoect與切點關(guān)聯(lián)起來

<aop:advisor advice-ref="ma" pointcut-ref="pt"/>

7.創(chuàng)建MyAspect類實現(xiàn)org.aopalliance.intercept包下的MethodInterceptor:

public class MyAspectimplements MethodInterceptor {

? ? public Object invoke(MethodInvocation invocation)throws Throwable {

????????????System.out.println("-----before-----");

????????????Object obj = invocation.proceed();

????????????System.out.println("-----after-----");

????????????return obj;

????}

}

完整代碼為:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

? ? ????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

? ? ? ???? xmlns:aop="http://www.springframework.org/schema/aop"

? ? ? ???? xsi:schemaLocation="http://www.springframework.org/schema/beans

????????????http://www.springframework.org/schema/beans/spring-beans.xsd

????????????http://www.springframework.org/schema/aop

? ? ? ? ? ? http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="us" class="com.qianfeng.aop04.UserServiceImpl"/>

<bean id="ma" class="com.qianfeng.aop04.MyAspect"/>

<!--將proxy-target-class設(shè)置為true,強制使用cglib方式來實現(xiàn)動態(tài)代理-->

? ? <aop:config proxy-target-class="true">


? ? ? ? ? ? 定義一個aop切點,該包com.qianfeng.aop04下多有任意類,類中的任意含餐不含參數(shù)的方法都會被代理

? ? ? ? -->

? ? ? ? <aop:pointcut id="pt" expression="execution(* com.qianfeng.aop04.*.*(..))"/>

<!--一個通知,將MyAspect與切點關(guān)聯(lián)起來-->

? ? ? ? <aop:advisor advice-ref="ma" pointcut-ref="pt"/>

????</aop:config>

</beans>

第二種:與第一種類似,不過在設(shè)置代理方式后有所不同

完整代碼為:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

? ? ? ???? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

? ? ? ???? xmlns:aop="http://www.springframework.org/schema/aop"

? ? ? ? ? ?xsi:schemaLocation="http://www.springframework.org/schema/beans ????????????http://www.springframework.org/schema/beans/spring-beans.xsd

????????????http://www.springframework.org/schema/aop

? ? ? ????? http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="us" class="com.qianfeng.aop05.UserServiceImpl"/>

<bean id="ma" class="com.qianfeng.aop05.MyAspect" />

<aop:config proxy-target-class="true">

????<aop:aspect ref="ma">

????????<aop:pointcut id="mpc" expression="execution(* com.qianfeng.aop05.*.*(..))"/>

????????<aop:before method="myBefore" pointcut-ref="mpc"/>

????????<aop:after method="myAfter" pointcut-ref="mpc"/>

????</aop:aspect>

</aop:config>

</beans>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末梢卸,一起剝皮案震驚了整個濱河市竟趾,隨后出現(xiàn)的幾起案子躺孝,更是在濱河造成了極大的恐慌每币,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,000評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件姻采,死亡現(xiàn)場離奇詭異峡捡,居然都是意外死亡永毅,警方通過查閱死者的電腦和手機委刘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,745評論 3 399
  • 文/潘曉璐 我一進店門丧没,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人锡移,你說我怎么就攤上這事呕童。” “怎么了罩抗?”我有些...
    開封第一講書人閱讀 168,561評論 0 360
  • 文/不壞的土叔 我叫張陵拉庵,是天一觀的道長灿椅。 經(jīng)常有香客問我套蒂,道長钞支,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,782評論 1 298
  • 正文 為了忘掉前任操刀,我火速辦了婚禮烁挟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘骨坑。我一直安慰自己撼嗓,他們只是感情好,可當我...
    茶點故事閱讀 68,798評論 6 397
  • 文/花漫 我一把揭開白布欢唾。 她就那樣靜靜地躺著且警,像睡著了一般。 火紅的嫁衣襯著肌膚如雪礁遣。 梳的紋絲不亂的頭發(fā)上斑芜,一...
    開封第一講書人閱讀 52,394評論 1 310
  • 那天,我揣著相機與錄音祟霍,去河邊找鬼杏头。 笑死,一個胖子當著我的面吹牛沸呐,可吹牛的內(nèi)容都是我干的醇王。 我是一名探鬼主播,決...
    沈念sama閱讀 40,952評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼崭添,長吁一口氣:“原來是場噩夢啊……” “哼寓娩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起呼渣,我...
    開封第一講書人閱讀 39,852評論 0 276
  • 序言:老撾萬榮一對情侶失蹤根暑,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后徙邻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體排嫌,經(jīng)...
    沈念sama閱讀 46,409評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,483評論 3 341
  • 正文 我和宋清朗相戀三年缰犁,在試婚紗的時候發(fā)現(xiàn)自己被綠了淳地。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,615評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡帅容,死狀恐怖颇象,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情并徘,我是刑警寧澤遣钳,帶...
    沈念sama閱讀 36,303評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站麦乞,受9級特大地震影響蕴茴,放射性物質(zhì)發(fā)生泄漏劝评。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,979評論 3 334
  • 文/蒙蒙 一倦淀、第九天 我趴在偏房一處隱蔽的房頂上張望蒋畜。 院中可真熱鬧,春花似錦撞叽、人聲如沸姻成。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,470評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽科展。三九已至,卻和暖如春糠雨,著一層夾襖步出監(jiān)牢的瞬間辛润,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,571評論 1 272
  • 我被黑心中介騙來泰國打工见秤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留砂竖,地道東北人。 一個月前我還...
    沈念sama閱讀 49,041評論 3 377
  • 正文 我出身青樓鹃答,卻偏偏與公主長得像乎澄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子测摔,可洞房花燭夜當晚...
    茶點故事閱讀 45,630評論 2 359

推薦閱讀更多精彩內(nèi)容