我是Spring批處理框架的總新手,我發現從http://www.javabeat.net/introduction-to-spring-batch/可以輕鬆理解的代碼用作學習工具。我有我的項目在Eclipse similiar設置爲從頁面代碼,它看起來像這樣:Spring批處理CommandLineJobRunner找不到.xml配置文件
和代碼執行使用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 exist和http://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");
}
非常感謝你的所有你的投入!
或許我不清楚。如果我把XML放在配置目錄和它的子目錄下,它就可以工作。當我移動文件並將文件路徑傳遞給CommandLineJobRunner時,它不起作用。我會嘗試檢查類路徑,也許它會給我一些線索。謝謝你的建議! – akiortagem 2014-12-03 01:27:31