【问题标题】:Environment Specific application.properties file in Spring non-Boot applicationSpring 非引导应用程序中的环境特定 application.properties 文件
【发布时间】:2017-09-06 21:50:23
【问题描述】:

我想完成这个:Environment Specific application.properties file in Spring Boot application

在 Spring 非引导应用程序中。关于如何做到这一点的任何想法?现在我正在设置环境变量来告诉应用程序使用哪些属性,更愿意以“启动”方式进行。

我们将不胜感激。

【问题讨论】:

    标签: java spring


    【解决方案1】:

    为了表示几个环境使用配置文件。如果您想了解更多信息,请浏览 this 网站。我认为这正是您正在寻找的。​​p>

    更新 1:

    考虑到你有一个固定后缀的属性文件,并且你有一组用于不同环境的属性文件,例如,

    开发-it_wroks.properties, test-it_wroks.properties 等等等等。

    etc.it_wroks 是后缀

    从 active_env.properties 确定活动环境

    profiles.active: development
    #profiles.active: test
    #profiles.active: stage
    #profiles.active: production
    

    编写自定义属性解析器

    import org.apache.commons.configuration2.Configuration;
    import org.apache.commons.configuration2.FileBasedConfiguration;
    import org.apache.commons.configuration2.PropertiesConfiguration;
    import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
    import org.apache.commons.configuration2.builder.fluent.Parameters;
    public class MyPropertyUtil {
        public static String getValuesFromPerpertyFile(String filename,String key){
            String value = null;
            Configuration config = getConfiguration(filename);
            value = config.getString(key);
    
            return value;
    
        }
    
        public static Configuration getConfiguration(String file){
    
            Configuration config = null;
    
            try{
                Parameters params = new Parameters();
                FileBasedConfigurationBuilder<FileBasedConfiguration> 
                    builder =new FileBasedConfigurationBuilder
                    <FileBasedConfiguration>(PropertiesConfiguration.class)
                    .configure(params.properties().setFileName(file));
    
                config = builder.getConfiguration();
    
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
    
            }
    
            return config;
        }
    
    }
    

    现在是你的调用类

    import org.apache.log4j.Logger;
    public class MyCallingClass {
    
        final static Logger logger = Logger.getLogger(this.getClass());
    
        //Determine the active enviourment,You may determine this from os environment variable if you want
        String activeEnvironment = MyPropertyUtil.
                getValuesFromPerpertyFile("resource/active_env.properties"
                        ,"profiles.active");
    
        //Set the property file
        String myEnvSpecificValue = MyPropertyUtil.
                getValuesFromPerpertyFile("resource/"+activeEnvironment+"it_wroks.properties",
                        "my.property.string");
    
        //Do what you want to
        logger.info(myEnvSpecificValue);
    
    }
    

    【讨论】:

    • 请解释如何将其用于问题中所述的非引导。
    【解决方案2】:

    您可以根据环境添加 application-environment.properties。 Spring boot 应该会根据活动环境自动检测相应的属性文件。

    【讨论】:

    • 在 Spring boot 中总是基于配置文件,这也是你需要设置的环境属性。我想不出任何其他可能的方法。我想关注这个问题,如果有新的东西出现,会很有趣。
    猜你喜欢
    • 2015-11-18
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    相关资源
    最近更新 更多