Spring注入实现bean

时间:2016-01-28 11:19:27

标签: java spring config

我有一个来自conf spring文件的以下片段:

<bean id="taskletStep" abstract="true" class="org.springframework.batch.core.step.tasklet.TaskletStep">
        <property name="jobRepository" ref="jobRepository"/>
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <bean id="hello" class="com.techavalanche.batch.PrintTasklet">
        <property name="message" value="Hello"/>
    </bean>

    <bean id="world" class="com.techavalanche.batch.PrintTasklet">
        <property name="message" value=" World!"/>
    </bean>

    <bean id="mySimpleJob" class="org.springframework.batch.core.job.SimpleJob">
        <property name="name" value="mySimpleJob" />
        <property name="steps">
            <list>
                <bean parent="taskletStep">
                    <property name="tasklet" ref="hello"/>
                </bean>

                <bean parent="taskletStep">
                    <property name="tasklet" ref="world"/>
                </bean>
            </list>
        </property>

对我来说,下面的内容不是很清楚:

<bean parent="taskletStep">
                    <property name="tasklet" ref="hello"/>
                </bean>

taskletStep它是一个接口,没有任何属性。但是代码工作正常,似乎ID为“hello”的bean被注入。因此我问,这是从配置文件向接口bean(id: taskletStep )注入bean实现(id: hello )的标准方法吗?

1 个答案:

答案 0 :(得分:1)

它是Spring Batch Job中的一个tasklet步骤。你在做什么都很好。或者,您也可以这样做:

<job id="mySimpleJob">
    <step id="step1">
        <tasklet ref="hello"/>
    </step>

    <step id="step2">
        <tasklet ref="world"/>
    </step>
</job>

这是一个完整的参考: http://docs.spring.io/spring-batch/reference/htmlsingle/