我需要将 currentDate 作为 String 传递给附加在 subject 中的 sendMetaStatsTask tasklet。 现在,如果我使用以下 xml 创建一个 scope="step"的 bean
<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>
它抛出:-
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)
但是,如果我使用 prototype ,它工作正常。
需要问一下为什么它在步骤范围内不起作用,我是不是漏掉了什么?
最佳答案
您需要告诉 bean currentDate 范围代理,以便任何注入(inject)仅对范围步骤有效。 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>
关于java - Spring Bean 初始化 - 通过 xml 作为字符串传递的日期不适用于步骤范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40733468/