Velocity 2.0: NoClassDefFoundError: org/apache/velocity/runtime/log/CommonsLogLogChute

Velocity 2.0: NoClassDefFoundError: org/apache/velocity/runtime/log/CommonsLogLogChute

在使用 Velocity 2.0 启动我的 Web 应用程序时,出现以下错误:

Caused by: java.lang.NoClassDefFoundError: 
             org/apache/velocity/runtime/log/CommonsLogLogChute
    at org.springframework.ui.velocity.VelocityEngineFactory.createVelocityEngine(VelocityEngineFactory.java:240)
    at org.springframework.ui.velocity.VelocityEngineFactoryBean.afterPropertiesSet(VelocityEngineFactoryBean.java:60)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 34 more

已满足所有依赖项。我们正在使用

在applicationContext.xml中,velocityEngine bean定义如下

<bean id="velocityEngine" 
class="org.springframework.ui.velocity.VelocityEngineFactoryBean"/>

对此有什么想法吗?

我的文件velocity-engine-core-2.0.jar只包含以下.runtime个子包:

defaults, directive, parser, resource, visitor

但没有 log .

UPDATE Spring Velocity Engine bean 声明中的以下 overrideLogging = FALSE 解决了该问题。 但是为什么呢?

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
    <property name="overrideLogging" value="false" />
</bean>

我只是按照提示 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ui/velocity/VelocityEngineFactory.html 但不确定发生了什么。

Velocity 对日志记录进行了更改:

Make Velocity use the base logger namespace 'org.apache.velocity' unless specified with runtime.log.name in the configuration, and have the runtime instance log with this base namespace, and other modules log with children namespaces

在主要版本 velocity 1.7 之前添加的 CommonsLogLogChute:

Add a CommonsLogLogChute that allows logging through commons-logging.

所以您的运行时环境中可能有旧的 jar 或配置。

当 overrideLogging 为真时,class org.apache.velocity.runtime.log.CommonsLogLogChute 仍然是 Spring 所必需的,而它在 Velocity 2.0 中已经消失,因为 Velocity 现在使用 slf4j 日志框架。

如果您需要 overrideLogging 为真,则需要等待 Spring Velocity classes 的更新。

阿里巴巴为该案例实施了支持上下文包:https://github.com/alibaba/spring-velocity-support

只需添加到maven:

<!-- Spring Framework -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.framework.version}</version>
</dependency>

<!-- Spring Context Velocity -->
<dependency>
    <groupId>com.alibaba.spring</groupId>
    <artifactId>spring-context-velocity</artifactId>
    <version>1.4.3.18.RELEASE</version>
</dependency>

但请注意,您的项目现在使用 Velocity 2.0。