【问题标题】:Spring Boot Externalizing properties not workingSpring Boot 外部化属性不起作用
【发布时间】:2016-08-06 17:15:39
【问题描述】:

我查看了以下主题并遵循了那里给出的内容。我的属性覆盖仍然没有发生

  1. Spring Boot - Externalized properties
  2. Profile Specific Property Enablement
  3. Spring Boot External Config

我在 Tomcat 8.0.33 和 Spring boot starter web 上,并在我的 setenv.sh 中得到了这个

export JAVA_OPTS="$JAVA_OPTS -Dlog.level=INFO -Dspring.config.location=file:/opt/jboss/apache-tomcat-8.0.33/overrides/ -Dspring.profiles.active=dev"

在 overrides 文件夹中我有 2 个文件

1) application.properties 2)application-dev.properties

application.properties 中有一个条目

spring.profiles.active=dev

我看到正确的 log.level 已输入到我的代码中,这意味着该命令正在运行。只是我不知道为什么我的覆盖没有按预期发生

我的工作区中没有任何 `PropertyPlaceholderConfigurer 代码。我什至不确定我是否需要 1

【问题讨论】:

    标签: java spring tomcat properties spring-boot


    【解决方案1】:

    我不使用这种方法来外部化属性。首先,我会为你的方法尝试一个建议,然后我会告诉你我正在使用什么。

    对您的方法的建议是使用 file:/// 而不是 file:/ 与 Spring 一样,我发现当不通过冒号后的三个斜杠时,它无法识别该属性。

    我为您创建了一个示例项目,available here with instructions

    现在是我使用的方法。

    我为每个配置文件定义了一个配置文件,并将 application.properties 文件保存在 src/main/resources 下。

    然后我在每个配置文件上使用@Profile 和@PropertySource 注解。

    例如:

    @Configuration
    @Profile("dev")
    @PropertySource("file:///${user.home}/.devopsbuddy/application-dev.properties")
    public class DevelopmentConfig {
    
    @Bean
    public EmailService emailService() {
        return new MockEmailService();
    }
    
    @Bean
    public ServletRegistrationBean h2ConsoleServletRegistration() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new WebServlet());
        bean.addUrlMappings("/console/*");
        return bean;
    }
    }
    

    @Configuration
    @Profile("prod")
    @PropertySource("file:///${user.home}/.devopsbuddy/application-prod.properties")
    public class ProductionConfig {
    
    @Bean
    public EmailService emailService() {
        return new SmtpEmailService();
    }
    }
    

    我还有一个对所有配置文件都有效的配置文件,我称之为ApplicationConfig,如下:

    @Configuration
    @EnableJpaRepositories(basePackages = "com.devopsbuddy.backend.persistence.repositories")
    @EntityScan(basePackages = "com.devopsbuddy.backend.persistence.domain.backend")
    @EnableTransactionManagement
    @PropertySource("file:///${user.home}/.devopsbuddy/application-common.properties")
    public class ApplicationConfig {
    }
    

    我的 src/main/resources/application.properties 文件如下所示:

    spring.profiles.active=dev
    default.to.address=me@example.com
    token.expiration.length.minutes=120
    

    当然,我可以通过将 spring.profile.active 属性作为系统属性传递来将其外部化,但对于我的情况,现在它很好。

    在运行应用程序时,如果我传递“dev”配置文件,Spring 将加载 DevelopmentConfig 类中定义的所有属性和 Bean 以及 ApplicationConfig 中的所有属性和 Bean。如果我通过“prod”,则将改为加载 ProductionConfig 和 ApplicationConfig 属性。

    我正在完成有关如何使用 Security、Email、Data JPA、Amazon Web Services、Stripe 等创建 Spring Boot 网站的课程。如果您愿意,您可以注册您的兴趣here,当课程开放注册时您会收到通知。

    【讨论】:

    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 2018-02-25
    • 2015-11-06
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多