我有一个正在编写的spring boot / batch应用程序。我的Tasklet中的JdbcTemplate没有自动接线。在execute方法内部,jdbcTemplate为null。有人知道为什么吗?我缺少一些简单的东西。我正在使用月食氧气在项目上运行bootRun任务。
这是主要应用程序。
package x.y;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
配置类。
package x.y.z;
@Configuration
@PropertySource("classpath:batch-oracle.properties")
public class DataSourceConfiguration {
@Autowired
private Environment environment;
@Bean(name = "dataSource", destroyMethod = "close")
public DataSource dataSource() {
// datasource definition
return dataSource;
}
@Bean()
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
批处理配置类。
package x.y.z;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Step step1() {
return stepBuilderFactory.get("step1").tasklet(new
ReadWatermark()).build();
}
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
}
tasklet类
package x.y.a
@Component
public class ReadWatermark implements Tasklet {
@Autowired
public JdbcTemplate jdbcTemplate;
@Autowired
public DataSource dataSource;
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
// tasklet definition
return RepeatStatus.FINISHED;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
我的build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'blah'
version = '1.0.0'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("commons-dbcp:commons-dbcp:1.4")
compile("org.springframework.boot:spring-boot-starter-batch")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-web")
runtime("com.oracle:ojdbc6:11.2.0.4")
}