spring - 具有多个 @Configuration 类的 Spring Boot/Batch 不实例化 bean(或加载属性)
问题描述
因此,首先,如果这似乎与其他问题相似,我深表歉意——我已经看过,尝试了建议的解决方案,但没有一个解决问题。首先是净化后的代码...
package com.mine.batchMain;
@SpringBootApplication
@Configuration
public class MyApplication implements CommandLineRunner {
//...
public static void main(String[] args) {
//....
}
}
package com.mine.batchMain;
//...
import com.min.batchMain.firstSteps.FirstStepConfigHolder;
//...
@Configuration
@EnableBatchProcessing
public class BatchConfigurer {
//....
@Autowired
private FirstStepConfigHolder firstStep;
//...
@Bean
public Step defineFirstStep() {
return stepBuilder.get("First Step")
.chunk<MyPOJO, MyPOJO>(batchSize)
.readerfirstStep.fetcher())
.writer(firstStep.extracter())
.listener(firstStep.listen())
.build();
}
//....
}
package com.mine.batchMain.firstSteps;
//...
import com.mine.batchMain.common.MyRepo;
import com.mine.batchMain.firstSteps.DocFetcher;
//...
@Configuration
@EnableJPARepositories
public class FirstStepConfigHolder {
//....
@Value("${myapp.dbUrl}")
String dbUrl;
@Value("${myapp.dbSchema}")
String dbSchema;
@Value("${myapp.dbUser}")
String dbUser;
@Value("${myapp.encDbPass}")
String encryptDbPass;
@Value("${myapp.dbDriver}")
String dbDriver;
@Value("${myapp.maxDocSize}")
String maxDocSize;
@Value("${myapp.maxNumDocs}")
String maxNumDocs;
@Bean
public DocFetcher fetcher() {
log.trace("Creating DocFetcher.")
return new DocFetcher(myDb());
}
@Bean
public MyRepo myDb() {
log.trace("Creating repo.");
MyRepo retDb = new MyRepo(myDataSource());
retDb.setMaxNumDocs(Integer.valueOf(maxNumDocs));
retDb.setMaxDocSize(Integer.valueOf(maxDocSize));
log.debug("Confirming db class state:"+retDb.toString());
return retDb;
}
private DataSource myDataSource() {
DriverManagerDataSource retDs = new DriverManagerDataSource(dbUrl, dbUser, decrypt(encryptDbPass));
retDs.setDriverClassName(dbDriver);
return retDs;
}
}
问题/症状如下: 1) MyRepo 没有选择 maxDocSize 和 maxNumDocs。(Logging 显示默认值) 2) Logging 显示对“Creating DocFetcher”的跟踪调用,但不显示对“Creating repo.”的跟踪调用,也不显示 Repo 状态的调试。
这令人沮丧,据我所知和理解,它应该把它们捡起来。我错过了什么和/或没有正确理解什么?
解决方案
确保您的BatchConfigurer.java文件应该与MainApplication.java文件在同一个包中。
或者,
在MainApplication.java文件中导入BatchConfigurer.java 。
检查此图像以供参考
推荐阅读
- java - containsKey() 总是返回 true
- c# - 通过 system.io 扫描我的 C:\ 驱动器时出现问题
- build - 错误:CSS 缩小错误:JisonLexerError:第 1 行的词法错误:无法识别的文本
- javascript - VSCode 在调试时不运行修改过的文件
- python - 从Python中的字典列表中提取多个键/值对的最有效方法?
- python - 如何在 Python 中沿圆形路径粘贴图像?
- javascript - Javascript:一个对象怎么可能是假的?
- excel - 将 Excel SUMPRODUCT 与多条件 COUNTIF 相结合
- angular - Angular 错误页面组件使用带有 id 的路由而没有重定向
- animation - 使用按钮交互重放 ThreeJs 动画