2014-08-31 35 views
1

工作,我有一個配置Bean。資源注入春天啓動不gradle這個

@Component 
public class Config { 

    @Value("classpath:${app.file.name.srgbicc}") 
    public Resource icc; 

    @PostConstruct 
    void init(){ 
     try { 
     tempdir = Files.createTempDir(); 
     newIcc = copyIccFileToTemp();   
     }catch (IOException e){ 
     throw new RuntimeException(e); 
     } 
    } 

    private File copyIccFileToTemp() throws IOException { 
     File tempIcc = new File(tempdir, icc.getFilename()); 
     FileUtils.copyFile(icc.getFile(),tempIcc); 
     return tempIcc; 
    }  

}

icc.getFile()有一個FileNotFoundException異常

application.properties

app.file.name.srgbicc = sRGB.icc 

我看着在我的類路徑,發現了以下情況:

build/classes/main (no icc file) 
build/resources/main (icc file, application.properties) 

在應用程序啓動期間打印出我的類路徑時,我只發現了...myApp/build/classes/main。 沒有../build/resources/main../src/main/resources那裏的條目。

所以我想知道爲什麼是資源不是在類路徑? 根據http://docs.spring.io/spring-boot/docs/1.1.5.RELEASE/reference/htmlsingle/#build-tool-plugins-gradle-running-applications 這應該是。

當然,如果我把icc文件中build/classes/main其所有的預期工作,但它不應該在那裏。對?

我試圖用gradle rungradle bootRun在intellij中使用static main運行應用程序。好處是應用程序無法始終如一地啓動它。

我的項目佈局

myApp 
    src 
     main 
      java 
       pkg 
        Config.java 
      resources 
       sRGB.icc 
       application.properties 

對於參考: 的IntelliJ 13 春季啓動1.1.5 Fgradle 2.0

更新 下面是一個剝離下來的例子我的代碼 https://gist.github.com/Vad1mo/bb5d23ca251341236fbd

我在config.init中刪除了@PostConstruct,並用gradle運行應用程序,所有測試都成功當我從intellij失敗時,測試失敗了。這是奇怪的,我知道有不同的行爲

+0

究竟如何運行應用程序(或搖籃的IntelliJ),以及究竟你在哪裏把資源文件?看起來有些配置錯誤,因爲處理過的資源文件應該放到'build/resources/main'中,而不是'build/classes/resources/main'。 – 2014-09-01 04:20:45

+0

@PeterNiederwieser我在創建問題時出現了複製和粘貼錯誤。我的路徑是'build/resources/main' – Vadimo 2014-09-01 07:07:09

+0

您究竟如何運行應用程序(Gradle或IntelliJ),以及您在哪裏放置資源文件? – 2014-09-01 09:08:10

回答

0

我現在解決了這個問題。

什麼幫助我只是做了簡單的clean build和的IntelliJ改造項目。我正在使用日食和maven,所以我預計它會自動發生。