How to inherit application.properties with spring boot multiple modules

三世轮回 提交于 2021-01-27 06:53:26

问题


I using spring boot multiple modules and i want inherit application.properties from parent . I have parent module : spring-ecommere-demo and sub module : model , core and security. In parent modules i put some config jdbc look like :

application.properties (parent module)

spring.datasource.url=jdbc:mysql://localhost:3306/BaoTrung
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.show-sql=true

And in sub module security i specific config look like:

application-security.properties (security module)

app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000

And config in Spring Boot application in security module look like :

@SpringBootApplication(scanBasePackages = "springecommeredemo")
@PropertySources({
        @PropertySource("application-security.properties")
})

But when i run it, it throw me exception

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (the profiles dev are currently active).

It mean sub module security can't inherit properties from parent project. How to inherit all properties from parent module. Because i using same database , i don't want config duplicate jdbc in my project. I want inherit common properties.Please help


回答1:


You need to add multiple Properties can be accessed in Spring, I added duplicated annotation for @PropertySource since before Java 8 if you needed to use multiple instances of the same annotation, they had to be wrapped in a container annotation. With Java 8, that's no longer necessary, allowing for cleaner, more readable code.

@SpringBootApplication(scanBasePackages = "springecommeredemo")
@PropertySource("application.properties")
@PropertySource("application-security.properties")



回答2:


I found solution this here : Maven Multi Module insists on duplicating datasource application.properties in business module

Only create sub-module : example :server-config and run it. In sub module : security add server-config as dependency and run it. It work for me



来源:https://stackoverflow.com/questions/58091902/how-to-inherit-application-properties-with-spring-boot-multiple-modules

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