使用JobLauncherTestUtils无法正常运行的作业限定

时间:2016-11-16 23:02:17

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

我有一个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文件。 ICK。

1 个答案:

答案 0 :(得分:1)

根据异常消息中的信息,您似乎需要消除Spring上下文中应该自动装配到成员变量utils的bean的歧义。像这样:

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

或者这个:

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

应该解决歧义。