Java Spring批处理命令行JobRunner可以';找不到.xml配置文件

Java Spring批处理命令行JobRunner可以';找不到.xml配置文件,java,xml,spring-batch,Java,Xml,Spring Batch,我完全是SpringBatch框架的初学者,我从中找到了易于理解的代码,可以用作学习工具。我在Eclipse中设置的项目与页面中的代码类似,如下所示: 代码使用CommandLineJobRunner执行fileWritingJob.xml中的作业,如下所示: package net.javabeat.articles.spring.batch.examples.filewriter; import org.springframework.batch.core.launch.support.

我完全是SpringBatch框架的初学者,我从中找到了易于理解的代码,可以用作学习工具。我在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的项目目录(与config级别相同)下创建一个目录,并将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只检查配置目录一样

我没什么主意了,有人能帮我吗? 更新:在仔细研究了一下之后,感谢Michael Minella关于ClassPathXmlApplicationContext的建议,我能够将xml放在任何我想要的地方。我也参考了本页和

因此,我现在要做的是使用ClassPathXmlApplicationContext声明一个新上下文,然后使用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");

  }

非常感谢您的所有投入

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

CommandLineJobRunner.main(new String[]{"jobs/fileWritingJob.xml", "LayeredMultiThreadJobTest"});
同样,如果作业配置文件位于config目录之外,则可以写入:

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

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

将基于xml的作业定义的路径传递给
CommandLineJobRunner
时发生的情况的一点详细信息。我们所要做的就是将该字符串传递给
ClassPathXmlApplicationContext
的构造函数。因此,作业定义的xml文件应该位于应用程序的类路径上。我无法从您的项目屏幕截图判断您是如何构建项目的,因此我不确定配置目录是否在您的类路径上。但是,如果它位于类路径上并且位于它的根目录下,我希望您能够将路径作为
“/config/fileWritingJob.xml”
传递到fileWritingJob.xml


调试此类问题时,此类的源代码可能会有所帮助。您可以在这里找到
CommandLineJobRunner
的源代码:

可能我不清楚。如果我将xml放在config dir及其子目录下,它就会工作。当我移动文件,然后将文件路径传递给CommandLineJobRunner时,它不起作用。我将尝试检查类路径,也许它会给我一些线索。谢谢你的建议!对不起,我提到了如果我把它放在config的子目录下,它是可以工作的,对吗?如果我使用完整路径或相对路径将其从config dir中取出,它将不起作用,但无论如何还是要感谢您