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

<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 属性.

时间: 2024-07-29 12:00:55

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

java.util.Arrays.sort两种方式的排序(及文件读写练习)

import java.io.*; import java.util.*; public class SortTest{ public static void main(String args[]) throws IOException, ClassNotFoundException { FileReader InWord = new FileReader(new File("words.txt")); BufferedReader in = new BufferedReader(In

spring之为java.util.Properties类型的属性进行赋值

Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串.在spring中可以用其存储连接数据库的相关信息. DataSource.java package com.gong.spring.beans; import java.util.Properties; public class DataSource { private Properties properties; public Properties getP

配置Java连接池的两种方式:tomcat方式以及spring方式

1. tomcat方式:在context.xml配置连接池,然后在web.xml中写配置代码(也可以在server.xml文件中配置连接池),这两种方法的区别是:在tomcat6版本及以上中context元素已经从server.xml文件中独立出来了,放在一个context.xml文件中.因为server.xml是不可动态重加载的资源,服务器一旦启动了以后,要修改这个文件,就得重启服务器才能重新加载.然而context.xml文件则不然,tomcat服务器会定时去扫描这个文件,一旦发现文件被修改

java.util.properties

http://gimgen1026.iteye.com/blog/152023 Properties 类已不是新东西了,它在 Java 编程的早期就有了,并且几乎没有什么变化.J2SE 的 Tiger 版本增强了这个类,不仅可以用它在单独一行中指定用等号分隔的多个键-值对,还可以用XML 文件装载和保存这些键-值对.在 驯服 Tiger的这一期文章中,John Zukowski 展示了如何驾驭这匹新一代的“役马”. J2SE 1.5 以前的版本要求直接使用 XML 解析器来装载配置文件并存储设置

JDK源码学习(9)- java.util.Properties实例与源码

java.util.Properties说明. 该类主要是读取属性配置文件,两种文件类型:普通文件格式为key = value:xml文件. 1)key = value示例如下: public class TestProperties { public static void main(String[] args) { Properties properties = new Properties(); FileInputStream fileInputStream; try { fileInpu

工作积累(二)——使用java.util.ResourceBundle和java.util.Properties实现常量功能

在 Java 中我们往往通过定义常量来使用一些值,方便我们进行修改配置,如: public classConstant {   public static final String IMG_ORI_PATH = "ori/";   public static final String IMG_THUMB_PATH = "thumb/";   -- } 这样我们在其他类中可以直接使用 Constant.IMG_ORI_PATH 来代替 "ori/"

java.util.Properties类 学习笔记

学习目标: 1.认识properties文件,理解其含义,会正确创建properties文件. 2.会使用java.util.Properties类来操作properties文件. 3.掌握相对路径,能正确书写一个properties文件的相对路径. 一.认识properties文件 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置. 注    释:前面加上#号 属性配置:以“键=值”的方式书写一个属性的配置信息. 3.propert

spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件

spring cloud  读取 配置文件属性值 1.bean @Data public class LocalFileConfig { /** * 文件存储地址 */ private String fileServerPath; private String fileDownloadUrl; private String defaultCutSize; private String fileValidateType; private Long fileSize; } 配置 @Configura

java.util.Properties类

Properties类很常用么,几乎每个项目,从j2se到j2ee每个项目都没离开过他,就算是jsp+servlet+jdbc的东西,jdbc的配置信息也是写Properties,国际化也是Properties,cdn也是Properties,memcached也是 Properties.总之java.utils.*无所不用,不所不在.. 小记下Properties: java.util.Properties是对properties这类配置文件的映射.支持key-value类型和xml类型两种.