Spring Boot 从依赖继承 application.properties [英] Spring Boot Inherit application.properties from dependency

查看:44
本文介绍了Spring Boot 从依赖继承 application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 5 个 Spring Boot 项目.所有这些都对带有一些共享/公共类的 Spring Boot 项目 No 6 有 Maven 依赖.5 个独立的项目在每个 application.properties 中分配了很多公共属性,我想将它们抽象并移动到公共项目中.整体看起来是这样的:

Let's say I have 5 Spring Boot Projects. All of them have a Maven dependency on a Spring Boot project No 6 with some shared/common classes. 5 independent projects have a lot of common properties assigned at each application.properties, which I'd like to abstract and move them to common project. Overall it looks like this:

                                            Project 1 (app.properties)
Common Project (app-common.properties) <--- Project 2 (app.properties)
                                            Project 3 (app.properties)...

当前的问题是 app-common.properties 位于 project1.jar/lib/common-project.jar 中,而 app-common.properties 显然不会在启动时加载.

Current problem is that app-common.properties is inside project1.jar/lib/common-project.jar and app-common.properties apparently do not load upon startup.

有没有办法从依赖中扩展它?

Is there a way to extend it from a dependency?

CommonProject 主类如下所示:

CommonProject Main class looks like this:

@SpringBootApplication
public class CommonApplication extends SpringBootServletInitializer {

    protected static void run(SpringApplication application, String[] args) {
        application.run(args);
    }
}

Project1 主类如下所示:

Project1 Main class looks like this:

public class Project1 extends CommonApplication {

    public static void main(String[] args) {
        run(new SpringApplication(Project1.class), args);
    }
}

推荐答案

使用 PropertySource 注解并为您的应用提供两个来源:

Use PropertySource annotation and provide two sources for your app:

@PropertySources({
        @PropertySource("classpath:app-common.properties"),
        @PropertySource("classpath:app.properties")
    })

可以在那里找到更多详细信息https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

more details can be found there https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

这篇关于Spring Boot 从依赖继承 application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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