没有为依赖

时间:2016-08-24 07:52:58

标签: spring-boot spring-batch

我试图运行批处理,但我无法将批处理服务注入其中。

BatchApplication.java

package leanbizapps.dexter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import leanbizapps.monster.config.SwaggerConfig;

@ComponentScan
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,SwaggerConfig.class,
        WebMvcAutoConfiguration.class,RepositoryRestMvcAutoConfiguration.class })
 public class BatchApplication {

     public static void main(String[] args) throws Exception {
     SpringApplication app = new SpringApplication(BatchApplication.class);
     ConfigurableApplicationContext ctx = app.run(args);
}
}

LeaveAllocationJobConfiguration.java

package leanbizapps.dexter.jobs;
@Configuration
@EnableBatchProcessing
public class LeaveAllocationJobConfiguration {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private EntityManagerFactory entityManagerFactory;

@Autowired
private BatchService batchService;

@Bean
public ItemReader<LeaveSetting> reader() {
    JpaPagingItemReader<LeaveSetting> leaveSettingReader = new JpaPagingItemReader<LeaveSetting>();
    leaveSettingReader.setEntityManagerFactory(entityManagerFactory);
    leaveSettingReader.setQueryString("from LeaveSetting");
    return leaveSettingReader;
}

@Bean
public Job addLeaveAllocationJob() {
    return jobs.get("addLeaveAllocationJob").listener(protocolListener()).start(step()).build();
}

@Bean
public Step step() {

    return stepBuilderFactory.get("step").<LeaveSetting, Boolean> chunk(1).reader(reader()).processor(processor())
            .writer(writer()).build();
}


@Bean
public ItemWriter<? super Boolean> writer() {
    return new ItemWriter<Boolean>() {

        @Override
        public void write(List<? extends Boolean> items) throws Exception {
            System.out.println("Processing " + items);

        }
    };
}

@Bean
public ItemProcessor<LeaveSetting, Boolean> processor() {
    return new ItemProcessor<LeaveSetting, Boolean>() {
        @Override
        public Boolean process(LeaveSetting leavesetting) throws Exception {
            int count =0;
            while(count>0){
                LocalDateTime localDateTime = LocalDateTime.now();
                batchService.leaveBatch(localDateTime);
            }
            return true;
        }

    };
}

@Bean
public ProtocolListener protocolListener() {
    return new ProtocolListener();
}

}

当我运行此代码时,我没有得到限定符bean类型错误。 No qualifying bean of type [leanbizapps.monster.services.BatchService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

  

org.springframework.beans.factory.BeanCreationException:创建名称为&#39; leaveAllocationJobConfiguration&#39;的注册自动连接依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private leanbizapps.monster.services.BatchService leanbizapps.dexter.jobs.LeaveAllocationJobConfiguration.batchService;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[leanbizapps.monster.services.BatchService]类型的限定bean:预期至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)~ [spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)〜[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)〜[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE]       at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)~ [spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764)〜[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]       在org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357)〜[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:305)〜[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE]       在leanbizapps.dexter.BatchApplication.main(BatchApplication.java:21)[classes /:na]   引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private leanbizapps.monster.services.BatchService leanbizapps.dexter.jobs.LeaveAllocationJobConfiguration.batchService;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到[leanbizapps.monster.services.BatchService]类型的限定bean:预期至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       ...省略了15个常见帧   引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[leanbizapps.monster.services.BatchService]的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{@ 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]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]       ...省略了17个常见帧

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您未提供BatchService,但它似乎位于包leanbizzapps.monster.services中,而您的主要类位于leanbizzapps.dexter包中。

Spring启动会自动扫描所有 sub 包,但是,它不会扫描其他包(例如leanbizzapps.monster.services)。

解决方案是重构应用程序,以便BatchApplication位于所有bean的父包中(因此您可以将其移动到leanbizzapps包中)。

另一种方法是将leanbizzapps.monster.services包添加到任何@ComponentScan中,例如在您的主类上:

@ComponentScan(basePackages = { "leanbizzapps.monster.services", "leanbizzapps.dexter" })
public class BatchApplication {
    // ...
}