Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何鉴定我没有的自动连线设定器';";“拥有”;_Java_Spring_Spring Batch_Spring 3_Spring Java Config - Fatal编程技术网

Java 如何鉴定我没有的自动连线设定器';";“拥有”;

Java 如何鉴定我没有的自动连线设定器';";“拥有”;,java,spring,spring-batch,spring-3,spring-java-config,Java,Spring,Spring Batch,Spring 3,Spring Java Config,要点是SpringBatch(v2)测试框架具有带有@Autowired注释的JobLauncherTestUtils.setJob。我们的测试套件有多个作业类提供者。因为这个类不是我可以修改的,所以我不确定如何才能确定它自动连接到哪个作业,每个测试可能会有所不同 STDOUT [WARN ] [2015.04.15 11:14:42] support.GenericApplicationContext - Exception encountered during context initia

要点是SpringBatch(v2)测试框架具有带有
@Autowired
注释的
JobLauncherTestUtils.setJob
。我们的测试套件有多个
作业
类提供者。因为这个类不是我可以修改的,所以我不确定如何才能确定它自动连接到哪个作业,每个测试可能会有所不同

 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
我已尝试添加此已识别的JavaConfig,但错误表明它仍在自动调用
setJob

@Configuration
public class SpringTestConfiguration
{
@Bean
public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot( final Job generateMetricsSnapshotJob )
{
    JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
    jobLauncherTestUtils.setJob( generateMetricsSnapshotJob );
    return jobLauncherTestUtils;
}
}

注意:我不需要JavaConfig解决方案,但它会很好。此外,如果可能的话,我希望仍然自动连接JobRepository这样的字段,因为只有一个。

也许您可以使用Spring概要文件来实现这一点。为每个
作业
提供程序bean定义分配不同的配置文件(使用注释
@profile(“profileName”)
),然后使用注释
@ActiveProfiles(“profileName”)激活特定测试类上正确提供程序的配置文件

当我遇到同样的问题时,我的解决方案是限制组件扫描,以便在测试上下文中只创建一个作业bean

@Configuration
@ComponentScan(basePackages={
    "com.example.batch.jobs.metrics",  //package where generateMetricsSnapshotJob is the only job
    "com.example.batch.common",
    "..."
})
public class SpringTestConfiguration
{
    @Bean
    public JobLauncherTestUtils jobLauncherTestUtils()
    {
        //generateMetricsSnapshotJob and other requirements will be autowired
        return new JobLauncherTestUtils();
    }
}
您可能需要调整包结构才能使其正常工作

  • 您可以扩展AutoWiredNotationBeanPostProcessor并重写 注入法

  • 删除
    条目

  • 注册你的bean


  • 我想出的解决办法

    @Configuration
    public class SpringBatchTestConfiguration
    {
    @Bean
    public static JobLauncherTestUtils jobLauncherTestUtilsForSnapshot()
    {
        return new SnapshotJobLauncherTestUtils();
    }
    
    public static class SnapshotJobLauncherTestUtils extends JobLauncherTestUtils
    {
        @Override
        @Qualifier( "generateMetricsSnapshotJob" )
        public void setJob( final Job job )
        {
            super.setJob( job );
        }
    }
    }
    
    在最后的测试中

    @Autowired
    @Qualifier( "jobLauncherTestUtilsForSnapshot" )
    protected JobLauncherTestUtils jobLauncherTestUtils;
    

    我很有信心我可以用@Component注释我的TestUtils并正确命名它,然后做同样的事情。

    另一种解决方案是通过setter注入它。我更喜欢这种解决方案,因为它更清晰、更容易

    @Configuration
    public class SpringTestConfiguration
    {
        @Bean
        public JobLauncherTestUtils jobLauncherTestUtilsForSnapshot()
        {
            return new JobLauncherTestUtils() {
                @Override
                @Autowired
                public void setJob(@Qualifier("generateMetricsSnapshotJob") Job job) {
                    super.setJob(job);
                }
            };
        }
    }
    
    相关的,可能是重复的