1.在bean中对String集合进行依赖注入
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
2.在bean中对List集合中进行依赖注入
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
3.在bean中对Map进行依赖注入
<property name="card">
<map>
<entry key="身份证" value="111111111111111111"/>
<entry key="银行卡" value="333333333333333333"/>
</map>
</property>
4.对Set集合进行依赖注入
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
<value>LOL</value>
</set>
</property>
5.对Properties进行依赖注入
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
<value>LOL</value>
</set>
</property>
代码
- beans.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="hello" class="com.lixv.entity.Hello">
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
<value>看电影</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="111111111111111111"/>
<entry key="银行卡" value="333333333333333333"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
<value>LOL</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="driver">202020</prop>
<prop key="url">101010</prop>
<prop key="username">303030</prop>
<prop key="password">000000</prop>
</props>
</property>
</bean>
<bean id="helloSpring" class="com.lixv.entity.HelloSpring" name="helloSpringNew1,helloSpringNew2">
<property name="springStr" value="springstr"/>
</bean>
</beans>
- 测试代码
package com.lixv.dao;
import com.lixv.entity.Hello;
import com.lixv.entity.HelloSpring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello);
}
}
- 运行结果