【问题标题】:Spring injecting implementation beanSpring注入实现bean
【发布时间】:2016-01-28 11:19:26
【问题描述】:

我有一个 conf spring 文件中的以下 sn-p:

<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:hello)注入接口bean(id:taskletStep)的标准方式吗?

【问题讨论】:

    标签: java spring config


    【解决方案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/

    【讨论】:

    • 好的,那么在接口中注入一个bean,在这种情况下是一个TaskletStep,可以通过在接口中使用标签吗?对我来说没有多大意义,因为接口没有属性。你知道我的意思吗?
    • 我知道你的意思。当我第一次开始使用它时,对我来说也没有任何意义。链接:docs.spring.io/spring-batch/1.0.x/spring-batch-docs/reference/…
    • @userr2004685 在步骤列表中使用 next 怎么样?即:&lt;job id="footballJob"&gt; &lt;step id="playerload" next="gameLoad"/&gt; &lt;step id="gameLoad" next="playerSummarization"/&gt; &lt;step id="playerSummarization"/&gt; &lt;/job&gt; 是否真的需要指定下一个?我以前从未见过它。
    • @Rollerball 用于指定下一步要执行的步骤。不过我从来没用过。
    • @userr2004685 是的,我的意思是......只有当你有一个步骤列表并且你想从 1 跳到 3 或任何其他步骤时才有意义.. 否则它会按顺序一个接一个地迭代......
    猜你喜欢
    • 2017-10-19
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多