欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring项目配置多环境配置文件

程序员文章站 2024-03-24 19:24:40
...
package com.zhan.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 获取配置文件的方法
 * @date 2019-03-25
 * @author bean.zhang
 */
public class CustomPropertiesUtils {
    private static Properties pro = new Properties();
    public static String env = null;

    static {
        try {
            env = getCurrentResourcePath();
            InputStream in = CustomPropertiesUtils.class.getResourceAsStream("/"+env + "/custom.properties");
            pro.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getProperties(String key) {
        return pro.getProperty(key);
    }



    private static String getCurrentResourcePath() {
        if (env != null) {
            return env;
        }
        env = System.getenv("resources_path");
        if (null == env) {
            env = System.getProperty("resources_path");
        }
        return env;
    }
}

在项目中创建配置文件。

spring项目配置多环境配置文件

 

idea的启动参数配置

spring项目配置多环境配置文件

 

项目中的使用方法

private static final String CDN_URL = CustomPropertisUtils.getProperties("CDN_URL");

PS:服务器上配置。export resources_path=dev  

#!/bin/bash
# author liuhongliang
# DEFINE

RETVAL=$?
#export CATALINA_BASE="$PWD"
export CATALINA_BASE="/data/8080"
export CATALINA_HOME="/data/tomcat"
export resources_path=dev
export CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=18088,suspend=n,server=y"
#export CATALINA_OPTS="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/8080/logs/heapdump -XX:+PrintGCDetails -Xloggc:/data/logs/heapdump.txt"
port=8080
#export JVM_OPTIONS="-Xms128m-Xmx1024m-XX:PermSize=128m-XX:MaxPermSize=512m"
export JAVA_OPTS='-Xms1028m -Xmx3072m'
case "$1" in
start)
if [ -f $CATALINA_HOME/bin/startup.sh ];then
echo $"StartTomcat"
$CATALINA_HOME/bin/startup.sh
fi
;;
stop)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];then
echo $"StopTomcat"
$CATALINA_HOME/bin/shutdown.sh
pid=$(ps -ef| grep "catalina.base=/data/""$port"| grep -v grep | awk '{print $2}')
sleep 3
kill -9 $pid
echo kill tomcat"$port" pid "$pid"
fi
;;
restart)
if [ -f $CATALINA_HOME/bin/shutdown.sh ];then
echo $"RestartTomcat"
$CATALINA_HOME/bin/shutdown.sh
pid=$(ps -ef| grep "catalina.base=/data/""$port"| grep -v grep | awk '{print $2}')
sleep 3
kill -9 $pid
echo kill tomcat"$port" pid "$pid"
sleep 3
echo start tomcat"$port"
$CATALINA_HOME/bin/startup.sh
fi
;;

*)
echo $"Usage:$0{start|stop}"
exit 1
;;
esac
exit $RETVAL