#千鋒#
一.五大"集合"的注入方式
都是利用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>