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

查看:224
本文介绍了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引导外部配置

我在tomcat 8.0.33和Spring引导启动网上,并在我的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.properties
2) application-dev.properties

申请n.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.

现在我使用的方法。

我为每个配置文件定义了一个配置文件,并将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,亚马逊网络服务,条纹等创建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天全站免登陆