簡體   English   中英

getting File java.io.FileNotFoundException: in IntelijIdea but working fine in Eclipse when loading external yaml file in bean in Spring boot

[英]getting File java.io.FileNotFoundException: in IntelijIdea but working fine in Eclipse when loading external yaml file in bean in Spring boot

我在 SpringBoot 應用程序中將外部 yml 文件加載到 bean 時遇到問題。 當我在intellij Idea 2019 2.3 版中運行它時,但是當我直接在Eclipse中運行它時,相同的代碼工作得非常好

下面是代碼

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.FileSystemResource;


@Configuration
@EnableConfigurationProperties({
    MyWebServicesConfiguration.class
})
public class MyWebServicesConfiguration {


    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
      PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();

      YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
      yaml.setResources(new FileSystemResource("./conf/sql.yml"));
      propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());

      return propertySourcesPlaceholderConfigurer;
    }
}

Spring開機的主要方法如下

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(
    exclude = { CassandraAutoConfiguration.class, MongoAutoConfiguration.class },
    scanBasePackages = { "com.this.that.myservices" }
)
@EnableScheduling
public class MyWebServices  {
    public static void main(final String... args) {

        final SpringApplication app = new SpringApplication(MyWebServices.class);
        app.run(args);
    }
}

以下是我僅在 intellij Idea 中運行但在 eclipse 中運行時才得到的文件未找到異常。

ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'properties' defined in class path resource [com/this/that/MyWebServicesConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.context.support.PropertySourcesPlaceholderConfigurer]: Factory method 'properties' threw exception; nested exception is java.lang.IllegalStateException: java.io.FileNotFoundException: ./conf/sql.yml

編輯下面是conf文件夾的位置在此處輸入圖像描述

我也有這個問題。 我在 Intellij 中有一個 FileNotFoundException,而它在 Eclipse 中運行良好。

我的解決方案是在“運行/調試配置”下指定“工作目錄”,因為我有一個多模塊項目(1 個項目中有多個項目/模塊)。 如果不這樣做,大主項目的根文件夾將是工作目錄。 這就是路徑不正確的原因(或者至少在我的情況下)。

例如:

  • MyUltimateParentApplication(項目根文件夾並在 Intellij 中打開)
    • MyChildApplication1(spring 啟動中的 fe 后端)
    • MyChildApplication2(Angular 前端)

在 Eclipse 中,“MyChildApplication1”的工作目錄是“MyChildApplication1”,而在 Intellij 中是“MyUltimateParentApplication”。 在 Intellij 中更改它為我修復了它。

雖然這可能不是每個人都可以解決的問題,但我仍然希望這對未來的其他人也有幫助。

暫無
暫無

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

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