From 5ef690369074888234a9616483d26e4637a126a8 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 19 Jan 2016 10:35:50 +0000 Subject: [PATCH] Make EmbeddedVelocityToolboxView work in servlet container deployments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EmbeddedVelocityToolboxView is used with both embedded containers and when a Spring Boot application is deployed to a servlet container. It process the ServletContext to intercept calls to getResource so that it can load Velocity’s toolbox.xml file from within an executable jar. Previously, when it created the proxy, it didn’t specify the ClassLoader to use. This resulted in the proxy being created using the ClassLoader that loaded the Class that is being proxied. This fails when the application is deployed to a ServletContainer as the ClassLoader that loaded the ServletContext implementation is the container’s ClassLoader and it cannot see the classes that are specific to the web application. This commit updates EmbeddedVelocityToolboxView to specify the ClassLoader to use when creating the proxy. It specifies its own ClassLoader thereby ensuring that the proxy creation can load application-specific classes. Fixes gh-4967 --- .../view/velocity/EmbeddedVelocityToolboxView.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/velocity/EmbeddedVelocityToolboxView.java b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/velocity/EmbeddedVelocityToolboxView.java index ca8ae5a9835..94955d59d9f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/velocity/EmbeddedVelocityToolboxView.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/servlet/view/velocity/EmbeddedVelocityToolboxView.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * Copyright 2012-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,10 +34,11 @@ import org.springframework.web.servlet.view.velocity.VelocityToolboxView; /** * Extended version of {@link VelocityToolboxView} that can load toolbox locations from - * the classpath as well as the servlet context. This is useful when run an embedded web - * server. + * the classpath as well as the servlet context. This is useful when running in an + * embedded web server. * * @author Phillip Webb + * @author Andy Wilkinson * @since 1.2.5 */ @SuppressWarnings("deprecation") @@ -69,7 +70,7 @@ public class EmbeddedVelocityToolboxView extends VelocityToolboxView { ProxyFactory factory = new ProxyFactory(); factory.setTarget(getServletContext()); factory.addAdvice(new GetResourceMethodInterceptor(getToolboxConfigLocation())); - return (ServletContext) factory.getProxy(); + return (ServletContext) factory.getProxy(getClass().getClassLoader()); } /**