java - 无法在spring + maven + tomcat中启动配置文件

标签 java spring maven tomcat

我需要在 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 定制是:

enter image description here

最佳答案

不能确切地说出你在哪里,但在我看来你正在使用一个属性占位符,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/

相关文章:

java - NoSuchMethodFound 错误 : dependency error in GWTP. ResourceDelegate 使用情况

Java读取不同数据库表

java - 等待 API 响应的 RESTTemplate

java - Spring 框架中的构造函数依赖注入(inject)

java - 使用 Spring IoC 在 Guava EventBus 中自动注册 beans

java - 从 Selenium 中的配置属性文件读取值时出现空指针异常

maven 从当前目录复制单个文件

java - 特定口味的变体如何发挥作用?

java - 如何关闭父窗口并继续在子窗口上执行

java - 嵌套流异常