Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

Spring Boot Inherit application.properties from dependency

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)...

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 Main class looks like this:

@SpringBootApplication
public class CommonApplication extends SpringBootServletInitializer {

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

Project1 Main class looks like this:

public class Project1 extends CommonApplication {

    public static void main(String[] args) {
        run(new SpringApplication(Project1.class), args);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use PropertySource annotation and provide two sources for your app:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...