Hibernate在Spring Batch Job中使用完全JavaConfig aproach

时间:2016-01-11 19:54:04

标签: spring hibernate spring-batch batch-processing

我使用JavaConfig完全配置了批处理作业。有困难,主要是因为我在配置上只使用java的材料很少。在弹簧批处理手册中,没有关于如何使用仅使用java的弹簧批完全配置作业的信息。我不知道是不是因为这个配置模型比基于xml的aprouch更新...

现在我在使用此配置类设置hibernate时遇到问题。我有一个xml设置,但我不知道如何引用它或如何为完全“JavaConfig”版本更改这些设置。

我的工作配置类是:

@Configuration
@EnableBatchProcessing
public class CrawlerBatchConfiguration {


    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Job job(JobBuilderFactory jobs) {
        return jobs.get("myJob").start(flow()).end().build();
    }

    protected Step step0() {
        return steps.get("setupCrawlerStep").tasklet(tasklet()).build();
    }

    private Flow flow() {
        FlowBuilder<Flow> builder = new FlowBuilder<>("flow1");
        builder.start(step0())
                .next(step1())
                .end();
        return builder.build();
    }


    @Bean
    protected Step step1() {
        return steps.get("processProductStep")
                .<TransferObject, Product>chunk(10)
                .reader(reader())
                .processor(processor())
                .writer(writer())
                .build();
    }

    @Bean
    protected Tasklet tasklet() {
        return new StartCrawlerTasklet();
    }


    @Bean
    protected ProductList getProductList() {
        return new ProductList();
    }

    @Bean
    public CrawlerListener listener() {
        CrawlerListener listener = new RibeiraoListener();
        return listener;
    }


    @Bean
    public ItemProcessor<TransferObject, Product> processor() {
        return new ProductProcessor();
    }

    @Bean
    public ItemReader<TransferObject> reader() {
        return new ProductItemReader();
    }

    @Bean
    public ItemWriter<Product> writer() {
        return new HIbernateProductsItemWriter();
    }


    @Bean
    public Crawler crawler() {
        return new RibeiraoCrawler(new UserAgentFactory());
    }

    @Bean
    public ProductBo productBo() {
        return new ProductBoImpl();
    }

    @Bean
    public ProductDao productDao() {
        return new ProductDaoImpl();
    }
}

和hibernate配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

    <!-- Hibernate session factory -->
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hbm2ddl.auto">update</prop>
                <prop key="current_session_context_class">thread</prop>

            </props>
        </property>

        <property name="annotatedClasses">
            <list>
                <value>br.com.alexpfx.supermarket.domain.Product</value>
                <value>br.com.alexpfx.supermarket.domain.Manufacturer</value>
            </list>
        </property>

    </bean>
</beans>

0 个答案:

没有答案