简体   繁体   English

Spring Batch CommandLineJobRunner找不到.xml配置文件

[英]Spring Batch CommandLineJobRunner can't find .xml configuration file

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. 我是Spring Batch Framework的初学者,我从http://www.javabeat.net/introduction-to-spring-batch/找到了易于理解的代码,用作学习工具。 I have my project set up in Eclipse similiar to the codes from the page, it looks like this : 我在Eclipse中设置了我的项目,类似于页面中的代码,它看起来像这样:

项目目录结构

and the code executes the jobs in fileWritingJob.xml using CommandLineJobRunner like so : 并且代码使用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"});
    }
}

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. 但是当我将fileWritingJob.xml移动到另一个目录(仍在项目目录下)时,它不会运行。 I've tried changed the filename arguments at the CommandLineJobRunner method using relative and full path but it still doesn't run. 我已尝试使用相对路径和完整路径更改CommandLineJobRunner方法中的文件名参数,但它仍然无法运行。 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: 例如,如果在项目目录(与config相同的级别)下创建一个名为jobs的目录并将xml放在那里,那么将文件路径传递给CommandLineJobRunner,如下所示:

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 但是当我尝试在config目录下创建一个subdir并将fileWritingJob.xml放在那里时,就像这样

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

it runs. 它运行。 It's as if the CommandLineJobRunner only checks the config directory. 就像CommandLineJobRunner只检查config目录一样。

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. 更新:在挖掘了一下之后,感谢Michael Minella关于ClassPathXmlApplicationContext的建议,我能够将xml放在我想要的任何地方。 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/ 我也咨询了这个页面, 当它存在时,Spring找不到bean xml配置文件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 : 所以我现在所做的是通过使用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");

  }

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 . 关于将基于xml的作业定义的路径传递给CommandLineJobRunner时发生的事情的一些细节。 All we do is we pass that string to the constructor of ClassPathXmlApplicationContext . 我们所做的就是将该字符串传递给ClassPathXmlApplicationContext的构造函数。 Because of that, it is expected that the xml file for the job definition be on the application's classpath. 因此,期望作业定义的xml文件位于应用程序的类路径上。 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. 我无法从你的项目屏幕截图中看出你是如何构建项目所以我不确定config目录是否在你的类路径上。 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" . 但是,如果它位于类路径上并且位于它的根目录下,我希望您能够将路径作为"/config/fileWritingJob.xml"传递给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 你可以在这里找到CommandLineJobRunner的源代码: https//github.com/spring-projects/spring-batch/blob/master/spring-batch-core/src/main/java/org/springframework/batch/core /launch/support/CommandLineJobRunner.java

You can specify the job path relative to your config directory. 您可以指定相对于config目录的作业路径。 For example, if fileWritingJob.xml is in config/jobs/ directory, then in you can execute the job as follows: 例如,如果fileWritingJob.xml位于config / jobs /目录中,那么您可以按如下方式执行作业:

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

Likewise, if the job configuration file is outside of the config directory, you can write: 同样,如果作业配置文件位于config目录之外,您可以编写:

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

You can specify an absolute path for locating the job. 您可以指定用于查找作业的绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用具有 Java 配置的 CommandLineJobRunner 启动 Spring Batch Job - how to launch Spring Batch Job using CommandLineJobRunner having Java configuration Spring无法加载XML配置文件 - Spring can't load xml configuration file 使用spring查找和使用XML配置文件 - Find and use XML Configuration file with spring Spring Batch:如何在XML配置中指定步骤的类? - Spring Batch: How can I specify a class of a step in XML configuration? 由企业调度程序运行时,Spring Batch CommandLineJobRunner挂起 - Spring Batch CommandLineJobRunner hangs when run by enterprise scheduler Spring在路径中找不到配置 - Spring can't find configuration in path 为什么我不能将用@Service注释的bean注入到spring-security.xml配置文件中声明的bean中? - Why I can't inject this bean annoted with @Service into a bean declared into the spring-security.xml configuration file? Spring Security XML配置找不到我的dataSource bean定义 - Spring Security XML configuration can not find my dataSource beans definition Spring Batch Java配置-写入没有本地文件的远程sftp xml文件 - Spring Batch Java configuration - Writing to remote sftp xml file without local file 如何使用 CommandLineJobRunner 调用嵌入在 Spring Boot 应用程序中的 Spring Batch 作业 - How to invoke Spring Batch Jobs embedded in a Spring Boot Application using CommandLineJobRunner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM