我正在尝试将一些现有的基于xml的配置转换为JavaConfig,而且我遇到了一堵墙。
@Config
类:@Configuration
@Import(TestContext.class)
public class BatchUsageReportJobTestContext {
@Bean(name = "test.jobLauncher")
public JobLauncherTestUtils jobLauncher(@Qualifier("reporting.job.usageReportJob") Job job, // From TestContext.class
@Qualifier("test.simpleJobLauncher") JobLauncher jobLauncher) { // From TestContext.class
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(job);
jobLauncherTestUtils.setJobLauncher(jobLauncher);
return jobLauncherTestUtils;
}
}
JobLauncherTestUtils
是一段弹簧批代码:
org.springframework.batch.test.JobLauncherTestUtils
(摘录)public class JobLauncherTestUtils {
private Job job;
@Autowired
public Job setJob(Job job) {
this.job = job;
}
}
问题在于,在调用了我的@Bean
方法之后,使用正确的@Qualifer
注释bean调用了setter,spring尝试@Autowire
(不是{{1}在它可以找到的字段中,覆盖我设置的字段。问题是有多个@Qualified
类型的bean,所以我得到以下内容:
Job
我无法想象最好的事情就是在这里。使用XML配置,您可以拥有任意类型的bean,并选择正确的bean - 这似乎不可能,或者至少很容易实现。
我究竟做错了什么?理想情况下,我希望@Bean注释类只注册一个bean - 不要做任何魔术。设置... 42 more
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 3: reporting.job.reportProcessing,reporting.job.summaryReport,reporting.job.usageReportJob
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:618)
... 44 more
也无济于事。
答案 0 :(得分:0)
通常我们会在
declaration
或getters
之上使用注释。所以 试试这个:
public class JobLauncherTestUtils {
@Autowired
private Job job;
public Job setJob(Job job) {
this.job = job;
}
}