後臺properties配置獨立tomcat以外配置方法

1、各環境修改tomcat目錄下配置文件進行配置java

經過修改catalina.sh配置(TOMCAT_HOME/bin)git

進入catalina.sh找到web

JAVA_OPTS='-Xms512m -Xmx512m -XX:PermSize=256M -XX:MaxNewSize=256m -XX:MaxPermSize=512m -Dfile.encoding=UTF-8'apache

JAVA_OPTS的值插入如下語句tomcat

在後面加上 -Dglobal.config.path=/usr/local/jconfigapp

紅色字體根據配置文件所在的位置修改webapp

《後臺開發人員》本地經過idea啓動包時,須要添加如下配置maven

使用idea的maven啓動,須要在edit configurations -Runner中添加Environment variables:global.config.path=E:/maps-jushi/project-setting/dev;ide

紅色字體修改成項目路徑,如圖字體

以上方法不行,則採用如下方法:

在parameters的command line內增長「-Dglobal.config.path=E:\gitworkplace\maps-jushi\project-setting」

clean -Pdev tomcat7:run -Dmaven.tomcat.port=8081 -Dglobal.config.path=E:\gitworkplace\maps-jushi\project-setting

 

2、java的文件SystemProperties寫法

package com.zdnst.common.infra.utils;



import java.util.Properties;


/**
 * 讀取web.properties配置文件
 * @author zhongyq
 * @version 1.0.0, 2013-3-19
 */
public class SystemProperties {
    // the properties object
    private static Properties props;
    
    /**
     * This method returns the property values, if found. It loads the properties
     * from the map properties file if not already loaded.
     * @param             property        the property whose value is requested for
     * @param             def             the default value to return if property not found
     * @return      String      retVal          the value returned
     */
    public static String getProperty(String property, String def) {
        String retVal = null;
        getProperties();
        if(props != null)
            retVal = props.getProperty(property, def).trim();        
        else
            retVal = def;
        return retVal;
    }
    
    /**
     * @describe: 根據給定的KEY獲取配置信息,沒有默認返回空字符串
     * @param key
     * @return
     * @author:kui.he
     * @time:2014年9月22日下午12:04:25
     */
   public static String getProperty(String key){
      return getProperty(key,"");
   }
    /**
     * sets the property for the given key & value
     * @param       strKey          String      Key
     * @param       strValue        String      Value
     * @return                      void        
     *
     **/
    public static void setProperty(String strKey, String strValue) {        
        if(props != null)
            props.setProperty(strKey, strValue);
    }

    /**
     * This method loads the properties object and returns it.
     * @return      Properties      the loaded property object, else null
     */
    public static Properties getProperties() {
        if(props == null) {
            try {
                loadProperties("resources.properties");

            }
            catch(Exception exc) {
                System.err.println(exc.getMessage());
                props = null;
            }
        }
        return props;
    }

    /**
     * This method loads the Properties object from the Map Properties file
     * @param             file        the configuration file
     */
    public static void loadProperties(String file) throws Exception {
        props = loadPropertiesFile(file);
    }

    /**
     * This method loads a Properties object and returns it
     * @param             file        the configuration file to load
     */
    public static Properties loadPropertiesFile(String file) throws Exception {

//        String proFile = ConfigUtils.getInstance().getClassPath()+file;
        String path = System.getProperty("global.config.path") == null ? ConfigUtils.getInstance().getClassPath()
                : System.getProperty("global.config.path") ;
        String proFile = path +"/"+file;
        //System.out.println(proFile);
        return ConfigUtils.loadProperties(proFile);

//        Properties retVal = new Properties();
//        String url=SystemProperties.class.getClassLoader().getResource("/")+file;
//        System.out.println("url:"+url);
//        InputStream in = SystemProperties.class.getClassLoader().getResourceAsStream(file);
//        if(in != null)
//            retVal.load(in);
//        in.close();
//        return retVal;
    }
    
    /**
     * 返回類目錄路徑,如:D:\tools\apache-tomcat-7.0.42\webapps\maps1.1\WEB-INF\classes\
     * 
     * @return
     * @author:kaiqiang.wu
     * @time:2015年11月18日上午10:59:22
     */
    public static String getClassPath(){
       
       return SystemProperties.class.getClassLoader().getResource("").getPath();
       
    }
    
    public static void main(String[] args) {
      System.out.println(SystemProperties.getClassPath());
   }
    
}

3、application-context.xml

<context:property-placeholder location="file:${global.config.path}/resources.properties"/>

4、將配置文件拉取到相應位置:

usr/local/jconfig/resources.properties拉取配置文件到相應的路徑;

相關文章
相關標籤/搜索