Velocity with Springboot 1.5.x

匿名 (未验证) 提交于 2019-12-03 02:53:02

问题:

With Springboot 1.4.4 I could use the VelocityEngine as bean directly. The configuration I did with the application.properties:

spring.velocity.properties.resource.loader=jar spring.velocity.properties.jar.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader spring.velocity.properties.jar.runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem spring.velocity.properties.jar.runtime.log.logsystem.log4j.category=velocity spring.velocity.cache=true spring.velocity.charset=UTF-8

In Springboot 1.5.x there is no Velocity Support anymore. What is the best way do integrate this configuration in Springboot 1.5.x?

I already added the dependency:

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

And created the Bean:

@Bean VelocityEngine velocityEngine(){     return new VelocityEngine(); }

But the Properties are missing.

With

@Autowired ConfigurableEnvironment configurableEnvironment;

I could parse the Properties, but it feels wrong.

回答1:

I will follow Jespers advice to use FreeMarker.

In order to answer my question, if someone cannot switch technologies but wants to move to Springboot 1.5.x, here as an easy solution: Properties need to be changed, remove spring.velocity.properties:

resource.loader=jar jar.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader jar.runtime.log.logsystem.class=org.apache.velocity.runtime.log.SimpleLog4JLogSystem jar.runtime.log.logsystem.log4j.category=velocity jar.resource.loader.cache=true input.encoding=UTF-8

Add the properties created the Bean:

@Bean VelocityEngine velocityEngine(){     Properties properties = new Properties();     properties.load(this.getClass().getResourceAsStream("/application.properties"));     return new VelocityEngine(properties); }

One important drawback is, with that solution you cannot change your property file name without changing it for the Velocity engine as well. So it removes some of the flexibility of Springboot.



转载请标明出处:Velocity with Springboot 1.5.x
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!