gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:28:18 25 4
gpt4 key购买 nike

我使用 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

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

application-security.properties(安全模块)

app.jwtSecret= JWTSuperSecretKey
app.jwtExpirationInMs = 604800000

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

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

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

描述:

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).

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

最佳答案

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

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

关于java - spring boot多个模块如何继承application.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com