Spring的DI(Ioc) - 注入集合类型

本文内容纲要:Spring的DI(Ioc) - 注入集合类型

1: 首先给service添加集合类型的属性,并提供getter, setter

package cn.gbx.serviceimpl;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.HashSet;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.Set;

import cn.gbx.daoimpl.PersonDao;

import cn.gbx.service.PersonService;

public class PersonServiceImpl implements PersonService {

private Set<String> sets = new HashSet<String>();

private List<String> list = new ArrayList<String>();

private Map<String, String> maps = new HashMap<String, String>();

private Properties properties = new Properties();

private PersonDao personDao;

private String name;

private Integer id;

public void save() {

System.out.println("id = " + id + "name = " + name);

personDao.save();

System.out.println("service : " + " save 方法");

}

public PersonDao getPersonDao() {

return personDao;

}

public void setPersonDao(PersonDao personDao) {

this.personDao = personDao;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public Set<String> getSets() {

return sets;

}

public void setSets(Set<String> sets) {

this.sets = sets;

}

public List<String> getList() {

return list;

}

public void setList(List<String> list) {

this.list = list;

}

public Map<String, String> getMaps() {

return maps;

}

public void setMaps(Map<String, String> maps) {

this.maps = maps;

}

public Properties getProperties() {

return properties;

}

public void setProperties(Properties properties) {

this.properties = properties;

}

}

  

2: 配置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-2.5.xsd">

<bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>

<bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >

<property name="personDao" ref="personDaoImpl"></property>

<property name="name" value="ok-gbx"></property>

<property name="id" value="22"></property>

<property name="list">

<list>

<value>valu1</value>

<value>valu2</value>

<value>valu3</value>

</list>

</property>

<property name="sets">

<set>

<value>value-1</value>

<value>value-2</value>

<value>value-3</value>

</set>

</property>

<property name="maps">

<map>

<entry key="key1" value="value1"></entry>

<entry key="key2" value="value2"></entry>

<entry key="key3" value="value3"></entry>

</map>

</property>

<property name="properties">

<props>

<prop key="key1">value1</prop>

<prop key="key2">value2</prop>

<prop key="key3">value3</prop>

</props>

</property>

</bean>

</beans>

  

3: 测试:

public class SpringTest {

@Test

public void spring1() {

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

PersonService ps = (PersonService)ctx.getBean("personServiceImpl");

ps.save();

System.out.println("--------List-------------");

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

System.out.println(s);

}

System.out.println("--------sets-------------");

for (String s : ps.getSets()) {

System.out.println(s);

}

System.out.println("--------maps-------------");

for (String key : ps.getMaps().keySet()) {

System.out.println(key + " : " + ps.getMaps().get(key));

}

System.out.println("--------propertis-------------");

for (Object key : ps.getProperties().keySet()) {

System.out.println(key + " : " + ps.getMaps().get(key));

}

}

@Test

public void spring2() {

MyClassPathXmlApplicationContext ctx = new MyClassPathXmlApplicationContext("beans.xml");

PersonService ps = (PersonService)ctx.getBean("personServiceImpl");

ps.save();

System.out.println();

}

}

  

本文内容总结:Spring的DI(Ioc) - 注入集合类型

原文链接:https://www.cnblogs.com/E-star/p/3558890.html

以上是 Spring的DI(Ioc) - 注入集合类型 的全部内容, 来源链接: utcz.com/z/296046.html

回到顶部