栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何为Spring Batch数据和业务数据Java配置单独的数据源?我应该做吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何为Spring Batch数据和业务数据Java配置单独的数据源?我应该做吗?

好的,这很奇怪,但是可以。将数据源移动到它自己的配置类中可以很好地工作,并且能够自动装配。

该示例是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();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391582.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号