BaoTrung Tran
BaoTrung Tran

Reputation: 460

How to inherit application.properties with spring boot multiple modules

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

Upvotes: 8

Views: 15111

Answers (4)

Charlo Poitras
Charlo Poitras

Reputation: 318

So I am building about the same framework as yours, here's what worked for me

Add the classpath prefix this to your @PropertySource annotation

@PropertySource("classpath:application-security.properties") // application-security on root

Also,
If the properties are located inside a package just do :

@PropertySource("classpath:/com/my/project/application.properties") // com.my.project package

If you want multiple sources, be sure to do the same :

@PropertySources({
    @PropertySource("classpath:xxx.properties"),
    @PropertySource("classpath:/com/my/project/xxx.properties")
})

Upvotes: 1

Shehan Simen
Shehan Simen

Reputation: 1306

I use spring boot 2.5. Let's say that I have a common module and a app module, which uses the common module. If I want to access all the properties defined in the common module properties file, then I will add the following to the top of the app module's properties file.

spring.config.import=classpath:common-module.properties

You can have any name for that "common-module" properties file.

Upvotes: 2

BaoTrung Tran
BaoTrung Tran

Reputation: 460

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

Upvotes: 1

Jonathan JOhx
Jonathan JOhx

Reputation: 5968

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

Upvotes: 5

Related Questions