简体   繁体   English

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

[英]Set default properties in a library with spring-boot

I have many different services using spring-boot.我使用 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.show_banner、管理 url 等。

How can I do this?我怎样才能做到这一点? If I have the following:如果我有以下几点:

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

I'd like them to be merged with the service1 version taking precedence.我希望它们与优先的 service1 版本合并。 Instead, it seems that only the first one found on the classpath is used.相反,似乎只使用了在类路径中找到的第一个。

(Alternatively, using @Configuration classes would be even better, but I'm not sure they can be used to define many of the properties) (或者,使用 @Configuration 类会更好,但我不确定它们是否可用于定义许多属性)

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 SpringApplication it can use setDefaultProperties .如果您的公共库负责创建SpringApplication则它可以使用setDefaultProperties These values can be overridden by your services' application.properties .这些值可以被您的服务的application.properties覆盖。

Alternatively, your library could use @PropertySource on one of its @Configuration classes to configure, for example, library.properties as a source.或者,您的库可以在其@Configuration类之一上使用@PropertySource来配置,例如, library.properties作为源。 Again, these properties could then be overriden in your services' application.properties .同样,这些属性可以在您的服务的application.properties被覆盖。

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.properties which 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 other places.例如,您可以拥有application-service1.properties文件,当您使用spring.profiles.active=service1属性运行应用程序时,该文件将自动使用,该属性可以在命令行和其他地方指定。 If you don't specify this property, Spring Boot will fallback to the default application.properties file.如果不指定此属性,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.抱歉格式不正确,我仍然不熟悉编辑器。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM