我需要在 spring 中使用配置文件。我使用本地 Tomcat。 有 maven 项目,所以,在 pom.xml 中我添加:
<profiles>
<profile>
<id>dev</id>
<activation>
<property>
<name>spring.profiles.active</name>
<value>dev</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>at1</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>spring.profiles.active</name>
<value>at1</value>
</property>
</activation>
</profile>
</profiles>
并在 application.properties 中添加了 spring.profiles.active=${activatedProperties} 注意:spring.profiles.active=@activatedProperties@ 也试过了
并且有两个文件 application-at1.properties 和 application-dev.properties
当使用 -Dspring.profiles.active=dev 构建 war 时,会出现错误消息 - 找不到此文件中的参数。
我的 tomcat 定制是:
最佳答案
不能确切地说出你在哪里,但在我看来你正在使用一个属性占位符,Spring Boot 没有选择一个配置文件,因为占位符实际上没有任何值(value)。
您可以通过以下方式进行配置:
使用属性占位符:
应用程序属性
spring.profiles.active=${activatedProperties}
pom.xml
<property>
<name>activatedProperties</name>
<value>dev</value>
</property>
只需指定运行时参数
从 pom.xml 中删除属性并使用一些默认值调整您的 application.properties 或根本不指定它
spring.profiles.active=at1 #you can remove this line if you want.
然后使用参数-Dspring.profiles.active=dev
运行war
使用 Maven 配置文件
您可以使用 -P dev
运行 maven 以确保使用正确的配置文件执行目标。
关于java - 无法在spring + maven + tomcat中启动配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52623241/