本地和产品环境的不同属性变量(春季) [英] Different property variable for Local and prod Environment (Spring)

查看:84
本文介绍了本地和产品环境的不同属性变量(春季)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Spring Web应用程序,在这里我需要具有在本地环境中具有不同价值而在生产环境中具有其他价值的变量.

I am working on an Spring web application in where I need to have variable's that will have different value in local environment and other value in production environment.

例如(文件上传目录).对于本地环境和产品环境,我的文件上传目录是不同的.

For Eg (file upload directory). My file upload directory is different for local and prod environment.

当前,我正在通过检查主机名(如果为"localhost",然后为A,否则为B)并采用此方法来做到这一点.通过属性文件解决此问题的另一种方法是,有人能为我提供如何实现该目标的指针吗?

Currently I am doing it by checking the host name (if 'localhost' then A else B) and taking this approach. By there is another way to solve this problem via property files, Does anybody provide me pointers to how to approach that?

推荐答案

您可以基于当前的一个或多个弹簧轮廓加载属性.为了设置弹簧轮廓,我主要将名为spring.profiles.active的系统属性设置为所需的值,例如developmentproduction.

You can load properties based on the current spring profile or profiles. To set a spring profile I mostly set a system property named spring.profiles.active to the desired value e.g. development or production.

这个概念很简单.从系统属性中读取当前活动的配置文件.生成文件名并使用PropertySourcesPlaceholderConfigurer加载属性文件.使用PropertySourcesPlaceholderConfigurer将使通过@Value批注访问这些属性更加容易.请注意,此示例假定一个配置文件处于活动状态.当多个配置文件处于活动状态时,可能需要格外小心.

The concept is pretty simple. Read the currently active profile from the system property. Build the filename and load the properties file using a PropertySourcesPlaceholderConfigurer. Using the PropertySourcesPlaceholderConfigurer will make it easier the access those properties through @Value annotations. Note that this examples assumes one profile is active. It may need some extra care when multiple profiles are active.

基于Java的配置

@Configuration
public class MyApplicationConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        String activeProfile = System.getProperty("spring.profiles.active", "production");
        String propertiesFilename = "app-" + activeProfile + ".properties";

        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocation(new ClassPathResource(propertiesFilename));

        return configurer;
    }
}

您还可以导入带有@Profile注释的多个配置类. Spring将根据当前活动的配置文件选择要使用的配置.每个类都可以将其自己的PropertySourcesPlaceholderConfigurer版本添加到应用程序上下文中.

You could also import multiple configuration classes annotated with @Profile. Spring would select which configuration to use based on the currently active profiles. Every class could add it's own version of PropertySourcesPlaceholderConfigurer to the application context.

@Configuration
@Import({Development.class, Production.class})
public class MyApplicationConfiguration {}

@Configuration
@Profile("development")
public class Development {}

@Configuration
@Profile // The default
public class Production {}


正如艾默生·法鲁吉亚(Emerson Farrugia)在其评论中指出的那样,每类@Profile的选择对于选择PropertySourcesPlaceholderConfigurer来说有点过分.注释@Bean声明会容易得多.


As Emerson Farrugia stated in his comment the @Profile per class approach is a bit drastic for selecting a PropertySourcesPlaceholderConfigurer. Annotating a @Bean declaration would be much simpler.

@Configuration
public class MyApplicationConfiguration {

    @Bean
    @Profile("development")
    public static PropertySourcesPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        // instantiate and return configurer...
    }

    @Bean
    @Profile // The default
    public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
        // instantiate and return configurer...
    }
}

这篇关于本地和产品环境的不同属性变量(春季)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆