java 使用 spring-boot 在库中设置默认属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28892948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:21:53  来源:igfitidea点击:

Set default properties in a library with spring-boot

javaspringspring-boot

提问by Eric D

I have many different services using spring-boot. I'd like to set up some configuration that is common for each, but allow the services to have their own properties and override them if they want. Example properties include spring.show_banner, management url ones, etc.

我使用 spring-boot 有许多不同的服务。我想设置一些通用的配置,但允许服务拥有自己的属性并根据需要覆盖它们。示例属性包括 spring.show_banner、管理 url 等。

How can I do this? If I have the following:

我怎样才能做到这一点?如果我有以下几点:

  • service-common with src/main/resources/application.yml with default properties
  • service1 with src/main/resources/application.yml with its own properties
  • 带有默认属性的 src/main/resources/application.yml 的 service-common
  • service1 带有 src/main/resources/application.yml 和它自己的属性

I'd like them to be merged with the service1 version taking precedence. Instead, it seems that only the first one found on the classpath is used.

我希望它们与优先的 service1 版本合并。相反,似乎只使用了在类路径上找到的第一个。

(Alternatively, using @Configuration classes would be even better, but I'm not sure they can be used to define many of the properties)

(或者,使用 @Configuration 类会更好,但我不确定它们是否可用于定义许多属性)

回答by Andy Wilkinson

There are several options available to you, all based on the order in which property sources are considered.

有多种选项可供您使用,所有选项均基于考虑属性源顺序

If your common library is responsible for creating the SpringApplicationit can use setDefaultProperties. These values can be overridden by your services' application.properties.

如果您的公共库负责创建SpringApplication它可以使用setDefaultProperties. 这些值可以被您的服务覆盖application.properties

Alternatively, your library could use @PropertySourceon one of its @Configurationclasses to configure, for example, library.propertiesas a source. Again, these properties could then be overriden in your services' application.properties.

或者,您的库可以使用@PropertySource它的一个@Configuration类来配置,例如,library.properties作为源。同样,这些属性可以在您的服务中被覆盖application.properties

回答by Gabriel Ruiu

I am not sure what you mean by merging them.

我不确定合并它们是什么意思。

But I'm assuming that in the end, you are describing the situation where you have profile-specific configuration. Because, any properties that are specific to a certain service can be managed/injected using Spring profiles, which will always take precedence over default property files(see documentation).

但我假设最后,您正在描述具有特定于配置文件的配置的情况。因为,特定于某个服务的任何属性都可以使用 Spring 配置文件进行管理/注入,这将始终优先于默认属性文件(请参阅文档)。

For example, you can have the file application-service1.propertieswhich would automatically be used when you run your app with the property spring.profiles.active=service1, which can be specified in the command line and otherplaces. If you don't specify this property, Spring Boot will fallback to the default application.propertiesfile.

例如,您可以拥有application-service1.properties文件,当您使用spring.profiles.active=service1属性运行应用程序时,该文件将自动使用,该属性可以在命令行和其他地方指定。如果不指定此属性,Spring Boot 将回退到默认的application.properties文件。

And you can of course write the common properties in both files:

您当然可以在两个文件中编写公共属性:

application.properties

应用程序属性

service.url=http://localhost:8080/endpoint
service.user=admin
service.password=admin

application-service1.properties

application-service1.properties

service.url=http://api.service.com/endpoint
service.user=admin
service.password=aosdnoni3

Hope this helps.

希望这可以帮助。

Sorry for the bad formatting, I'm still unfamiliar with the editor.

抱歉格式不正确,我仍然不熟悉编辑器。

回答by Dennis Crissman II

public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationEnvironmentPreparedEvent) {
            ApplicationEnvironmentPreparedEvent envEvent = (ApplicationEnvironmentPreparedEvent) event;
            ConfigurableEnvironment env = envEvent.getEnvironment();
            Properties props = new Properties();
            //set props as desired
            env.getPropertySources()
                    .addFirst(new PropertiesPropertySource("customname", props));
    }
}

Then in src/main/resources/META-INF/spring.factories, add line: org.springframework.context.ApplicationListener=mypackage.MyApplicationListener

然后在 src/main/resources/META-INF/spring.factories 中,添加一行: org.springframework.context.ApplicationListener=mypackage.MyApplicationListener