如何在Spring中使用速度1.7

ash*_*ram 6 spring velocity

我使用速度1.7与spring 3.1框架发送电子邮件.velocity用于电子邮件模板.

以下是配置

<bean id="velocityEngine"
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <props>
            <prop key="resource.loader">class</prop>
            <prop key="class.resource.loader.class">
                org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </prop>
        </props>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

以下是我的代码

 @Component
 public class EmailUtils {

    @Autowired
    private static VelocityEngine velocityEngine;

    public static void sendMail(String subject, Map data, String template,
            String toName, String toAddress) {


        HtmlEmail email = new HtmlEmail();

        try {
            email.setHostName(hostName);
            email.setSmtpPort(smtpPort);
            email.setSubject(subject);

            System.out.println(template +" template");
            System.out.println(data +" data ");
            System.out.println(velocityEngine +" velocityEngine ");

            String message = VelocityEngineUtils.mergeTemplateIntoString(
                    velocityEngine, template, data);

            System.out.println(message +" message message ");

            email.setMsg(message);
            email.addTo(toAddress, toName);
            email.setFrom(fromAddress, fromName);
            email.send();
        } catch (EmailException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行应用程序时,我得到以下错误.

java.lang.NullPointerException
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)
Run Code Online (Sandbox Code Playgroud)

因为速度引擎是null.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<bean id="velocityEngine"
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <props>
            <prop key="resource.loader">class</prop>
            <prop key="class.resource.loader.class">
                org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
            </prop>
        </props>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

请帮我.我还需要做其他任何配置吗?

ndt*_*viv 9

我在使用时遇到了同样的问题.我可以通过以下方式解决它:

@Configuration
public class ApplicationConfiguration {

    @Bean
    public VelocityEngine getVelocityEngine() throws VelocityException, IOException{
        VelocityEngineFactory factory = new VelocityEngineFactory();
        Properties props = new Properties();
        props.put("resource.loader", "class");
        props.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        factory.setVelocityProperties(props);
        return factory.createVelocityEngine();      
    }
}
Run Code Online (Sandbox Code Playgroud)

但相反 - 因为我正在为我的视图使用速度模板并且已经声明了VelocityConfigurer,所以我在我的VelocityConfigurer声明中使用Autowired并在其上调用了getVelocityEngine().

我确实发现,如果它被自动装入@Service-s或@ Component-s并且您已在共享的applicationContext.xml文件中声明,那么xml声明也必须在该共享的applicationContext.xml中,而不是xxx-servlet.xml配置.

我认为这是因为applicationContext.xml在应用程序中的所有servlet之间共享,因此必须在xxx-servlet.xml文件之前完全处理它.

或者,您可以启用spring bean工厂调试,看看它是否在实例化它时遇到任何问题:

log4j.category.org.springframework.beans.factory=DEBUG
Run Code Online (Sandbox Code Playgroud)


mre*_*isz 5

我认为你不能使用@Autowired静态字段.您可以使用非静态setter解决此问题:

@Autowired
public void setVelocityEngine(VelocityEngine ve) {
  EmailUtils.velocityEngine = ve;
}
Run Code Online (Sandbox Code Playgroud)

或以其他方式,但我强烈建议EmailUtils变成一个非静态bean.Spring更好地处理非静态组件(不需要丑陋的黑客),可以交换实现,你的代码将坚持DI philosphy.