Spring-boot 应用配置 [英] Spring-boot application configuration

查看:64
本文介绍了Spring-boot 应用配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解如何配置 spring-boot 并且它提供了一个成熟且明智的覆盖机制,该机制记录良好,但我正在转换一个应用程序,该应用程序从不符合 spring-boot 机制的其他来源获取其配置.

I understand how to configure spring-boot and it provides a mature and sensible overriding mechanism that is documented well but I'm converting an application that gets its configuration from other source that is not in line with the spring-boot mechanism.

最终,应用程序使属性可用于使用@Value("$the.prop.key:default") 绑定或在spring xml 配置中使用的代码.无法更改这些属性的检索和绑定方式.

Ultimately the application makes properties available to the code that can be bound in using @Value("$the.prop.key:default") or used in spring xml config. The way that these properties are retrieved and bound cannot be changed.

我正在尝试配置嵌入式 tomcat 服务器端口,但我能做到的唯一方法是使用 application.properties.我可以将其更改为其他文件,甚至更改位置,但我无法更改机制(必须是文件).

I am trying to configure the embedded tomcat server port but the only way I can do this is using application.properties. I can change this to a different file and even change the location but I cannot change the mechanism (it has to be a file).

查看 spring-boot 代码,我看到它使用 EmbeddedServletContainerCustomizer 实现的概念来设置这些属性.好的,我将创建一个实现并使用它设置服务器属性.但不幸的是,你有 2 个实现试图做同样的事情 ServerProperties 和我的实现.代码对这些进行排序,但因为 ServerProperties 没有对其进行排序,所以它被设置为最低优先级,最后执行低优先级,因此我的实现被覆盖.

Looking into the spring-boot code I see it uses the notion of EmbeddedServletContainerCustomizer implementations to set these properties. Fine, I will create an implementation and set the server properties using this. But unfortunately you get 2 implementations trying to do the same thing ServerProperties and my implementation. The code orders these but because ServerProperties has not ordering it is set to the lowest priority and a low priority gets executed last and so my implementation gets overwritten.

相反,我实现了一个 BeanPostProcessor:

Instead I have implemented a BeanPostProcessor:

@Named
public class SpringBootCustomConfigurator implements BeanPostProcessor {

@Value("$the.prop.key:8080")
private int port;

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
        throws BeansException {
    if (bean instanceof ServerProperties) {
        ServerProperties serverProperties = (ServerProperties) bean;
        serverProperties.setPort(port);
    }
    return bean;
}
}

这做了我需要做的,但它不是一个令人满意的实现.有什么想法吗?

This does what I need to do but it isn't a satisfactory implementation. Any thoughts?

推荐答案

鉴于它是关于外部属性的新来源,我认为编写一个 ApplicationContextInitializer(或 ApplicationListener 在启动时侦听 Spring Boot 事件之一),它会在正确的位置将新的 PropertySource 添加到您的 Environment.您可以使用 SpringApplication 或使用 META-INF/spring.factories 注册初始化器.

Given that it's about a new source of external properties, I think it would be more natural to write an ApplicationContextInitializer (or ApplicationListener to listen for one of the Spring Boot events on startup) that adds a new PropertySource to your Environment in the right place. You can register initalizers with the SpringApplication or using META-INF/spring.factories.

这篇关于Spring-boot 应用配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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