在一个应用中,需要将 java.util.Properties 作为参数传入 一个类中,并读出其中的一个参数
package cn.com.test.spring.properties;
import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PropertyTest {
private Properties pro;
public void setPro(Properties pro) {
this.pro = pro;
}
public Properties getPro() {
return pro;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("context_test_property.xml");
PropertyTest test = (PropertyTest)context.getBean("PropertyTest");
System.out.println(test.getPro().get("testString"));
}
}
如何将java.util.Properties以一个Bean的形式传入该类呢?可以使用Spring的静态工厂方法:
首先定义一个类实现一个返回 Properties的静态方法:
package cn.com.test.spring.properties;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Configuration {
private static Properties pro = null;
public static Properties getProperties(String propertyFile)
throws FileNotFoundException, IOException {
if (pro == null) {
pro = new Properties();
pro.load(new FileInputStream(propertyFile));
}
return pro;
}
}
然后在配置文件中注册这个静态工厂方法即可
<beans xmlns="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-3.0.xsd">
<bean id="PropertyTest" class="cn.com.test.spring.properties.PropertyTest">
<property name="pro" ref="prop" />
</bean>
<bean id="prop" class="cn.com.test.spring.properties.Configuration"
factory-method="getProperties">
<constructor-arg
value="E:\Project\Java\Spring\springstudy\src\test.properties" />
</bean>
</beans>
如果在src的根路径下有文件test.propertie,其内容是:
testString=this is a test string for the test of a property file
则运行程序将打印出这句话
分享到:
相关推荐
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
java.util.ConcurrentModificationException 异常问题详解1
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
Exception in thread “main“ java.util.InputMismatchException
java.util.Date与java.sql.Date互转及字符串转换为日期时间格式.docx
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
java并发工具包 java.util.concurrent中文版-带书签版
详细介绍java.util.Date和java.sql.Date相互转换的多种方法总结,希望对大家有帮助
java.util包
org.springframework.web.util.HtmlCharacterEntityReferences.class org.springframework.web.util.HtmlUtils.class org.springframework.web.util.HttpSessionMutexListener.class org.springframework.web.util....
java.util.pdf
这是我在编写struts2中遇到的问题,整理出来,包括截图,希望可以帮到大家
世界范围内的时区列表。由 java.util.TimeZone 类导出
java.util包源码,pdf版,方便打印
使用java.util.timer实现的简单定时任务,在实现简单一次性定时任务时,使用java.util.timer非常的简单易用,适合没有接触过quartz的新手急用。
java.util包总结,方便大家学习。请多指教。
java.util.concurrent总体概览图。 收取资源分3分。需要的同学可以下载一下。 java.util.concurrent主要包括5个部分executor,colletions,locks,atomic,tools。 该图详细的列举了并发包下面的结构,包含所有接口和...
Java.util包常用接口