码迷,mamicode.com
首页 > 编程语言 > 详细

spring 注入java.util.Properties 属性两种xml中的配置练习

时间:2015-01-12 09:24:51      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:spring 注入properties

<beans  xmlns="http://www.springframework.org/schema/beans" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns:p="http://www.springframework.org/schema/p" 
		xmlns:util="http://www.springframework.org/schema/util" 
		xmlns:context="http://www.springframework.org/schema/context"
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
							http://www.springframework.org/schema/beans/spring-beans.xsd
							http://www.springframework.org/schema/context 
							http://www.springframework.org/schema/context/spring-context.xsd
							http://www.springframework.org/schema/util 
							http://www.springframework.org/schema/util/spring-util.xsd">

	<bean id="propertiesInjectBean1" class="com.doctor.spring.context.inject.PropertiesInjectBean1">
		<property name="properties">
			<value>
				name=doctor
				sex=man
				address=alien
			</value>
		</property>
	</bean>
	<bean id="propertiesInjectBean2" class="com.doctor.spring.context.inject.PropertiesInjectBean2">
		<property name="properties">
			<props>
				<prop key="name">doctor</prop>
				<prop key="address">alien</prop>
			</props>
		</property>
	</bean>

</beans>

类如下:

package com.doctor.spring.context.inject;

import java.util.Properties;


public class PropertiesInjectBean1 {
	private Properties properties;

	public Properties getProperties() {
		return properties;
	}

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

package com.doctor.spring.context.inject;

import java.util.Properties;


public class PropertiesInjectBean2 {

	private Properties properties;

	public Properties getProperties() {
		return properties;
	}

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

启动容器:

package com.doctor.spring.context.inject;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PropertiesInjectPractice {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/injectValueIntoBeanPropertiesPractice/propertiesInjectPracticeConfigurer.xml");
		PropertiesInjectBean1 injectBean1 = context.getBean(PropertiesInjectBean1.class);
		System.out.println(injectBean1.getProperties());
		
		PropertiesInjectBean2 injectBean2 = context.getBean(PropertiesInjectBean2.class);
		System.out.println(injectBean2.getProperties());
		
		context.close();
	}

}
输出:

{address=alien, name=doctor, sex=man}
{address=alien, name=doctor}

这两种都可以注入java.util.Properties 属性.

spring 注入java.util.Properties 属性两种xml中的配置练习

标签:spring 注入properties

原文地址:http://blog.csdn.net/doctor_who2004/article/details/42617429

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!