首页 > 解决方案 > 具有多个 @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 状态的调试。

这令人沮丧,据我所知和理解,它应该把它们捡起来。我错过了什么和/或没有正确理解什么?

标签: springspring-bootconfigurationspring-batch

解决方案


确保您的BatchConfigurer.java文件应该与MainApplication.java文件在同一个包中。

或者,

MainApplication.java文件中导入BatchConfigurer.java 。

检查此图像以供参考


推荐阅读