Spring对字段和集合的注入---依赖注入

11 篇文章 0 订阅

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>

这样就可以完成对容器类的属性注入了



  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值