Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use velocity 1.7 with Spring

I am using velocity 1.7 withe spring 3.1 framework for sending email. velocity is used for email templates.

Below is the configuration

<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>

and below is my code

 @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();
        }
    }
}

When I run the application I get following error.

java.lang.NullPointerException
at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:58)

as the velocity engine is 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>

Please help me. Is there any other configuration that I need to do?

like image 570
ashishjmeshram Avatar asked Mar 05 '12 05:03

ashishjmeshram


2 Answers

I had the same issue when using . I could have solved it in the following way:

@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();      
    }
}

But instead - because I was using velocity templates for my views and had already declared a VelocityConfigurer, so I instead Autowired in my declaration of VelocityConfigurer and called getVelocityEngine() on that.

I did discover that, if it's being autowired into @Service-s or @Component-s and you've declared in a shared applicationContext.xml file, then the xml declaration had to be in that shared applicationContext.xml as well rather than the xxx-servlet.xml config.

I think this is because applicationContext.xml is shared amongst all servlets in the application, so it has to be fully processed before the xxx-servlet.xml files are.

Alternatively, you could enable spring bean factory debugging and see if it's having any issues instantiating it:

log4j.category.org.springframework.beans.factory=DEBUG
like image 200
ndtreviv Avatar answered Nov 02 '22 23:11

ndtreviv


I think you cannot use @Autowired with a static field. You can work around this with a non-static setter:

@Autowired
public void setVelocityEngine(VelocityEngine ve) {
  EmailUtils.velocityEngine = ve;
}

or in another way but I would strongly recommend turning EmailUtils into a non-static bean. Spring handles non-static components much better (no ugly hacks needed), it will be possible to swap implementations and your code will stick to DI philosphy.

like image 41
mrembisz Avatar answered Nov 03 '22 01:11

mrembisz