1. 程式人生 > >Spring對欄位和集合的注入---依賴注入

Spring對欄位和集合的注入---依賴注入

Spring容器中,對於Bean的屬性,或者說是集合,可以使用Spring容器進行值的注入和載入。包括基本型別的值的注入和容器類的注入。首先需要寫一個Bean.

package com.bird.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import com.bird.dao.PersonDao;

public class PersonServerBean {

	private PersonDao personDao;
	private String name;
	private Set<String> set = new HashSet<String>();
	private List<String> list = new ArrayList<String>();
	private Properties properties = new Properties();
	private Map<String,String> map = new HashMap<String,String>();

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	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 String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public void init() {
		personDao.add();
		System.out.println(name);
	
		for(String value : set){
			System.out.println(value);
		}
		
		for(String temp:list){
			System.out.println(temp);
		}
		
		for(int i = 1; i <= properties.size(); i++){
			System.out.println(properties.getProperty(String.valueOf(i)));
		}
		
		for(Entry<String, String> entry :map.entrySet()){
			System.out.println(entry.getKey()+"=="+entry.getValue());
		}
	}
}

在Bean.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd" >
           <context:annotation-config/>
           
	<!-- 
	
	<bean id="personDao" class="com.bird.dao.impl.PersonDaoBean"></bean>
	
	
	<bean id="personService" class="com.bird.service.impl.PersonServerImpl" 
	init-method="init" destroy-method="destory" scope="prototype"></bean>
	 
	 
	 
	<bean id="personServerBean" class="com.bird.service.impl.PersonServerBean">
		<property name="personDao" ref="personDao"></property>
	</bean>
	-->
	
	<bean id="personServerBean" class="com.bird.service.impl.PersonServerBean">
		<property name="personDao">
			<bean class="com.bird.dao.impl.PersonDaoBean"></bean>
		</property>
		<property name="name" value="bird"></property>
		
		<property name="set">
			<set>
				<value>第一個</value>
				<value>第二個</value>
				<value>第三個</value>
			</set>
		</property>
		
		<property name="list">
			<list>
				<value>one</value>
				<value>two</value>
				<value>three</value>
			</list>
		</property>
		
		<property name="properties">
			<props>
				<prop key="1">Properties1</prop>
				<prop key="2">Properties2</prop>
				<prop key="3">Properties3</prop>
			</props>
		</property>
		
		<property name="map">
			<map>
				<entry key="key-1" value="value-1"></entry>
				<entry key="key-2" value="value-2"></entry>
				<entry key="key-3" value="value-3"></entry>
			</map>
		</property>
	</bean>
</beans>

這樣就可以完成對容器類的屬性注入了