#千锋#
一.五大"集合"的注入方式
都是利用xml文件进行注入:
1.数组 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文件配置动态代理
第一种:可以通过设置aop切点里的参数来指定返回类型,指定包,指定类,指定方法,指定参数列表来进行代理
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内部先加上:
<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的方式来实现动态代理:
<aop:config proxy-target-class="true">
5.定义一个aop切点:
<aop:pointcut id="pt" expression="execution(* com.qianfeng.aop04.*.*(..))"/>
6.通知 将MyAsoect与切点关联起来
<aop:advisor advice-ref="ma" pointcut-ref="pt"/>
7.创建MyAspect类实现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设置为true,强制使用cglib方式来实现动态代理-->
<aop:config proxy-target-class="true">
定义一个aop切点,该包com.qianfeng.aop04下多有任意类,类中的任意含餐不含参数的方法都会被代理
-->
<aop:pointcut id="pt" expression="execution(* com.qianfeng.aop04.*.*(..))"/>
<!--一个通知,将MyAspect与切点关联起来-->
<aop:advisor advice-ref="ma" pointcut-ref="pt"/>
</aop:config>
</beans>
第二种:与第一种类似,不过在设置代理方式后有所不同
完整代码为:
<?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>