簡體   English   中英

Spring Batch CommandLineJobRunner找不到.xml配置文件

[英]Spring Batch CommandLineJobRunner can't find .xml configuration file

我是Spring Batch Framework的初學者,我從http://www.javabeat.net/introduction-to-spring-batch/找到了易於理解的代碼,用作學習工具。 我在Eclipse中設置了我的項目,類似於頁面中的代碼,它看起來像這樣:

項目目錄結構

並且代碼使用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方法中的文件名參數,但它仍然無法運行。 例如,如果在項目目錄(與config相同的級別)下創建一個名為jobs的目錄並將xml放在那里,那么將文件路徑傳遞給CommandLineJobRunner,如下所示:

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

或這個

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

它不起作用。

但是當我嘗試在config目錄下創建一個subdir並將fileWritingJob.xml放在那里時,就像這樣

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

它運行。 就像CommandLineJobRunner只檢查config目錄一樣。

我的想法已經用完了,任何人都可以幫助我嗎?

更新:在挖掘了一下之后,感謝Michael Minella關於ClassPathXmlApplicationContext的建議,我能夠將xml放在我想要的任何地方。 我也咨詢了這個頁面, 當它存在時,Spring找不到bean xml配置文件http://www.mkyong.com/spring-batch/spring-batch-hello-world-example/

所以我現在所做的是通過使用ClassPathXmlApplicationContextand聲明一個新的上下文,然后使用job launcher運行它,方法如下:

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");

  }

非常感謝您的所有投入!

關於將基於xml的作業定義的路徑傳遞給CommandLineJobRunner時發生的事情的一些細節。 我們所做的就是將該字符串傳遞給ClassPathXmlApplicationContext的構造函數。 因此,期望作業定義的xml文件位於應用程序的類路徑上。 我無法從你的項目屏幕截圖中看出你是如何構建項目所以我不確定config目錄是否在你的類路徑上。 但是,如果它位於類路徑上並且位於它的根目錄下,我希望您能夠將路徑作為"/config/fileWritingJob.xml"傳遞給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

您可以指定相對於config目錄的作業路徑。 例如,如果fileWritingJob.xml位於config / jobs /目錄中,那么您可以按如下方式執行作業:

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

同樣,如果作業配置文件位於config目錄之外,您可以編寫:

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

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

暫無
暫無

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

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