0

さまざまなパッケージに次のクラスがあります。

@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) を使用して有効にする機能を指定できます。)

4

1 に答える 1

0

M. Deinum のおかげで、私はついに答えを得ました。

を持っていることで@Enable*、私は無効にして@EnableAutoConfigurationいました。唯一の例外は@EnableBatchProcessing、明示的に必須の です。(そのことは理解していますが、なぜ で処理できないのかわかりません@EnableAutoConfiguration。)

答えは、上記の Application クラスと次のクラスの組み合わせでした。

@Configuration
@EnableBatchProcessing
public class BatchConfig
{
}

これが配置されると、他のすべてが機能しました。

これが私のテストコードにどのように影響するかは、別の日の課題のままです...

于 2016-01-27T08:59:24.780 に答える