【问题标题】:How to load property file based on spring profiles如何根据弹簧配置文件加载属性文件
【发布时间】:2017-05-06 21:30:43
【问题描述】:

如何创建项目架构以支持多种环境。在 spring 的帮助下,每个环境都会有来自不同属性文件的不同数据源,例如(dev-propertfile、test-propertyFil、Production-propertyfile)

org.springframework.core.env.Environment;

【问题讨论】:

标签: java spring hibernate


【解决方案1】:

我将逐步介绍 Spring Boot 应用程序的过程。

  1. /src/main/resources/application.properties 中提及 spring.profiles.active=dev(或 Prod)
  2. 创建 /src/main/resources/application-dev.properties 并在此处提供您的自定义开发配置。
  3. 创建 /src/main/resources/application-prod.properties 并在此处提供您的自定义产品配置。

运行。

【讨论】:

    【解决方案2】:

    将属性文件放在与application.property相同的位置并关注 命名约定application-{profile}.properties 喜欢 application-dev.properties,application-test.properties, application-prod.properties

    application.properties 中设置spring.profiles.active=dev,test

    【讨论】:

    【解决方案3】:

    对于 Spring Boot 应用程序,即使使用 YAML 文件也可以轻松运行

    spring: 
      profiles: dev
      property: this is a dev env
    ---
    spring: 
      profiles: prod
      property: this is a production env 
    ---
    

    但是,对于 Spring MVC 应用程序,它需要更多的工作。看看this link

    基本上,它涉及2个步骤

    1. 在 servlet 上下文中获取 Spring Profile

    如果您在服务器上设置了配置文件并希望它在您的应用程序中检索它,您可以使用 System.getProperty 或 System.getenv 方法。 如果没有找到配置文件,这里是获取配置文件并将其默认为本地配置文件的代码。

    private static final String SPRING_PROFILES_ACTIVE = "SPRING_PROFILES_ACTIVE";
    String profile;
    
    /**
     * In local system getProperty() returns the profile correctly, however in docker getenv() return profile correctly
     * */
    protected void setSpringProfile(ServletContext servletContext) {
    if(null!= System.getenv(SPRING_PROFILES_ACTIVE)){
        profile=System.getenv(SPRING_PROFILES_ACTIVE);
    }else if(null!= System.getProperty(SPRING_PROFILES_ACTIVE)){
        profile=System.getProperty(SPRING_PROFILES_ACTIVE);
    }else{
        profile="local";
    }
    log.info("***** Profile configured  is  ****** "+ profile);
    
    servletContext.setInitParameter("spring.profiles.active", profile);
    }
    
    1. 要访问 application-dev.properties,现在说您需要使用 类级别的@Profile("dev")

    以下代码将获取 application-dev.properties 和 common.properties

    @Configuration
    @Profile("dev")
    public class DevPropertyReader {
    
    
        @Bean
        public static PropertyPlaceholderConfigurer properties() {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        Resource[] resources = new ClassPathResource[] { new ClassPathResource("properties/common.properties"), new ClassPathResource("properties/application-dev.properties") };
        ppc.setLocations(resources);
        ppc.setIgnoreUnresolvablePlaceholders(true);
        return ppc;
        }
    }
    

    要访问说 application-prod.properties,您必须在类级别使用 @Profile("prod")。更多详情可以found here

    【讨论】:

      【解决方案4】:

      看看Spring Profile。您将定义一组配置文件配置,例如测试、开发、生产。然后,当您启动应用程序时,您可以定义它应该使用的配置文件。

      这里有一些tutorials的使用方法。

      这家伙和你有同样的问题:How to config @ComponentScan dynamic?

      【讨论】:

        【解决方案5】:

        我们想要一种根据 Spring MVC 项目中的环境(spring 配置文件)从 application-<your_env>.properties 文件加载不同属性的方法,因此我们实现了类似这样的配置类。

        @Configuration
        @PropertySource({ "classpath:application-${envTarget:dev}.properties" })
        @Data
        public class EnvironmentConfig {
        
            @Value("${files.s3.accessId:}")
            String s3AccessId;
        
            @Value("${files.s3.accessToken:}")
            String s3AccessToken;
            .
            .
            .
        }
        

        然后我们在需要使用它的类中加载EnvironmentConfig

        在运行应用程序时,您只需传递-DenvTarget=<your_env>,它就会从项目的src/resources文件夹中提取application-<your_env>.properties文件。

        在上面的代码中,当没有指定envTarget 时,它将从application-dev.properties 文件中加载值。

        感谢 Karthikeyan Muthurangam 提出这个聪明的解决方案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-27
          • 1970-01-01
          相关资源
          最近更新 更多