如何通过Spring Boot多个模块继承application.properties [英] How to inherit application.properties with spring boot multiple modules
问题描述
我使用spring boot多个模块,我想从parent继承application.properties.我有一个父模块:spring-ecommere-demo和一个子模块:model,core和security.在父模块中,我将一些配置jdbc设置为:
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 (父模块)
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 (安全模块)
app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000
安全模块中的Spring Boot应用程序中的配置如下:
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
说明:
无法配置数据源:未指定'url'属性,并且无法配置任何嵌入式数据源.
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
动作:
请考虑以下事项:如果要嵌入式数据库(H2,HSQL或Derby),请将其放在类路径上.如果您有数据库设置要从特定配置文件加载,您可能需要激活它(配置文件开发者当前处于活动状态).
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).
这意味着子模块安全性不能从父项目继承属性.如何从父模块继承所有属性.因为我使用相同的数据库,所以我不想在项目中配置重复的jdbc.我要继承通用属性.请帮忙
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
推荐答案
您需要添加多个属性
可以在Spring中访问,我为 @PropertySource
添加了重复的注释从 Java 8
开始,如果您需要使用同一注释的多个实例,则必须将它们包装在容器注释中.使用 Java 8
,不再需要此代码,从而可以编写更清晰,更易读的代码.
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")
这篇关于如何通过Spring Boot多个模块继承application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!