<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:application.properties
</value>
                                <value>classpath:resource.properties
</value>
</list>
</property>
</bean>
比如我在resource.properties里面定义了一些配置信息,那么我如何在spring里面得到这个信息呢?
是一个web环境。不是main方式说动加载。
我实例化PropertyPlaceholderConfigurer和它的父类,都没有相应的方法可以获取到。不知道如何做

解决方案 »

  1.   


    import java.io.FileInputStream;
    import java.util.Properties;
    public class TestProperties {
    public static void main(String[] args) {
       Properties props = new Properties();
       String name_en = "",password_en = "",name_cn = "",password_cn = "";
       try{
         props.load(new FileInputStream(new TestProperties().getClassDirectory()+"test.properties"));
         name_en = props.getProperty("name_en");
         password_en = props.getProperty("password_en");
         name_cn = props.getProperty("name_cn");
         password_cn = props.getProperty("password_cn");
       }catch(Exception e){
           e.printStackTrace();
       }
       System.out.println("name_en = "+name_en+"   , password_en = "+password_en);
       System.out.println("name_cn = "+name_cn+"   , password_cn = "+password_cn);
    }
    String getClassDirectory() {
         String cla = this.getClass().getClassLoader().getResource(".").getPath();
         return cla;
       }
    }
      

  2.   

    我都说好几遍不是在main方式下手动加载。
    哪位知道告诉一下。
      

  3.   

    你要用injection,把值注入到你需要的地方。