如何通过Spring Boot多个模块继承application.properties

问题描述 投票:0回答:1

我使用spring boot启动多个模块,我想从parent继承application.properties。我有父模块:spring-ecommere-demo和子模块:model,core和security。在父模块中,我将一些配置jdbc设置为:

application.properties(父模块)

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

并且在子模块安全性中,我特定的配置如下:

application-security.properties(安全模块)

app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000

以及安全模块中Spring Boot应用程序中的配置如下:

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

但是当我运行它时,它抛出了异常

描述:

无法配置数据源:未指定'url'属性,无法配置嵌入式数据源。

原因:无法确定合适的驱动程序类别

动作:

请考虑以下事项:如果要嵌入式数据库(H2,HSQL或Derby),请将其放在类路径上。如果您有数据库设置要从特定配置文件加载,您可能需要激活它(配置文件开发者当前处于活动状态)。

这意味着子模块安全性不能从父项目继承属性。如何从父模块继承所有属性。因为我使用相同的数据库,所以我不想在项目中配置重复的jdbc。我要继承通用属性。请帮助

java spring maven spring-boot dependency-properties
1个回答
0
投票

您需要在Spring中添加多个Properties,我为@PropertySource添加了重复的注释,因为在Java 8之前,如果您需要使用同一注释的多个实例,则必须将它们包装在容器注释中。使用Java 8,就不再需要此代码,从而使代码更清晰,可读性更高。

@SpringBootApplication(scanBasePackages = "springecommeredemo")
@PropertySource("application.properties")
@PropertySource("application-security.properties")
© www.soinside.com 2019 - 2024. All rights reserved.