我试图通过从Program Arguments中检索数据库参数来配置数据源bean,但问题是我收到以下异常:
Caused by: java.lang.IllegalStateException: No context holder available for step scope
计划参数:
com.myexample.conf.Config taskletExampleJob DBurl=db:3600/db DBuser=test DBpassword=test
Java代码:
@Configuration
@EnableBatchProcessing
public class Config {
@Bean
@StepScope
public DataSource dataSource1(@Value("#{jobParameters['DBurl']}") String url,
@Value("#{jobParameters['DBuser']}") String user,
@Value("#{jobParameters['DBpassword']}") String password) throws Exception {
Properties dataSourceProperties = new Properties();
dataSourceProperties.put("url", url);
dataSourceProperties.put("username", user);
dataSourceProperties.put("password", password);
dataSourceProperties.put("defaultAutoCommit", true);
return BasicDataSourceFactory.createDataSource(dataSourceProperties);
}
@Bean
public org.springframework.batch.core.scope.StepScope stepScope() {
final org.springframework.batch.core.scope.StepScope stepScope = new org.springframework.batch.core.scope.StepScope();
stepScope.setAutoProxy(true);
stepScope.setOrder(0);
return stepScope;
}
}
答案 0 :(得分:0)
我知道了,解决方案是使用Spring Boot。在这种情况下,我们甚至不必使用jobParameters,而是直接使用@Value(“ DBurl”)