我想在我的Jobs
应用程序中定义多个spring-batch
,并尝试将它们模块化如下:
@Configuration
@EnableBatchProcessing(modular = true)
public class ModularConfig {
@Bean
public ApplicationContextFactory getJob1() {
Sysout("job1");
return new GenericApplicationContextFactory(Job1.class);
}
@Bean
public ApplicationContextFactory getJob2() {
Sysout("job2");
return new GenericApplicationContextFactory(Job2.class);
}
}
从sysout我可以看到两个作业bean都已创建。
但是:当其中一个作业启动时,找不到该作业的@Beans
。
public class Job1 {
@Bean
public Job job(Step step) {
return jobFactory.get("job1")
.start(step)
.build();
}
@Bean
public Step step(ItemReader<String> reader, ItemProcessor<String, String> processor, ItemWriter<String> writer) {
return stepFactory.get("importStep")
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
@JobScope
public IteamReader<String> reader(@Value("#{jobParameters['filename']}") String filename) {
//returns a FlatFileItemReader<String>
}
//omitted also `@Bean` for processor + writer
}
结果:在作业配置中找不到reader
!为什么呢?
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'reader' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:596) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.GenericTypeAwareAutowireCandidateResolver.checkGenericTypeMatch(GenericTypeAwareAutowireCandidateResolver.java:95) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.GenericTypeAwareAutowireCandidateResolver.isAutowireCandidate(GenericTypeAwareAutowireCandidateResolver.java:64) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver.isAutowireCandidate(QualifierAnnotationAutowireCandidateResolver.java:142) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:687) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:646) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:630) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1191) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.7.RELEASE.jar:4.2.7.RELEASE]
... 34 more
那么为什么读者找不到?我的意思是:除了命名bean方法本身reader()
以及用变量名reader
注入它之外我还能做些什么?
旁注:当我删除modularty并在@Configuration
上使用class Job1
时,作业运行正常。所以我很确定作业和读者的配置应该是正确的。但当然我不能在下面运行类似的工作类,所以我想启用模块化。
编辑:当我在作业上启用@Configuration
注释时,我遇到了重复的作业名称异常。这就是我在上面的代码中没有它的原因。
Caused by: org.springframework.batch.core.configuration.DuplicateJobException: A job configuration with this name [job1] was already registered
at org.springframework.batch.core.configuration.support.MapJobRegistry.register(MapJobRegistry.java:51) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.support.DefaultJobLoader.doRegister(DefaultJobLoader.java:258) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.support.DefaultJobLoader.doLoad(DefaultJobLoader.java:193) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.support.DefaultJobLoader.load(DefaultJobLoader.java:151) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
at org.springframework.batch.core.configuration.support.AutomaticJobRegistrar.start(AutomaticJobRegistrar.java:173) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
... 16 more
答案 0 :(得分:1)
PackageManager p = getPackageManager();
ComponentName componentName = new ComponentName("myPackageName", "MyLauncherClass");
p.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
上没有thread.sleep()
bean将无法注册。请查看此测试用例Test Configuration