我想用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);
}
}
这是我做的一个例子,但我现在不想做成这样,因为我的属性文件是随时可能添加修改的,如果添加修改,我需要改好几个文件,有没有别的方法能够实现?请高手解答一下,如果有别的方法,请附上代码。谢谢。

解决方案 »

  1.   

    可以写个类,专门来读取xxx.properties文件中的内容:package util;import java.io.File;
    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);
    }
    }
      

  2.   

    将fileName 换成你的final static String fileName = "/test.properties";
    调用OVLoadProperties.getInstance().getProperties("myName")获得其myName值调用OVLoadProperties.getInstance().getProperties("myAddress")获得其myAddress值
      

  3.   

    我是想在spring中做单例的,这个类我在spring中怎么配置?
      

  4.   

    这个类我也写了,不知道在spring中怎么搞。
      

  5.   

    不清楚你为啥把那个properties文件的内容加载到spring中
      

  6.   

    spring读取属性文件,不是有专门的spring类读取属性用到spring配置文件的?
    propertylocationconfigure的
      

  7.   

    我通常都是用ssh框架的。单独的spring很少用的!
      

  8.   

    使用 Spring 3 中的 @Value 注解,参考:http://chrislovecnm.com/2010/03/08/spring-3-java-based-configuration-with-value/或者自己用 java.util.Properties 实现,自己实现的话也很简单。
      

  9.   

    干脆别用 properties 文件了。 xmlns:util="http://www.springframework.org/schema/util" 命名空间打开。 <util: map> 
        ....
    </util: map>