Spring Batch CommandLineJobRunner 找不到 .xml 配置文件 [英] Spring Batch CommandLineJobRunner can't find .xml configuration file

查看:33
本文介绍了Spring Batch CommandLineJobRunner 找不到 .xml 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spring Batch Framework 的初学者,我从 和 http://www.mkyong.com/spring-batch/spring-batch-hello-world-example/

所以我现在要做的是使用 ClassPathXmlApplicationContext 声明一个新的上下文,然后使用作业启动器运行它,方法如下:

public static void main(String[] args) {字符串[] springConfig ={文件:/路径/到/xml/文件"};ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");Job job = (Job) context.getBean("JobName");尝试 {JobExecution execution = jobLauncher.run(job, new JobParameters());System.out.println("退出状态:" + execution.getStatus());} 捕获(异常 e){e.printStackTrace();}System.out.println("完成");}

非常感谢您的所有意见!

解决方案

当您将基于 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

I'm a total beginner in Spring Batch Framework, and I found easily understandable codes from http://www.javabeat.net/introduction-to-spring-batch/ to use as a learning tools. I have my project set up in Eclipse similiar to the codes from the page, it looks like this :

and the code executes the jobs in fileWritingJob.xml using CommandLineJobRunner like so :

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

and it runs as expected with no problem. But when I move the fileWritingJob.xml to another dir (still under the project dir) it doesn't run. I've tried changed the filename arguments at the CommandLineJobRunner method using relative and full path but it still doesn't run. For example, if create a directory under the project directory (same level as config) named jobs and put the xml there then pass the filepath to CommandLineJobRunner like this:

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

or this

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

it doesn't work.

But when I tried creating a subdir under the config directory and put the fileWritingJob.xml there, like this

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

it runs. It's as if the CommandLineJobRunner only checks the config directory.

I'm running out of ideas, can anyone help me?

UPDATE : After digging around a bit, thanks to Michael Minella's suggestion about ClassPathXmlApplicationContext I am able to put the xml wherever I want. I also consulted to this page Spring cannot find bean xml configuration file when it does exist and http://www.mkyong.com/spring-batch/spring-batch-hello-world-example/

So what I do now is declaring a new context by using ClassPathXmlApplicationContextand then run it using job launcher, here is how :

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

  }

Thank you very much for all your inputs!

解决方案

A little detail around what is happening when you pass in the path to the xml based job definition to the CommandLineJobRunner. All we do is we pass that string to the constructor of ClassPathXmlApplicationContext. Because of that, it is expected that the xml file for the job definition be on the application's classpath. I can't tell from your project screen shot how you are building the project so I'm not sure if the config directory is on your classpath or not. However, if it is on the classpath and lives at the root of it, I'd expect you to be able to pass the path to the fileWritingJob.xml as "/config/fileWritingJob.xml".

The source for this class can be helpful when debugging this type of issue. You can find the source code for the CommandLineJobRunner here: https://github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/CommandLineJobRunner.java

这篇关于Spring Batch CommandLineJobRunner 找不到 .xml 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆