java - Spring Bean 初始化 - 通过 xml 作为字符串传递的日期不适用于步骤范围

标签 java xml spring spring-mvc

我需要将 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/

相关文章:

java - 如何在选择 JMenu 时将颜色 arrowIcon 更改为 JMenu

java - 有没有一种简单的方法可以判断 StackTraceElement 是否来自您的源代码?

sql - 在 SQL Server 2008 中使用 xml 和存储过程将数据插入到表中

c# - 如何从 XML 文件中删除空格

java - gradle 可以只包含特定的包并排除其他所有包吗

java - spring中如何定义命名空间

使用编码键快速制作 XML

java - Spring 启动 org.springframework.web.util.NestedServletException : Request processing failed

java - 与直接使用包装的 Twitter 等库相比,SpringSocial 为我带来了哪些额外好处

java - 尝试通过拾取频率在 Canvas 上绘图 - 处理