在spring 中如何注入map,set,list,property等引數
阿新 • • 發佈:2019-01-10
- package cn.dao;
- publicinterface PersonDao {
- publicabstractvoid add();
- }
cn.dao.imp
- package cn.dao;
- publicinterface PersonDao {
- publicabstractvoid add();
- }
cn.service
- package cn.service;
- publicinterface PersonService {
-
public
- }
cn.service.imp
- package cn.service.imp;
- 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 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor;
- import cn.service.PersonService;
- publicclass PersonServiceBean implements PersonService {
- private Set<String> sets=new HashSet<String>();
- private List<String> lists=new ArrayList<String>();
- private Properties properties=new Properties();
-
private
- public Map<String, String> getMaps() {
- return maps;
- }
- publicvoid setMaps(Map<String, String> maps) {
- this.maps = maps;
- }
- public Properties getProperties() {
- return properties;
- }
- publicvoid setProperties(Properties properties) {
- this.properties = properties;
- }
- public List<String> getLists() {
- return lists;
- }
- publicvoid setLists(List<String> lists) {
- this.lists = lists;
- }
- public Set<String> getSets() {
- return sets;
- }
- publicvoid setSets(Set<String> sets) {
- this.sets = sets;
- }
- publicvoid save()
- {
- System.out.println("執行save()方法");
- }
- }
junit.test
- package junit.test;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.AbstractApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import cn.service.imp.PersonServiceBean;
- publicclass SpringTest
- {
- @Test
- publicvoid instanceSpring()
- {
- AbstractApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
- PersonServiceBean personServiceBean=(PersonServiceBean) ctx.getBean("personService");
- System.out.println("-------------set--------------");
- for(String value:personServiceBean.getSets())
- {
- System.out.println(value);
- }
- System.out.println("-------------list--------------");
- for(String value:personServiceBean.getLists())
- {
- System.out.println(value);
- }
- System.out.println("-------------properties--------------");
- for(Object key:personServiceBean.getProperties().keySet())
- {
- System.out.println(key+"="+(String)personServiceBean.getProperties().getProperty((String)key));
- }
- System.out.println("-------------map--------------");
- for(String key:personServiceBean.getMaps().keySet())
- {
- System.out.println(key+"="+(String)personServiceBean.getMaps().get(key));
- }
- ctx.close();
- }
- }
beans.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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">
- <beanid="personService"class="cn.service.imp.PersonServiceBean">
- <propertyname="sets">
- <