繁体   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