Spring Boot 外部化属性不起作用 [英] Spring Boot Externalizing properties not working

查看:37
本文介绍了Spring Boot 外部化属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have looked at the below threads and followed things given there. Still my property override is not happening

  1. Spring Boot - 外化属性
  2. 配置文件特定属性启用
  3. Spring Boot 外部配置

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

I am on Tomcat 8.0.33 and Spring boot starter web and got this in my 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"

在覆盖文件夹中我有 2 个文件

And in the overrides folder I got 2 files

1) application.properties2) application-dev.properties

application.properties 中只有一个条目

The application.properties has a single entry in it

spring.profiles.active=dev

我看到正确的 log.level 被提供给我的代码,这意味着这个命令正在工作.只是我不知道为什么我的覆盖没有按预期发生

I see that the proper log.level is fed to my code which means this command is working. Its just that I am clueless as to why my override is not happening as expected

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

I don't have any `PropertyPlaceholderConfigurer code in my workspace. I am not even sure if I need 1

推荐答案

我不使用这种方法来外部化属性.首先,我会尝试对您的方法提出建议,然后我会向您展示我正在使用的方法.

I don't use this method to externalise properties. First, I'll try a suggestion for your method and then I'll show you what I'm using.

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

The suggestion for your method is to use file:/// instead of file:/ as with Spring I found that when not passing the three slashes after the colon it didn't recognise the property.

我已经为您创建了一个示例项目,可在此处获得说明.

I've created a sample project for you, available here with instructions.

现在说说我使用的方法.

Now for the method I use.

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

I define a Configuration file for each profile and I keep the application.properties file under src/main/resources.

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

Then I use the @Profile and @PropertySource annotations on each configuration file.

例如:

@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,如下:

I have also got a Configuration file that is valid for all profiles, which I call ApplicationConfig, as follows:

@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 文件如下所示:

My src/main/resources/application.properties file looks like the following:

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

当然,我可以通过将 spring.profile.active 属性作为系统属性传递来将其外部化,但就我而言,现在很好.

Of course I could externalise the spring.profile.active property by passing it as a system property but for my case and for now it's fine.

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

When running the application, if I pass the "dev" profile, Spring will load all properties and Beans defined in the DevelopmentConfig class plus all those in ApplicationConfig. If I pass "prod", the ProductionConfig and ApplicationConfig properties will be loaded instead.

我正在完成一门关于如何使用安全性、电子邮件、数据 JPA、Amazon Web Services、Stripe 等创建 Spring Boot 网站的课程.如果你愿意,你可以注册你的兴趣 此处,您将在课程开放注册时收到通知.

I'm completing a course on how to create a Spring Boot website with Security, Email, Data JPA, Amazon Web Services, Stripe and much more. If you want, you can register your interest here and you will get notified when the course is open for enrolment.

这篇关于Spring Boot 外部化属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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