在多模块的Spring Boot项目中,application.yml
文件通常用于配置应用程序的各种属性。子模块中的 application.yml
文件可以帮助你为特定的模块定制配置,而不影响其他模块或整个应用程序的配置。以下是如何使用子模块中的 application.yml
文件的基本步骤和相关概念:
application.properties
和 application.yml
。application.yml
使用YAML格式,它更加易于人类阅读和编写。application.yml
文件来定义特定于该模块的配置。application.yml
文件通常包含该模块的默认配置。application-dev.yml
。src/main/resources
目录下创建 application.yml
文件。application.yml
文件中编写所需的配置项。例如:server:
port: 8081
spring:
datasource:
url: jdbc:mysql://localhost:3306/module_db
username: user
password: pass
spring.profiles.active
属性来激活特定的配置文件。例如,在父模块的 application.yml
中设置:spring:
profiles:
active: dev
这将激活所有模块中的 application-dev.yml
文件。
SPRING_APPLICATION_JSON
中的属性ServletConfig
初始化参数ServletContext
初始化参数System.getProperties()
)application-{profile}.yml
外部配置文件application-{profile}.yml
内部配置文件application.yml
外部配置文件application.yml
内部配置文件@PropertySource
注解问题:子模块的配置没有生效。
原因:
解决方法:
application.yml
文件位于子模块的 src/main/resources
目录下。application.yml
或 application-{profile}.yml
。application.yml
中使用 spring.profiles.active
来激活子模块的特定配置文件。假设我们有一个名为 user-service
的子模块,它的 application.yml
文件内容如下:
server:
port: 8082
user:
service:
enabled: true
在父模块的 application.yml
中,我们可以这样激活 user-service
的配置:
spring:
profiles:
active: user-service
这样,当启动应用程序时,Spring Boot会加载并应用 user-service
模块的配置。
通过这种方式,你可以有效地管理和使用多模块项目中的配置文件。
领取专属 10元无门槛券
手把手带您无忧上云