好的,这很奇怪,但是可以。将数据源移动到它自己的配置类中可以很好地工作,并且能够自动装配。
该示例是Spring Batch Service Example的多数据源版本:
DataSourceConfiguration:
public class DataSourceConfiguration { @Value("classpath:schema-mysql.sql") private Resource schemascript; @Bean @Primary public DataSource hsqldbDataSource() throws SQLException { final SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriver(new org.hsqldb.jdbcDriver()); dataSource.setUrl("jdbc:hsqldb:mem:mydb"); dataSource.setUsername("sa"); dataSource.setPassword(""); return dataSource; } @Bean public JdbcTemplate jdbcTemplate(final DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean public DataSource mysqlDataSource() throws SQLException { final SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriver(new com.mysql.jdbc.Driver()); dataSource.setUrl("jdbc:mysql://localhost/spring_batch_example"); dataSource.setUsername("test"); dataSource.setPassword("test"); DatabasePopulatorUtils.execute(databasePopulator(), dataSource); return dataSource; } @Bean public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") final DataSource dataSource) { return new JdbcTemplate(dataSource); } private DatabasePopulator databasePopulator() { final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addscript(schemascript); return populator; }}
BatchConfiguration:
@Configuration@EnableBatchProcessing@import({ DataSourceConfiguration.class, MBeanExporterConfig.class })public class BatchConfiguration { @Autowired private JobBuilderFactory jobs; @Autowired private StepBuilderFactory steps; @Bean public ItemReader<Person> reader() { final FlatFileItemReader<Person> reader = new FlatFileItemReader<Person>(); reader.setResource(new ClassPathResource("sample-data.csv")); reader.setLineMapper(new DefaultLineMapper<Person>() { { setLineTokenizer(new DelimitedLineTokenizer() { { setNames(new String[] { "firstName", "lastName" }); } }); setFieldSetMapper(new BeanWrapperFieldSetMapper<Person>() { { setTargetType(Person.class); } }); } }); return reader; } @Bean public ItemProcessor<Person, Person> processor() { return new PersonItemProcessor(); } @Bean public ItemWriter<Person> writer(@Qualifier("mysqlDataSource") final DataSource dataSource) { final JdbcBatchItemWriter<Person> writer = new JdbcBatchItemWriter<Person>(); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Person>()); writer.setSql("INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)"); writer.setDataSource(dataSource); return writer; } @Bean public Job importUserJob(final Step s1) { return jobs.get("importUserJob").incrementer(new RunIdIncrementer()).flow(s1).end().build(); } @Bean public Step step1(final ItemReader<Person> reader, final ItemWriter<Person> writer, final ItemProcessor<Person, Person> processor) { return steps.get("step1") .<Person, Person> chunk(1) .reader(reader) .processor(processor) .writer(writer) .build(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)