Spring集合注入

本文内容纲要:Spring集合注入

UserDaoImpl.java

package com.zhangpn.daoImpl;

import java.util.List;

import java.util.Map;

import java.util.Set;

import com.zhangpn.dao.UserDao;

public class UserDaoImpl implements UserDao{

private List<String> list;

private Set<String> set;

private Map<String,String> map;

public List<String> getList() {

return list;

}

public void setList(List<String> list) {

this.list = list;

}

public Set<String> getSet() {

return set;

}

public void setSet(Set<String> set) {

this.set = set;

}

public Map<String, String> getMap() {

return map;

}

public void setMap(Map<String, String> map) {

this.map = map;

}

}

  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="UserDaoImpl" class="com.zhangpn.daoImpl.UserDaoImpl" scope="singleton">

<property name="list">

<list>

<value>list1</value>

<value>list2</value>

<value>list3</value>

</list>

</property>

<property name="set">

<set>

<value>set1</value>

<value>set2</value>

<value>set3</value>

</set>

</property>

<property name="map">

<map>

<entry key="map1" value="map1"></entry>

<entry key="map2" value="map2"></entry>

<entry key="map3" value="map3"></entry>

</map>

</property>

</bean>

</beans>

  Test.java:

package com.zhangpn.Test;

import java.util.Map.Entry;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhangpn.daoImpl.UserDaoImpl;

public class Test {

public static void main(String[] args) {

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

UserDaoImpl userDaoImpl = (UserDaoImpl)applicationContext.getBean("UserDaoImpl");

for(String s:userDaoImpl.getList()) {

System.out.println(s);

}

for(String s:userDaoImpl.getSet()) {

System.out.println(s);

}

for(Entry s:userDaoImpl.getMap().entrySet()) {

System.out.println(s.getValue());

System.out.println(s.getKey());

}

}

}

  测试结果:

Image

本文内容总结:Spring集合注入

原文链接:https://www.cnblogs.com/batj/p/9129123.html

以上是 Spring集合注入 的全部内容, 来源链接: utcz.com/z/296039.html

回到顶部