4

I need to pass currentDate as String to my sendMetaStatsTask tasklet appended in subject . Now, if I create a bean with scope="step" using following xml

<bean id="sendMetaStatsTask" class="org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter" scope="step">
        <property name="targetObject">
            <bean class="com.nextag.catalog.springbatch.tasklets.GenerateReportFromQueriesTasklet">
                <property name="mailTo" value="#{jobParameters['MAIL_TO']}"/>
                <property name="mailFrom" value="#{jobParameters['MAIL_FROM']?:'wizereporter@nextag.com'}"/>
                <property name="mailSubject" value="#{jobParameters['PARTNER_DOMAIN']+' Affiliate Seller Report - '+ currentDate.toString()}"/>
            </bean>
        </property>
        <property name="targetMethod" value="execute"/>
    </bean>

    <bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">
        <constructor-arg value="dd/MM/yyyy"/>
    </bean>

    <bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format" scope="step">
        <constructor-arg>
            <bean class="java.util.Date"/>
        </constructor-arg>
    </bean>

It throws :-

Error creating bean with name 'currentDate' defined in BeanDefinition defined in file [/home/nextag/Apache6/tomcat/webapps/nextag/WEB-INF/classes/META-INF/spring/batch/jobs/seller-meta-stats-logging-job.xml]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot create scoped proxy for bean 'scopedTarget.currentDate': Target type could not be determined at the time of proxy creation. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)

However, it works fine in case I use prototype .

Need to ask why it is not working in step scope, am I missing something ?

1 Answers1

3

You need to tell the bean currentDate scope proxy so any injection is only valid to scope step. A good explanation is here

<bean id="fastDateFormat" class="org.apache.commons.lang.time.FastDateFormat" factory-method="getInstance">        
        <constructor-arg value="dd/MM/yyyy"/>
    </bean>

    <bean id="currentDate" class="java.util.Date" factory-bean="fastDateFormat" factory-method="format" scope="step">
        <aop:scoped-proxy/>
        <constructor-arg>
            <bean class="java.util.Date"/>
        </constructor-arg>
    </bean>
Community
  • 1
  • 1
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • Sorry for late reply, I was busy . I think This solution didn't work because, here I am injecting singleton bean into a step-scope bean which should be available at the time of instantiation of scope bean . – Aayush Singhal Jan 16 '17 at 00:12