spring boot多个模块如何继承application.properties

Bao*_*ran 8 java spring dependency-properties maven spring-boot

我使用 Spring Boot 多个模块,我想从 Parent 继承 application.properties 。我有父模块:spring-ecommere-demo 和子模块:模型、核心和安全性。在父模块中,我放置了一些 jdbc 配置,如下所示:

application.properties(父模块)

spring.datasource.url=jdbc:mysql://localhost:3306/BaoTrung
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.show-sql=true
Run Code Online (Sandbox Code Playgroud)

在子模块安全性中,我的具体配置如下所示:

application-security.properties(安全模块)

app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000
Run Code Online (Sandbox Code Playgroud)

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

@SpringBootApplication(scanBasePackages = "springecommeredemo")
@PropertySources({
        @PropertySource("application-security.properties")
})
Run Code Online (Sandbox Code Playgroud)

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

描述:

无法配置数据源:未指定“url”属性,并且无法配置嵌入数据源。

原因:未能确定合适的驱动程序类

行动:

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

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

Jon*_*ohx 5

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

@SpringBootApplication(scanBasePackages = "springecommeredemo")
@PropertySource("application.properties")
@PropertySource("application-security.properties")
Run Code Online (Sandbox Code Playgroud)