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
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.
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.
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")
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With