簡體   English   中英

FileNotFoundException:對於 Spring 中的屬性文件 在 Tomcat 中部署的引導應用程序

[英]FileNotFoundException: for properties file in Spring Boot application deployed in Tomcat

我有一個 Spring 引導應用程序,它使用位於 src/main/config 中的屬性文件中的一些參數。 我需要在不同的環境中運行此應用程序:DEV、SIT、PROD 等,並且每次都需要更改該文件的屬性。

我在類路徑中有這個文件,在 src/main/config 中,也在 instance-config 的 tomcat 文件夾中。 當我在 Tomcat 上部署應用程序時,我需要它使用位於 tomcat 文件夾中的文件中的配置。

不過,我遇到了一個問題:它找不到我的屬性文件。

我試圖創建一些配置文件,以便僅在“dev”上使用來自類路徑(src/main/config)的屬性文件,但我無法完成構建,因為它給我錯誤:“無法解析配置 class [com. db.wmdl.config.TestConfig];嵌套異常是 java.io.FileNotFoundException: class 路徑資源 [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>

這是 class,其中包含對屬性文件的引用:

@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);
    }

}

如何使我的應用程序工作並從 instance-config 文件夾中讀取屬性?

如果您使用 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 配置文件處於活動狀態。 您需要專門激活開發配置文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM