一.Spring对于集合的五种注入方式
1.五种集合
(1).数组(array):
数组是可以用来存储固定容量数据的一个容器,存储的每个数据都有一个下标,来确定数据的具体位置.
(2)list:
list是一个可扩容的存储有序集合,他的底层容器是数组,所以它也拥有和数组一样的特性,不过list比数组多一个可自动扩容的机制,当list集合存储空间不够时,会触发它的自动扩容机制,重新创建一个新的大数组,将所有的数据存入到这个新的数组中去,初始容量为10,每次扩容为原长度的1.5倍
(3)set:
set是一个没有下标的集合,可以通过迭代器来遍历集合中的元素,它最大的作用在于可以去除重复的元素,当我们需要给一大串数据去重时,可以选择set集合进行操作.
(4)map:
map是一个双列集合,以键值对(key-value)的形式存储数据,键和值都允许为null.
(5)properties:
Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,能让用户够脱离程序本身去修改相关的变量设置。就像在Java中,其配置文件常为.properties文件,是以键值对的形式进行参数配置的.
2.五种集合的注入方式
接下来我们用代码来进行展示,先创建一个bean类型的类,取名为MyColl类,类中定义5个集合的私有的全局全局变量,并给出getter和setter方法,代码如下所示:
public class MyColl {
private String[] arrays;
private List<Object> list;
private Set<Object> set;
private Map<String,String> map;
private Properties prop;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("MyColl{");
sb.append("arrays=").append(Arrays.toString(arrays));
sb.append(", list=").append(list);
sb.append(", set=").append(set);
sb.append(", map=").append(map);
sb.append(", prop=").append(prop);
sb.append('}');
return sb.toString();
}
public String[] getArrays() {
return arrays;
}
public void setArrays(String[] arrays) {
this.arrays = arrays;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Set<Object> getSet() {
return set;
}
public void setSet(Set<Object> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProp() {
return prop;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
XML代码如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="obj" class="java.lang.Object" />
<bean id="stu" class="nz.study.bean.Student">
<property name="sid" value="8" />
<property name="name" value="小红" />
<property name="sex" value="false" />
<property name="score" value="86" />
<property name="age" value="20" />
</bean>
<bean id="myColl" class="nz.study.ioc.MyColl">
<property name="arrays" >
<array><!--数组允许重复元素-->
<value>java</value>
<value>java</value>
<value>html5</value>
<value>python</value>
<value>testing</value>
</array>
</property>
<property name="list">
<list><!--list集合允许重复元素-->
<value>zhouxingxing</value>
<value>zhouxingxing</value>
<value>9527</value>
<ref bean="obj" />
<ref bean="stu" />
</list>
</property>
<property name="set">
<set>
<value>sunwukong</value>
<value>sunwukong</value><!--该值已经存在,所以不会被加入-->
<value>zhubajie</value>
<value>tangseng</value>
<value>shaheshang</value>
</set>
</property>
<property name="map">
<map>
<entry key="jack" value="杰克" /><!--添加方法返回为null-->
<entry key="jack" value="杰克2" /><!--添加方法返回杰克-->
<entry key="rose" value="肉丝" /><!--添加方法返回杰克2-->
<entry key="rose" value="null" /><!--添加方法返回肉丝,map允许空value-->
<entry key="null" value="肉丝"/><!--map允许空key-->
<entry key="null" value="null" /><!--map允许key和value同时为null,该方法返回肉丝-->
</map>
</property>
<property name="prop">
<props>
<prop key="url">jdbc:mariadb://localhost:3306/mydb2</prop>
<prop key="driver">org.mariadb.jdbc.Driver</prop>
<prop key="userName">root</prop>
<prop key="password">19960914wx</prop>
</props>
</property>
</bean>
</beans>
以上需要注意的是,除八个基本数据类型加上String都是使用value进行赋值的,其他类型的引用数据类型都是使用ref方式进行赋值.
二.xml实现SpringAOP的两种方式
1.xml配置文件中引入aop的相关链接(织入配置):
实现起来与AOP03的配置差不多,都是基于xml进行实现,具体代码如下所示:
<?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="nz.study.aop04.UserServiceImpl" />
<bean id="ma" class="nz.study.aop04.MyAspect" />
<!--
proxy-target-class设置为true,强制使用cglib实现动态代理
-->
<aop:config proxy-target-class="true">
<!--
定义一个aop的切点
该包nz.study.aop04下所有的任意类,类下的任意含参不含参的方法,返回值必须为list的方法将会被代理
<aop:pointcut id="pt" expression="execution(java.util.List nz.study.aop04.*.*(..))" />
该包nz.study.aop04下所有的任意类,类下的任意含参不含参数的方法,返回值必须为boolean的方法将会被代理
<aop:pointcut id="pt" expression="execution(boolean nz.study.aop04.*.*(..))" />
nz.study.aop04.UserServiceImpl.deleteUser(int)为具体的方法,参数为int,返回值任意
<aop:pointcut id="pt" expression="execution(* nz.study.aop04.UserServiceImpl.deleteUser(int))"/>
-->
<aop:pointcut id="pt" expression="execution(* nz.study.aop04.UserServiceImpl.deleteUser(int))"/>
<!--
通知,将MyAspect与切点关联起来
-->
<aop:advisor advice-ref="ma" pointcut-ref="pt"/>
</aop:config>
</beans>
2.切面式
aspect是切面,pointcut是切点,也就是需要代理的方法,通过需要代理的方法切点形成了一个切面。具体代码如下所示
<?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="nz.study.aop05.UserServiceImpl"/>
<bean id="ma" class="nz.study.aop05.MyAspect"/>
<aop:config proxy-target-class="true">
<aop:aspect ref="ma">
<aop:pointcut id="mpc" expression="execution(* nz.study.aop05.*.*(..))"/>
<aop:before method="myBefore" pointcut-ref="mpc"/>
<aop:after method="myAfter" pointcut-ref="mpc"/>
</aop:aspect>
</aop:config>
</beans>
疫情期间,这是我在千峰线上学习的第14天,加油,奥利给!!!