【问题标题】:No qualifying bean of type [leanbizapps.monster.services.BatchService] found for dependency没有为依赖找到类型 [leanbizapps.monster.services.BatchService] 的合格 bean
【发布时间】:2016-12-31 05:50:33
【问题描述】:

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

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:创建名为“leaveAllocationJobConfiguration”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private leanbizapps.monster.services.BatchService leanbizapps.dexter.jobs.LeaveAllocationJobConfiguration.batchService;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type of [leanbizapps.monster.services.BatchService] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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] 在 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] 在 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] 在 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: No qualifying bean of type of [leanbizapps.monster.services.BatchService] found for dependency: 预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:{@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 有资格作为此依赖项的自动装配候选者。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在 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个常用框架

我该如何解决这个问题?

【问题讨论】:

  • 您有什么理由回滚我的修订版?我只添加了语法高亮,确保代码块格式正确,并将您的错误格式化为代码块,因为这很难读。

标签: spring-boot spring-batch


【解决方案1】:

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

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

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

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

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

【讨论】:

    【解决方案2】:
    @ComponentScan(basePackages={"leanbizapps.monster", "leanbizapps.dexter"})
    public class BatchApplication {
    // source code
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-29
      • 2018-12-02
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      相关资源
      最近更新 更多