找不到类型为 [leanbizapps.monster.services.BatchService] 的合格 bean 以依赖

No qualifying bean of type [leanbizapps.monster.services.BatchService] found for dependency

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

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: Error creating bean with name 'leaveAllocationJobConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private leanbizapps.monster.services.BatchService leanbizapps.dexter.jobs.LeaveAllocationJobConfiguration.batchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 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. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at 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.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at 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] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at 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] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:764) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:357) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) ~[spring-boot-1.3.1.RELEASE.jar:1.3.1.RELEASE] at leanbizapps.dexter.BatchApplication.main(BatchApplication.java:21) [classes/:na] Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private leanbizapps.monster.services.BatchService leanbizapps.dexter.jobs.LeaveAllocationJobConfiguration.batchService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 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. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] ... 15 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 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. 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] ... 17 common frames omitted

我该如何解决这个问题?

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

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

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

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

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