さまざまなパッケージに次のクラスがあります。
@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);
}
}
最初のクラスは、アクチュエータ機能をオンにします。他の注釈は必要ありません。
ただし、上記のように BatchConfig から @Component を削除すると、@Autowired ScheduleJob.jobBuilder を設定するために必要な JobBuilderFactory への依存関係が見つからないため、ブートが開始されません。
例外:
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) を使用して有効にする機能を指定できます。)