【问题标题】:Spring Batch CommandLineJobRunner can't find .xml configuration fileSpring Batch CommandLineJobRunner 找不到 .xml 配置文件
【发布时间】:2015-01-30 12:00:12
【问题描述】:

我是 Spring Batch 框架的初学者,我从 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 方法中的文件名参数,但它仍然无法运行。例如,如果在项目目录(与配置相同的级别)下创建一个名为 jobs 的目录并将 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/

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

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

  }

非常感谢您的所有投入!

【问题讨论】:

    标签: java xml spring-batch


    【解决方案1】:

    您可以指定相对于config 目录的作业路径。例如,如果 fileWritingJob.xml 位于 config/jobs/ 目录中,则可以按如下方式执行作业:

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

    同样,如果作业配置文件在config目录之外,你可以这样写:

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

    您可以指定定位作业的绝对路径。

    【讨论】:

    • 对不起,我确实提到如果我把它放在 config 的子目录中它会起作用,对吧?如果我使用完整路径或相对路径将其从配置目录中取出,它将不起作用,但无论如何谢谢
    【解决方案2】:

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

    【讨论】:

    • 也许我不清楚。如果我将 xml 放在配置目录及其子目录下,它就可以工作。当我移动文件然后将文件路径传递给 CommandLineJobRunner 时它不起作用。我会尝试检查类路径,也许它会给我一些线索。谢谢你的建议!
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 2018-08-21
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多