我在不同的包中有以下类:
@EnableWebMvc
public class ActuatorConfig
{
// empty
// only needed to get REST so I can hit Actuator endpoints
}
@Component // REMOVE THIS LINE?
@EnableBatchProcessing
public class BatchConfig
{
// empty
}
@Component
@Configuration
public class ScheduleJob
{
@Autowired
private JobBuilderFactory jobBuilder;
@Bean
protected JobExecutionListener scheduleJobListener()
{
return new ScheduleJobListener();
}
}
(编辑:已添加应用程序类)
@SpringBootApplication
public class AfxApplication
{
public static void main(String[] args)
{
SpringApplication.run(AfxApplication.class, args);
}
}
第一节开启了Actuator功能。我不需要任何其他注释。
但是,如果我如上所述从BatchConfig中删除@Component,则Boot将无法启动,因为它无法找到它所需的JobBuilderFactory依赖项,以便填充@Autowired ScheduleJob.jobBuilder。
例外:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.configuration.annotation.JobBuilderFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
... 25 common frames omitted
何时需要@Component以及何时不需要?
(我拿出了@Enable *注释,因为我正在尝试运行不需要启用所有功能的单元测试,这样我就可以使用@ComponentScan(basePackageClasses)指定我想要启用的功能。 )
答案 0 :(得分:0)
感谢M. Deinum,我终于得到了答案。
通过@Enable*
,我禁用了@EnableAutoConfiguration
。唯一的例外是@EnableBatchProcessing
,这是明确要求的。 (我知道是这样,但我不确定为什么@EnableAutoConfiguration
无法处理它。)
答案是上面列出的Application类和这个类的组合:
@Configuration
@EnableBatchProcessing
public class BatchConfig
{
}
一旦到位,其他一切都有效。
这将如何影响我的测试代码将是另一天的任务...