Reputation: 93
I'm trying to run the batch but I was not able to inject the batchservice into it.
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();
}
}
When I run this code I'm getting no qualifier bean type error. 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$1.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
How could I solve this issue?
Upvotes: 0
Views: 2186
Reputation: 174
@ComponentScan(basePackages={"leanbizapps.monster", "leanbizapps.dexter"})
public class BatchApplication {
// source code
}
Upvotes: 1
Reputation: 44745
You didn't provide your BatchService
, but it appears to be in the package leanbizzapps.monster.services
, while your main class resides in the leanbizzapps.dexter
package.
Spring boot will automatically scan all subpackages, however, it will not scan other packages (like leanbizzapps.monster.services
).
The solution is to restructure the application so that BatchApplication
is in the parent package of all beans (so you could move it to the leanbizzapps
package for example).
The alternative is to include the leanbizzapps.monster.services
package in any @ComponentScan
, for example on your main class:
@ComponentScan(basePackages = { "leanbizzapps.monster.services", "leanbizzapps.dexter" })
public class BatchApplication {
// ...
}
Upvotes: 2