Spring Boot 2 : How to load each application.yml files of different modules into a given web or batch runner

我们两清 提交于 2020-12-05 19:41:49

问题


I create a Spring Boot Application, and I'm wondering if it is possible to load upmteens application.yml files of different modules.

I have this structure :

myProject
|__ moduleCommons
|   |__ application.yml
|__ moduleWeb
|   |__ application.yml
|   |__ MyProjectWebApplication.java
|__ moduleBatch
    |__ application.yml
    |__ MyProjectBatchApplication.java

To launch the Spring Boot application of web module, I run the MyProjectWebApplication.java.

To launch a Spring Batch of batch module, I run the MyProjectBatchApplication.java.

Both of them need some commons properties, which are set in application.yml file of commons module (like database configuration, directory paths for upload files, etc.). And some others properties are specific to each module, so set in application.yml file of web module (like servlet context path, jwt settings) or batch module (like mailer settings).

And, for example, MyProjectWebApplication class into which i would like to load web and commons application.yml files :

package com.myProject.web;

@SpringBootApplication(
        scanBasePackages = {"com.myProject.web", "com.myProject.commons"},
        exclude = {DataSourceAutoConfiguration.class, LiquibaseAutoConfiguration.class}
)
@EnableJpaRepositories(
        basePackages = "com.myProject.commons.datasources.defaut.repository",
        repositoryFactoryBeanClass = EnversRevisionRepositoryFactoryBean.class
)
@ContextConfiguration(classes = {MyProjectDbConfig.class})
public class MyProjectWebApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyProjectWebApplication.class);
    }

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

But it seems I load only web module application.yml file with this configuration...

Help ! :)


回答1:


You could make it by setting application.yml in common module and /config/application.yml in the depending modules. If you need a third fallback for test you can use /config/application-test.yml with @ActiveProfiles("test") on every @Test.

That way your set up would be: test properties -> main properties -> commons properties

This works on a build-from-scratch testcase, but I am not sure of the consequences of having main properties under config when running on different environments (https://github.com/spring-projects/spring-boot/issues/9128, i.e, whether commons application.yml is guaranteed to precede other dependencies potential ones)




回答2:


You can rename your application file in common module to application.yaml or application.propertes, and don't change it in other modules. It's a hack, but it's work. If you use same files in your target and common module, Spring will take only one, which in target module. So you need to add another default file or one more profile.



来源:https://stackoverflow.com/questions/55078650/spring-boot-2-how-to-load-each-application-yml-files-of-different-modules-into

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!