问题
On starting up my Web app with Velocity 2.0 I'm getting the following error:
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
All dependencies have been satisfied. We are using
- slf4j-api-1.7.25.jar
- slf4j-simple-1.7.25.jar
- commons-lang3-3.5.jar
- commons-io-2.5.jar
- commons-logging-1.2.jar (yes we do have Commons-Logging)
In applicationContext.xml the velocityEngine bean is defined as follows
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean"/>
Any thoughts on this?
My file velocity-engine-core-2.0.jar
only contains the following .runtime
subpackages:
defaults, directive, parser, resource, visitor
but no log
.
UPDATE The following overrideLogging = FALSE in the Spring Velocity Engine bean declaration solved the problem. But why?
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="overrideLogging" value="false" />
</bean>
I just followed a tip on https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ui/velocity/VelocityEngineFactory.html but not sure what happened.
回答1:
When overrideLogging is true, the class org.apache.velocity.runtime.log.CommonsLogLogChute is still required by Spring while it has disappeared in Velocity 2.0, since Velocity now uses the slf4j logging framework.
You'll need to wait for an update of the Spring Velocity classes if you need overrideLogging to be true.
回答2:
Velocity made a change in logging:
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
CommonsLogLogChute added in before major version velocity 1.7:
Add a CommonsLogLogChute that allows logging through commons-logging.
So you probably have an old jar or configuration in your runtime environment.
回答3:
Alibaba implemented a support context package for that case: https://github.com/alibaba/spring-velocity-support
Just add to 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>
But beware that your project now uses Velocity 2.0.
来源:https://stackoverflow.com/questions/46118555/velocity-2-0-noclassdeffounderror-org-apache-velocity-runtime-log-commonsloglo