gpt4 book ai didi

java - 文件未找到异常 : for properties file in Spring Boot application deployed in Tomcat

转载 作者:行者123 更新时间:2023-12-01 16:48:04 27 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,它使用位于 src/main/config 中的属性文件中的一些参数。我需要在不同的环境中运行这个应用程序:DEV、SIT、PROD 等,并且每次都需要更改该文件的属性。

我在类路径中的 src/main/config 以及 tomcat 文件夹的实例配置中都有此文件。当我在 Tomcat 上部署应用程序时,我需要它使用 tomcat 文件夹中文件中的配置。

但是我有一个问题:它找不到我的属性文件。

我尝试创建一些配置文件,以便仅在“dev”上使用类路径(src/main/config)中的属性文件,但我无法完成构建,因为它给了我错误:“无法解析配置类[com.db.wmdl.config.TestConfig];嵌套异常为 java.io.FileNotFoundException:类路径资源 [wmdl_options_app.properties] 无法打开,因为它不存在”

我在 pom 中有此配置文件部分:

<profiles>
<profile>
<id>dev</id>
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/config</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>tomcat</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging.type>war</packaging.type>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.4.RELEASE</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>

这是包含对属性文件的引用的类:

@SpringBootApplication( scanBasePackages = {"com.db.wmdl.glue2g.*","com.db.wmdl.fo.service","com.db.wmdl.fo.persistence", "com.db.wmdl.fo.serversync"})
@PropertySource("classpath:wmdl_options_app.properties")
@EnableScheduling
public class Application {

@PostConstruct
public void init() throws IOException {
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
context.setConfigLocation(new ClassPathResource("log4j-dl-fo-config.xml").getURI());
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

如何使我的应用程序正常工作并从实例配置文件夹中读取属性?

最佳答案

如果您使用 tomcat 配置文件运行,则还需要将这些添加到 tomcat 配置文件中。

<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/config</directory>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
</build>

此外,默认情况下,tomcat 配置文件处于 Activity 状态。您需要专门激活开发配置文件。

关于java - 文件未找到异常 : for properties file in Spring Boot application deployed in Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61739841/

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