2014-12-02 167 views
2

我是Spring批處理框架的總新手,我發現從http://www.javabeat.net/introduction-to-spring-batch/可以輕鬆理解的代碼用作學習工具。我有我的項目在Eclipse similiar設置爲從頁面代碼,它看起來像這樣:Spring批處理CommandLineJobRunner找不到.xml配置文件

Project Dir Structure

和代碼執行使用CommandLineJobRunner像這樣在fileWritingJob.xml的工作:

package net.javabeat.articles.spring.batch.examples.filewriter; 

import org.springframework.batch.core.launch.support.CommandLineJobRunner; 

public class Main { 

    public static void main(String[] args) throws Exception { 

     CommandLineJobRunner.main(new String[]{"fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 
    } 
} 

並按預期運行,沒有問題。但是,當我將fileWritingJob.xml移動到另一個目錄(仍在項目目錄下)時,它不會運行。我試過用相對路徑和完整路徑改變CommandLineJobRunner方法的文件名參數,但它仍然不能運行。例如,如果創建項目目錄下的目錄(同一級別的配置)評選工作,並把XML有那麼文件路徑傳遞給CommandLineJobRunner這樣的:

CommandLineJobRunner.main(new String[]{"/jobs/fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 

或本

CommandLineJobRunner.main(new String[]{"../jobs/fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 

它不起作用。

但是,當我試圖創建config目錄下建立了子目錄,並把fileWritingJob.xml那裏,這樣

CommandLineJobRunner.main(new String[]{"configsubdir/fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 
運行

。就好像CommandLineJobRunner只檢查config目錄。

我用盡想法,任何人都可以幫助我嗎?

更新:經過挖掘了一下,感謝Michael Minella關於ClassPathXmlApplicationContext的建議,我可以將xml放在任何我想要的位置。我也諮詢到這個頁面Spring cannot find bean xml configuration file when it does existhttp://www.mkyong.com/spring-batch/spring-batch-hello-world-example/

所以我現在要做的是通過使用ClassPathXmlApplicationContextand然後使用作業啓動運行它,在這裏宣佈一個新的上下文是如何:

public static void main(String[] args) { 

    String[] springConfig = 
     { 
      "file:/path/to/xml/file" 
     }; 

    ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); 

    JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); 
    Job job = (Job) context.getBean("JobName"); 

    try { 

     JobExecution execution = jobLauncher.run(job, new JobParameters()); 
     System.out.println("Exit Status : " + execution.getStatus()); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    System.out.println("Done"); 

    } 

非常感謝你的所有你的投入!

回答

1

當您將基於xml的作業定義的路徑傳遞到CommandLineJobRunner時,會發生一些細節。我們所做的只是將該字符串傳遞給構造函數ClassPathXmlApplicationContext。因此,預計作業定義的xml文件位於應用程序的類路徑中。我無法從項目屏幕截圖中知道您是如何構建項目的,因此我不確定config目錄是否在您的類路徑中。但是,如果它位於類路徑上並且位於它的根目錄下,我希望您能夠將路徑傳遞到fileWritingJob.xml,如"/config/fileWritingJob.xml"

調試此類問題時,此類的源代碼可能會有幫助。你可以在這裏找到CommandLineJobRunner的源代碼:https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java

+0

或許我不清楚。如果我把XML放在配置目錄和它的子目錄下,它就可以工作。當我移動文件並將文件路徑傳遞給CommandLineJobRunner時,它不起作用。我會嘗試檢查類路徑,也許它會給我一些線索。謝謝你的建議! – akiortagem 2014-12-03 01:27:31

0

您可以指定相對於config目錄的作業路徑。例如,如果fileWritingJob.xml配置/職位/目錄,然後在你可以執行如下任務:

CommandLineJobRunner.main(new String[]{"jobs/fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 

同樣,如果作業的配置文件是配置之外目錄中,您可以編寫:

CommandLineJobRunner.main(new String[]{"../fileWritingJob.xml", "LayeredMultiThreadJobTest"}); 

您可以指定查找作業的絕對路徑。

+0

對不起,我確實提到過,如果我把它放在配置權限的子目錄中,它會起作用嗎?如果我使用全路徑或相對路徑將其從配置目錄中刪除,但無法正常工作,但是感謝 – akiortagem 2014-12-03 01:32:40

相關問題