Search code examples
javaspring-bootspring-mvcvelocityspring-context

Cannot access org.springframework.ui.velocity.VelocityEngineFactory Method setVelocityEngine()


i have upgraded my java web project from spring boot 1.5.22 to 2.6.6. During this Upgrade the Velocity package is not even deprecated, it got removed. I know that it is recommended to switch to FreeMarker, but as a quick fix i was trying to fix my project. First i included the following three dependencies to get the old velocity package and classes.

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.25.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.3.RELEASE</version>
    </dependency>

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

After this most of my code got fixed after some changes. The last remaining problem in my configuration bean is my VelocityConfigurer. I am trying to init a VelocityEngine with some properties and to create a VelocityConfigurer with the freshly created VelocityEngine afterwards. Like i did it before the spring boot update.

import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;

@Configuration
public class MailConfig {

@Bean
@Primary
public VelocityConfigurer velocityEngineBean() {

    VelocityEngine engine = new VelocityEngine();
    engine.setProperty(Velocity.RESOURCE_LOADER, "ds");
    engine.setProperty("ds.resource.loader.class", "XXX.CustomDataResourceLoader");

    engine.setProperty("spring.velocity.checkTemplateLocation=false", "false");
    engine.setProperty("spring.velocity.velocimacro.library", "XXX.vm");

    engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, Slf4jLogChute.class.getName());

    engine.init();

    VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
    velocityConfigurer.setVelocityEngine(engine);
    return velocityConfigurer;
}

}

But i get the following error. Error: Cannot access org.springframework.ui.velocity.VelocityEngineFactory

I can figure out why it can access this method. The .jar with the Class is there.VelocityEngineFactory


Solution

  • This won’t work as Spring Boot 2.6 requires Spring Framework 5.3. Velocity support was deprecated in Spring Framework 4.3 and removed in 5.0. If you want to use an up-to-date and supported version of Spring Boot (2.5.x or 2.6.x at the time of writing), you should migrate to an alternative templating engine.