珠海多语种网站制作百度2022最新版本
在某个类中需要依赖其它类时,通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理。Spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过Spring容器帮我们new指定实例并且将实例注入到需要该对象的类中,今天就来总结一下Spring对于数组、集合及Properties的注入。
(1)创建数组、集合及Properties的类
public class MyCollection {private String[] arrays;private List<Object> list;private Map<String, String> map;private Set<Object> set;private Properties prop;@Overridepublic String toString() {return "MyCollection{" +"arrays=" + Arrays.toString(arrays) +", list=" + list +", map=" + map +", set=" + set +", prop=" + prop +'}';}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 Map<String, String> getMap() {return map;}public void setMap(Map<String, String> map) {this.map = map;}public Set<Object> getSet() {return set;}public void setSet(Set<Object> set) {this.set = set;}public Properties getProp() {return prop;}public void setProp(Properties prop) {this.prop = prop;}
}
(2).创建Beans.xml文件
创建beans.xml文件分别给数组,集合和Properties注入。
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
1.beans.xml给数组注入
这里以String类型的数组为例,数组的注入需要使用< array >< array/>标签,在标签中有多对< value >< value />标签可以给数组中添加元素。
<bean id="myCollection" class="com.ioc.MyCollection"><property name="arrays"><array><value>zhangsan</value><value>lisi</value><value>wangwu</value></array></property>
</bean>
2.beans.xml给List集合注入
给list集合注入时,在property 标签中需要使用< list >< list />标签来注入,同样
在标签中有多对< value >< value />标签可以给list集合中添加元素。
<bean id="myCollection" class="com.ioc.MyCollection">
<property name="list"><list><value>100001</value><value>zhangsan</value><value>lisisi</value><ref bean="obj"/></list></property>
</bean>
3.beans.xml给map集合中注入
给map集合注入时,在property 标签中需要使用< map >< map />标签来注入,因为map集合中的每个元素都是以键值对的形式存在的,所以在map标签内部需要以
< entry >< entry />标签来注入,并且在entry标签中需要有key和value属性。
<bean id="myCollection" class="com.ioc.MyCollection"><property name="map"><map><entry key="1" value="tom"/><entry key="2" value="jerry"/><entry key="3" value="dog"/></map></property>
</bean>
4.beans.xml给set集合注入
给set集合注入时,在property 标签中需要使用< set >< set />标签来注入,同样
在标签中有多对< value >< value />标签可以给list集合中添加元素。
<bean id="myCollection" class="com.ioc.MyCollection"><property name="set"><set><value>zhangsan</value><value>lisi</value><value>100</value><ref bean="obj"/><value>120</value></set></property>
</bean>
5.beans.xml给properties注入
properties文件在使用数据库连接池的时候是经常需要用到的,所以给properties注入也是非常重要的,在给properties注入时,需要使用< props >< props />标签,而且在props标签内有多对< prop >< /prop >标签,key值作为属性在prop标签之上,value值在每对prop标签之间。
<bean id="myCollection" class="com.ioc.MyCollection"><property name="prop"><props><prop key="driverClassName">com.mysql.jdbc.Driver</prop><prop key="url">jdbc:mysql://localhost:3306/test</prop><prop key="username">root</prop><prop key="password">147258</prop></props></property>
</bean>