Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 912
BaoTrung Tran Avatar asked Sep 25 '19 05:09

BaoTrung Tran


People also ask

Can we have 2 application properties in spring boot?

So you can create two applications. properties files one for the original database and the other for the test database which you use during development.

How do I manage multiple properties in spring boot?

To add different files you can use the spring. config. location properties which takes a comma separated list of property files or file location (directories). The one above will add a directory which will be consulted for application.


3 Answers

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")
like image 175
Jonathan JOhx Avatar answered Oct 13 '22 04:10

Jonathan JOhx


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.

like image 2
Shehan Simen Avatar answered Oct 13 '22 05:10

Shehan Simen


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

like image 1
BaoTrung Tran Avatar answered Oct 13 '22 06:10

BaoTrung Tran