如何使用 spring-boot 外部化数据源配置? [英] How can i externalize datasource configuration with spring-boot?

查看:89
本文介绍了如何使用 spring-boot 外部化数据源配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将现有的 spring-application 移动到 spring-boot,因此重新创建无需启动即可工作的内容.

I'm currently trying to move an existing spring-application to spring-boot and therefore recreate things that worked without boot.

我想从外部源配置一些属性(如 spring.datasource.*).特别是一个包含多个属性文件的文件夹.

I want to configure some properties (like spring.datasource.*) from an external source. specificly a folder with several properties files.

我设置了一个配置类来创建像这样的 propertyPlaceholder 配置器:

I set up a configuration class that creates propertyPlaceholder configurers like this:

@Configuration
public class PropertySourceConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer defaultsPlaceHolderConfigurer() throws IOException {
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/*-defaults.properties"));
    propertyConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertyConfigurer;
}

@Bean
public static PropertySourcesPlaceholderConfigurer externalPlaceHolderConfigurer() throws IOException {
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertyConfigurer.setLocations(new
            PathMatchingResourcePatternResolver().getResources("file:/my-config-path/*.properties"));
    propertyConfigurer.setOrder(1);
    propertyConfigurer.setIgnoreUnresolvablePlaceholders(true);
    return propertyConfigurer;
}

这似乎适用于大多数事情(例如 amqp 或我自己的配置属性),但是当我尝试使用 spring-data-jpa 时,它们会被忽略.基本上在这些文件中设置 spring.datasource.url(以及用于自动配置的其他内容)无效.

This seems to work for most things (like amqp or my own config properties) but when i try to use spring-data-jpa they are ignored. basicly setting spring.datasource.url (and other things used for auto-config) in those files has no effect.

查看 PropertySourcesPropertyResolver 的日志,我发现这些配置器属于 localProperties 组,在查找 spring.datasource.* 时不使用该组..

looking through the logs of the PropertySourcesPropertyResolver i figured out that these configurer fall under the localProperties group which is not used when looking for spring.datasource.*.

有没有办法解决这个问题,或者有什么更好的方法可以将外部属性文件添加到我的上下文中?

is there a way to fix this or a better way to add external properties files to my context?

我知道我可以设置 spring.config.location 来做类似的事情,但我无法将命令行属性传递给我的应用程序,需要在我的应用程序中执行此配置.afaik 这对这个属性是不可能的.

I know i could set spring.config.location to do something similar but i can not pass command-line properties to my application and need to do this config from within my application. afaik this is not possible with this property.

设置spring.config.location:

尝试 1:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CampaignServiceStarter.class);
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.setInitParameter("spring.config.location", "file:/my-config-path/*.properties");
    }
}

尝试 2:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CampaignServiceStarter.class).properties("spring.config.location=file:/my-config-path/*.properties");
    }
}

在这两种情况下,根本没有选择外部属性(即使在它以前工作的地方,如 amqp 配置)

in both cases the external properties were not picked up at all (even in places where it worked before, like the amqp config)

推荐答案

如何使用外部配置在本节 Spring Boot 参考指南.

How to use external configuration is explained in this section of the Spring Boot Reference Guide.

spring.config.location 是包含 application.properties 文件的目录的路径.它需要一个逗号分隔的值列表,因此您可以指定多个路径.它不需要通配符.它是一个路径,因此不是匹配多个属性文件的表达式.如果您想更改默认的 application,请使用 spring.config.name 使其成为其他内容.

The spring.config.location is a path to the directory which contains your application.properties file. It takes a comma separated list of values so you could specify multiple paths. It doesn't take the wildcard. It is a path so not an expression to match multiple property files. If you want to change the default application then use the spring.config.name to make it something else.

Spring Boot 的默认值被认为是 Spring Boot 的其余部分(使用默认配置等).

The defaults of Spring Boot are opinionated as the rest of Spring Boot (with the default configuration etc.).

如果您想进行更广泛的(预)配置,您应该使用 ApplicationContextInitializer 手动将 PropertySource 添加到 Environment.这是提到 此处 在 Spring Boot 参考指南中.

If you want to do more extensive (pre) configuration you should use an ApplicationContextInitializer to manually add the PropertySources to the Environment. This is mentioned here in the Spring Boot Reference Guide.

初始化程序的外观示例.

An example of how the initializer might look.

public class ConfigurationInitializer implements ApplicationContextInitializer {

    private static final String DEFAULT_PROPS = "classpath*:/*-defaults.properties";
    private static final String EXTERNAL_PROPS = "file:/my-config-path/*.properties";

    public void initialize(ConfigurableApplicationContext applicationContext) {
        final Resource[] defaultConfigs = applicationContext.getResources(DEFAULT_PROPS);
        final Resource[] externalConfigs = applicationContext.getResources(EXTERNAL_PROPS);

        final ConfigurableEnvironment env = applicationContext.getEnvironment();
        final MutablePropertySources mps =  env.getPropertySources();
        for (Resource r : externalConfigs) {
            mps.addLast(new ResourcePropertySource(r.getFilename(), r);
        }
        for (Resource r : defaultConfigs) {
            mps.addLast(new ResourcePropertySource(r.getFilename(), r);
        }   
    }
}

然后在构建应用程序对象时按如下方式添加它.

Then when building your application object add it as follows.

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CampaignServiceStarter.class)
            .initializers(new ConfigurationInitializer());
    }
}

现在应该将配置添加到属性源列表中.

Now the configs should be added to the list of property sources.

这篇关于如何使用 spring-boot 外部化数据源配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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