【问题标题】:Qualify of Job to run with JobLauncherTestUtils not working在 JobLauncherTestUtils 不工作的情况下运行作业的资格
【发布时间】:2016-11-16 23:02:17
【问题描述】:

我有一个 launch-context.xml,它定义了 7 个不同的作业,所有这些作业都有相同的父级。它们有“jobA”、“jobB”等名称。

我试过了:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = { "/launch-context.xml", "/OurBatchKernelTestConfig.xml" })
 public class AllTest extends BaseRaptorBatchTest {

     @Autowired
     private JobLauncherTestUtils utils;

     @Autowired
     @Qualifier(value="jobA")
     private Job job;

     @Test
     public void testLaunch() {
         Properties p = new Properties(); // then I set these up.
         JobExecution je = utils.launchJob(paraCvter.getJobParameters(p));
     }
 }

这不起作用。

我得到一个例外:

 STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initialization - cancelling refresh attempt
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobLauncherTestUtilsForSnapshot': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.batch.test.JobLauncherTestUtils.setJob(org.springframework.batch.core.Job); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 2: coverageRuleBatch,generateMetricsSnapshotJob

我也试过了:

https://stackoverflow.com/a/29658577/869809

我试过了:

https://stackoverflow.com/a/36352437/869809

这些都不起作用。

我可以创建我的 launch-content.xml 的副本并删除其他作业。然后我在注释中提到它,一切都很好。但是我需要 7 个不同的 xml 文件。艾克。

【问题讨论】:

  • 你解决过这个问题吗?

标签: java spring spring-batch spring-java-config


【解决方案1】:

根据异常消息中的信息,您似乎需要明确 Spring 上下文中的哪个 bean 应该自动连接到成员变量 utils。像这样的:

 @Autowired
 @Qualifier(value="coverageRuleBatch")
 private JobLauncherTestUtils utils;

或者这个:

 @Autowired
 @Qualifier(value="generateMetricsSnapshotJob")
 private JobLauncherTestUtils utils;

应该解决歧义。

【讨论】:

  • 所以,首先我想使用一个自动连接的 JobLauncherTestUtils 实例并调用 setJob 来完成所需的工作。但是使用 @Qualifier(value="jobA") 自动连接作业 ivar 不起作用。
  • 其次,我尝试使用包含我的工作集的 JobLauncherTestUtils 子类。那也没用。
  • 实际上,您提出的解决方案与我在上面引用的stackoverflow.com/a/36352437/869809 中的解决方案相同。那没有用。
  • 如果自动装配不起作用,您可以尝试构造函数注入。虽然我在 autowire +qualifier 方面取得了很好的成功
【解决方案2】:

显然,您的测试配置不应实例化超过 1 个 Job,因为 JobLauncherTestUtils 只需要一个 bean。

为了解决JobLauncherTestUtils 的限制,您可以通过构造函数/设置器自己管理依赖项,如下所示:Spring Batch JUnit test for multiple jobs

或者,您可以将JobLauncherTestUtils 子类化并设置@Qualifier

@Autowired
@Qualifier("myChosenJobBeanName")
public void setJob(Job job) {
    this.job = job;
}

但您需要为每个要测试的作业覆盖它。最好以工厂方法结束。

您可以在没有JobLauncherTestUtils 的情况下启动作业。只是研究实施。很简单:

getJobLauncher().run(this.job, jobParameters);

也可以关注How can I qualify an autowired setter that I don't "own"

【讨论】:

    猜你喜欢
    • 2012-08-07
    • 2021-07-29
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2017-01-23
    • 1970-01-01
    相关资源
    最近更新 更多