加载applicatonContext时的Spring Velocity异常

时间:2012-06-06 16:27:29

标签: spring velocity

在我们的应用程序中使用spring,velocity作为电子邮件的代码,我得到了以下例外。有任何想法来解决这个问题。

 "org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.ui.velocity.VelocityEngineFactoryBean' to required type 
'org.apache.velocity.app.VelocityEngine' for property 'velocityEngine'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 
[org.springframework.ui.velocity.VelocityEngineFactoryBean] to required type [org.apache.velocity.app.VelocityEngine] for property 'velocityEngine': no matching editors or conversion strategy found "

以下是我的上下文配置。

http://www.springframework.org/schema/beans/spring-beans.xsd“>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="xxxx.US"/>
    <property name="port" value="25"/>
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">false</prop>
            <prop key="mail.smtp.starttls.enable">false</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader       
        </value>
    </property>
</bean>

<bean id="emailUtil" class="com.example.MailUtil">
    <property name="mailSender" ref="mailSender" /> 
    <property name="velocityEngine" ref="velocityEngine" />
</bean>

2 个答案:

答案 0 :(得分:0)

您不需要在emailUtil bean中引用velocityEngine。关于你的spring配置的其他所有内容都是正确的。

我从我的应用程序中抓取了以下代码,向您展示我们如何使用速度模板。

private String generateMessageFromTemplate(User user, VelocityTemplateEnum velocityTemplateEnum) {
    VelocityContext context = new VelocityContext();
    context.put("user", user);
    StringWriter stringWriter = new StringWriter();
    String template = velocityTemplateDao.findById(velocityTemplateEnum.name()).getTemplate();
    Velocity.evaluate(context, stringWriter, velocityTemplateEnum.name(), template);
    return stringWriter.getBuffer().toString();
}

答案 1 :(得分:0)

我有类似的设置,只有我在VelocityEngine中自动装配,这对我有用。是否为您自动选择了一个选项?另外,您可以为MailUtil类添加代码吗?也许使用velocityEngine字段/ setter不是100%正确吗?