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

#千鋒#

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

都是利用xml文件進(jìn)行注入:

1.數(shù)組 arrays

<property name="arrays">

????<array>

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

????????<value>hjx</value><!--允許重復(fù)-->

? ? ? ? <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><!--允許重復(fù)-->

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

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

????</list>

</property>

3.Set集合

<property name="set">

????<set>

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

????????<value>suk</value><!--不允許重復(fù)-->

? ? ? ? <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:就是使用連接池時(shí)使用的,以 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文件配置動(dòng)態(tài)代理

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

1.先對(duì)bean.xml文件進(jìn)行處理,將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"/>

重點(diǎn)步驟:

3.添加依賴包:不然程序會(huì)報(bào)錯(cuò):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,表示強(qiáng)制使用cglib的方式來實(shí)現(xiàn)動(dòng)態(tài)代理:

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

5.定義一個(gè)aop切點(diǎn):

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

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

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

7.創(chuàng)建MyAspect類實(shí)現(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,強(qiáng)制使用cglib方式來實(shí)現(xiàn)動(dòng)態(tài)代理-->

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


? ? ? ? ? ? 定義一個(gè)aop切點(diǎn),該包c(diǎn)om.qianfeng.aop04下多有任意類,類中的任意含餐不含參數(shù)的方法都會(huì)被代理

? ? ? ? -->

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

<!--一個(gè)通知,將MyAspect與切點(diǎn)關(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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容