我想用spring读取属性文件的信息:
applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test.properties</value>
</property>
</bean>
<bean id="readProperties" class="sy.ic.dev.util.ReadProperties"
scope="request">
<property name="myName" value="${name}"></property>
<property name="myAddress" value="${address}"></property>
</bean>
</beans>
test.properties
myName=lishuo
myAddress=dalianReadProperties.java
package sy.ic.dev.util;public class ReadProperties { private static String myName;
public String getMyName() {
return myName;
} public void setMyName(String myName) {
ReadProperties.myName = myName;
} public String getMyAddress() {
return myAddress;
} public void setMyAddress(String myAddress) {
ReadProperties.myAddress = myAddress;
} private static String myAddress; // 为name和address提供GETTER和SETTER方法 public static void main(String[] args) {
System.out.println("My name is " + myName);
System.out.println("My address is " + myAddress);
}
}
这是我做的一个例子,但我现在不想做成这样,因为我的属性文件是随时可能添加修改的,如果添加修改,我需要改好几个文件,有没有别的方法能够实现?请高手解答一下,如果有别的方法,请附上代码。谢谢。
applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test.properties</value>
</property>
</bean>
<bean id="readProperties" class="sy.ic.dev.util.ReadProperties"
scope="request">
<property name="myName" value="${name}"></property>
<property name="myAddress" value="${address}"></property>
</bean>
</beans>
test.properties
myName=lishuo
myAddress=dalianReadProperties.java
package sy.ic.dev.util;public class ReadProperties { private static String myName;
public String getMyName() {
return myName;
} public void setMyName(String myName) {
ReadProperties.myName = myName;
} public String getMyAddress() {
return myAddress;
} public void setMyAddress(String myAddress) {
ReadProperties.myAddress = myAddress;
} private static String myAddress; // 为name和address提供GETTER和SETTER方法 public static void main(String[] args) {
System.out.println("My name is " + myName);
System.out.println("My address is " + myAddress);
}
}
这是我做的一个例子,但我现在不想做成这样,因为我的属性文件是随时可能添加修改的,如果添加修改,我需要改好几个文件,有没有别的方法能够实现?请高手解答一下,如果有别的方法,请附上代码。谢谢。
解决方案 »
- tomcat启动不了了。
- 如何用HQL查询表结构
- SpringIDE在eclipse3.2中装了后用不了
- 请问哪位大哥有spring in action或hibernate in action这两本书的源代码
- 关于servlet的一个问题
- 关于在struts中怎样定义双重循环的问题?大家帮帮忙,急等!!!!!
- 如何获取一条字符串中的第一个字符啊?
- 一个NameNotFoundException找不到EJB的问题(WebLogic8+JB9)
- java中对图片的处理?急用
- xml中的namespace 和前缀(prefix)的使用方法?
- 继续求SQL语句 oracle的 "viszl"请进 呵呵
- 为什么classpath中不设置Java\jdk\jre\lib\ext路径
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;//单例模式实现读取***.properties文件的内容
public class OVLoadProperties {
// 声明一个自己的实例
private static OVLoadProperties instance = new OVLoadProperties();
final static String fileName = "/config.properties";
// 返回该实例
public static synchronized OVLoadProperties getInstance() {
return instance;
} // 获取key所对应的值
public String getProperties(String key) {
Properties p = new Properties();
InputStream is = null;
try {
// ***.properties文件放在src目录的下边
is = OVLoadProperties.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream(fileName);
p.load(is);
} catch (Exception e) {
System.out.println("加载文件出错啦!" + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
return p.getProperty(key);
}
}
调用OVLoadProperties.getInstance().getProperties("myName")获得其myName值调用OVLoadProperties.getInstance().getProperties("myAddress")获得其myAddress值
propertylocationconfigure的
....
</util: map>