struts2配置文件有没有类似spring配置文件引用properties的功能
如:
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:init.properties</value>
</property>
</bean>

解决方案 »

  1.   

    package com.inker.utils.property;import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Map.Entry;/**
     * 读取properties配置文件 
     */
    public class PropertiesUtil { private static  Properties  props = null;

    /**
     * 根据KEY查询prop文件的值
     * @param key
     * @param propFileName   prop 文件名 文件位于classes目录中
     */
    public static Object getValueByKeyFormProp(String key, String propFileName)throws IOException{

    props = new Properties();

    props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(propFileName));

    return props.get(key);
    }

    /**
     * 将prop转化成Map 
     */
    public static Map<String, String> propToMap(String propFileName)throws IOException{

    Map<String, String> map = new HashMap<String, String>();
    props = new Properties();

    props.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(propFileName));

    for(Entry<Object, Object> entry : props.entrySet()){
    map.put(entry.getKey().toString(), entry.getValue().toString());
    }

    return map;
    }

    public static void main(String[] args) throws Exception{ System.out.print(PropertiesUtil.getValueByKeyFormProp("49", "memberlevel.properties"));

    }}
      

  2.   

    在类里面读取我知道,我意思是能不能像spring那样直接引用就可以了?