解决SpringBoot1.5.x版本对Velocity模板不支持的方案

解决SpringBoot1.5.x版本对Velocity模板不支持的方案

项目构建工具Gradle
build.gradle配置文件
SpringBoot版本:1.5.9.RELEASE

引入SpringBoot集成Velocity模板的依赖

进入项目build.gradle配置文件所在目录

使用gradle build --refresh-dependencies命令刷新依赖

报错的原因很明显,org.springframework.boot:spring-boot-starter-velocity:1.5.9.RELEASE这个依赖不能加载进来,原因是找不到这个依赖。于是上网查找原因说是SpringBoot1.5.x以后的版本不再支持Velocity这个模板,抱着疑问去Spring官网上寻找答案,在GitHub上关注的SpringBoot项目找到了答案,进入到自己关注的开源项目中查看,

现在SpringBoot版本已经是2.0.x了,我们在Wiki中查找历史版本

来看1.4版本和1.5版本有何区别

先看1.4版本,Spring4.3之后将Velocity标记为过时,但是此时SpringBoot1.4版本仍然是支持Velocity模板的

再看1.5版本,此时已经移除对Velocity模板的依赖,不再支持

由于此时项目使用的版本已经是1.5.9.RELEASE,为了项目稳定的运行,不能降低SpringBoot版本,于是经过讨论有两个解决方案
方案一:使用带版本号的SpringBoot整合Velocity版本
SpringBoot版本保持不变,使用spring-boot-starter-velocity的低版本依赖,最终选择的版本是1.4.6.RELEASE,重新替换SpringBoot对Velocity的依赖

重新刷新依赖

从上面可以看出其它的依赖仍然是1.5.9版本,而SpringBoot集成Velocity版本是1.4.6,我们可以来看看这个jar包到底有什么

pom.properties配置文件定义jar版本信息

pom.xml配置文件配置引入的其它依赖,下面是配置文件内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starters</artifactId>
<version>1.4.6.RELEASE</version>
</parent>
<artifactId>spring-boot-starter-velocity</artifactId>
<name>Spring Boot Velocity Starter</name>
<description>Starter for building MVC web applications using Velocity views.
Deprecated since 1.4</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>commons-digester</groupId>
<artifactId>commons-digester</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-tools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
</dependencies>
</project>
从上面依赖中看出我们关心的重点是两个jar包 velocity和velocity-tools

接下来就是重新配置Velocity
上面仅仅是将依赖导入了项目, SpringBoot1.5.x版本并不会主动加载注入Velocty模板相关类,因此需要我们在使用启动的时候主动创建VelocityEngine,注入到Spring容器中,在初始化VelocityEngine这个Bean的同时需要去加载默认两个配置文件velocity.properties配置文件和directive.properties配置文件,因为加载的机制有了变化,那么我们也要响应的修改默认配置文件的一些属性配置项,
因此在classpath下新建velocity.properties配置文件

新建的 velocity.properties 配置文件内容
resource.loader=jar
jar.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
jar.resource.loader.cache=true
input.encoding=UTF-8
而Velocity默认配置文件内容中加载是这样的
加载部分

和编码部分

是以file文件的形式加载,我们需要修改加载机制,就像我们配置文件中配置的那样使用jar的形式去加载,唯一额外的一项就是设置编码格式为UTF-8。
我们在启动项目的时候需要加载这个Bean,于是在SpringBoot启动类中加入如下代码:

这样我们就可以在SpringBoot1.5.9版本中使用Velocity模板了
上述做法是从stackoverflow参考的,地址为:

国外的大神解决方案总是那么平易近人,让人容易理解。
解决方案二:项目中直接引入Velocity模板,使用配置的方式Spring去整合Velocity
build.gradle配置引入Velocity依赖

刷新依赖

之后就是使用Spring整合Velocity模板,也就是将VelocityEngine这个Bean注入到Spring容器中,整合的方式很多,虽然项目是SpringBoot,但是依然可以使用Spring配置文件方式整合,
在整合之前还需要引入支持Velocity的一个重要jar包,这个jar包很重要,必须引入,否则无法注册VelocityEngine这个Bean。

刷新依赖

接下来是spring-context.xml配置文件需要引入Spring整合Velocity的配置

spring-service.xml配置文件内容
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-autowire="byName">
<!-- 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>
<prop key="velocimacro.library" />
<prop key="directive.foreach.counter.name">loopCounter</prop>
<prop key="directive.foreach.counter.initial.value">1</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="parser.pool.size">50</prop>
<prop key="input.encoding">UTF-8</prop>
<prop key="output.encoding">UTF-8</prop>
</props>
</property>
</bean>
</beans>
从上面整合的信息来看最重要的就是使用spring-context-support这个jar包下的 org.springframework.ui.velocity.VelocityEngineFactoryBean类来创建名称为velocityEngine的这个Bean,注入到Spring容器中。具体加载信息可以看这个类的源码,这里不再赘述。
对velocityProperties的配置是来自VelocityEngineFactoryBean的父类VelocityEngineFactory中的一个Map类型的属性velocityProperties,这里存储的是加载默认配置文件的内容,上面也提到过,源码中已经体现的很明白。
然后在SpringBoot启动类家需要加载上面配置的spring-context.xml配置文件
只要在启动类上加上
@ImportResource({"classpath:/spring/spring-context.xml"})即可

到这里就可以使用Velocity模板了。
参考文章:
以上就是两种在SpringBoot1.5.x版本中使用Velocity模板,虽然破坏了SpringBoot的灵活性,但是也不失一种解决升级SpringBoot版本的方案。



猜你喜欢

转载自blog.csdn.net/erlian1992/article/details/80792386
今日推荐