spring集合Collection注入
1、BookDao接口和实现类
public interface BookDao {
void save();
}
public class BookDaoImpl implements BookDao {
private int[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
public void setArray(int[] array) {
this.array = array;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void save(){
System.out.println("book dao save......" );
System.out.println("遍历数组:"+ Arrays.toString(array));
System.out.println("遍历List"+ list);
System.out.println("遍历Set"+ set);
System.out.println("遍历Map"+ map);
System.out.println("遍历Properties"+ properties);
}
}
2、配置文件
<?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="bookDao" class="com.itheima.dao.impl.BookDaoImpl" >
<property name="array" >
<array>
<value>100</value>
<value>200</value>
<value>300</value>
</array>
</property>
<property name="list">
<list>
<value>itcast</value>
<value>itheima</value>
<value>itjava</value>
</list>
</property>
<property name="set">
<set>
<value>java</value>
<value>php</value>
<value>golang</value>
<value>golang</value>
<value>c++</value>
</set>
</property>
<property name="map">
<map>
<entry key="country" value="China"/>
<entry key="province" value="Henan"/>
<entry key="city" value="Kaifeng"/>
</map>
</property>
<property name="properties">
<props>
<prop key="country">china</prop>
<prop key="province">Henan</prop>
<prop key="city">Kaifeng</prop>
</props>
</property>
</bean>
</beans>
3、使用方法
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();
}
4、总结
- Array使用array和value节点注入
- List使用list和value节点注入
- Set使用set和value节点注入
- Map使用map和entry节点中的key和value属性注入
- Properties使用props中prop节点的key属性和内容注入
文章来源地址https://www.toymoban.com/news/detail-606175.html
文章来源:https://www.toymoban.com/news/detail-606175.html
到了这里,关于spring学习笔记八的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!