VelocityEngineUtils has been removed in Spring 3.2 so what else to use?

风流意气都作罢 提交于 2019-11-29 03:54:17

I don't think VelocityEngineUtils was in the spring-context jar (at least not since Spring last 3.1.x releases, according to GitHub).

Anyway, you can find it in spring-context-support-3.2.0.RELEASE.jar

I wanted to add a complete answer.

First, you add the dependency:

    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.6.4</version>
    </dependency>

Then, if you had a custom VelocityEngineFactory like this;

@Bean
public VelocityEngineFactory velocityEngine(){
    VelocityEngineFactoryBean bean = new VelocityEngineFactoryBean();
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty("resource.loader", "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    bean.setVelocityProperties(properties);
    return bean;
}

you need to replace it with a bean definition, something like below (to your @Configuration class);

@Bean
public VelocityEngine velocityEngine() throws Exception {
    Properties properties = new Properties();
    properties.setProperty("input.encoding", "UTF-8");
    properties.setProperty("output.encoding", "UTF-8");
    properties.setProperty("resource.loader", "class");
    properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine velocityEngine = new VelocityEngine(properties);
    return velocityEngine;
}

Last, use it as:

@Autowired
private VelocityEngine velocityEngine;

public String prepareRegistrationEmailText(User user) {
    VelocityContext context = new VelocityContext();
    context.put("username", user.getUsername());
    context.put("email", user.getEmail());
    StringWriter stringWriter = new StringWriter();
    velocityEngine.mergeTemplate("registration.vm", "UTF-8", context, stringWriter);
    String text = stringWriter.toString();
    return text;
}

Good luck.

Regarding the deprecation of VelocityEngineUtils in Spring, the Spring documentation isn't very clear on what to use instead.

It just says:

Deprecated. Use mergeTemplateIntoString(VelocityEngine, String, String, Map) instead, following Velocity 1.6's corresponding deprecation in its own API.

And to make it more confusing, the link refers back to itself.

Basically it's just saying use Velocity itself.

Here's a description of how you do that..

 // instead of a model map, you use a VelocityContext

 VelocityContext velocityContext = new VelocityContext();
 velocityContext.put("key1", value1);
 velocityContext.put("key2", value2);

 // the velocityEngine you wired into Spring has a mergeTemplate function
 // you can use to do the same thing as VelocityEngineUtils.mergeTemplate
 // with the exception that it uses a writer instead of returning a String      

 StringWriter stringWriter = new StringWriter();
 velocityEngine.mergeTemplate("templateName.vm", "UTF-8", velocityContext, stringWriter);

 // this is assuming you're sending HTML email using MimeMessageHelper

 message.setText(stringWriter.toString(), true);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!