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

查看:378
本文介绍了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天全站免登陆