如何将XML更改为配置类?

时间:2012-10-09 11:10:12

标签: spring configuration annotations spring-batch cglib

对不起,我很讨厌英语。

请帮帮我。我有问题。
我希望将@X形式的XML更改为Java类,而@Bean则使用它 我该怎么办?

以下是我想改变的 我不知道'module-context.xml'特别是...
只是我想知道。

发射-context.xml中

<import resource="classpath:/META-INF/spring/module-context.xml" />

<bean id="jobOperator"
    class="org.springframework.batch.core.launch.support.SimpleJobOperator"
    p:jobLauncher-ref="jobLauncher" p:jobExplorer-ref="jobExplorer"
    p:jobRepository-ref="jobRepository" p:jobRegistry-ref="jobRegistry" />
<bean id="jobExplorer"
    class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
    p:dataSource-ref="SpringDataSource" />
<bean id="jobRegistry"
    class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean
    class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry" />
</bean>
<bean id="jobLauncher"
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
    class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"
    p:dataSource-ref="SpringDataSource" p:transactionManager-ref="springTransactionManager" />

<bean id="placeholderProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:batch.properties" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="1" />
</bean>

<bean id="SpringDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${batch.jdbc.driver}" />
    <property name="url" value="${batch.jdbc.url}" />
    <property name="username" value="${spring.batch.jdbc.user}" />
    <property name="password" value="${spring.batch.jdbc.password}" />
</bean>
<bean id="springTransactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
    lazy-init="true">
    <property name="dataSource" ref="SpringDataSource" />
</bean>

模块-context.xml中

<job id="job1" xmlns="http://www.springframework.org/schema/batch"
    incrementer="jobParametersIncrementer">
    <step id="simple-step" parent="simpleStep">
        <tasklet>
            <chunk reader="exampleReader" writer="exampleWriter" />
        </tasklet>
    </step>
</job>
<bean id="exampleReader"
    class="jp.co.app.cm.nochunk.ExampleItemReader" />
<bean id="exampleWriter"
    class="jp.co.app.cm.nochunk.ExampleItemWriter" />
<bean id="simpleStep"
    class="org.springframework.batch.core.step.item.SimpleStepFactoryBean"
    abstract="true">
    <property name="transactionManager" ref="springTransactionManager" />
    <property name="jobRepository" ref="jobRepository" />
    <property name="startLimit" value="100" />
</bean>
请告诉我某人?

2 个答案:

答案 0 :(得分:3)

前四个bean被翻译,这应该足以让你开始并掌握这个想法:

@Configuration
public class Spring {

    @Bean
    public MapJobRegistry mapJobRegistry() {
        return new MapJobRegistry();
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor() {
        JobRegistryBeanPostProcessor pp = new JobRegistryBeanPostProcessor();
        pp.setJobRegistry(mapJobRegistry());
        return pp;
    }

    @Bean
    public SimpleJobLauncher simpleJobLauncher() {
        SimpleJobLauncher jl = new SimpleJobLauncher();
        jl.setJobRepository(jobRepository().getObject());
        return jl;
    }

    @Bean
    public JobRepositoryFactoryBean jobRepositoryFactoryBean() {
        JobRepositoryFactoryBean fb = new JobRepositoryFactoryBean();
        fb.setDataSource(springDataSource());
        fb.setTransactionManager(springTransactionManager());
        return fb;
    }

}

答案 1 :(得分:1)

我知道这是一个旧帖子,但万一有人会通过谷歌发布此帖子 Java配置已添加到spring批处理启动版本2.2。

  

直到2.2.0,配置作业的唯一选择是通过XML(或者   通过批量DSL或手工)。但是,在2.2.0中,基于Java   已添加配置作为定义Spring Batch Jobs的方法。至   支持这个新的配置选项,一个注释和构建器   已添加课程。

http://docs.spring.io/spring-batch/2.2.x/reference/html/whatsNew.html#whatsNewJavaConfiguration http://docs.spring.io/spring-batch/2.2.x/reference/html/configureJob.html#javaConfig

你可以在这里找到一个例子:
https://github.com/spring-projects/spring-batch/tree/2.2.x/spring-batch-samples/src/main/java/org/springframework/batch/sample/config