Browse Source

DATACMNS-718 - Class loader improvements in ProxyProjectionFactory.

ProxyProjectionFactory now implements ResourceLoaderAware to be able to use the class loader used by the framework. SpringDataWebConfiguration now forwards the Application context as ResourceLoader through ProxyingHandlerMethodArgumentResolver into the target factory.
1.10.x
Oliver Gierke 11 years ago
parent
commit
b6990bbef6
  1. 21
      src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java
  2. 14
      src/main/java/org/springframework/data/web/ProxyingHandlerMethodArgumentResolver.java
  3. 7
      src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java

21
src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java

@ -27,7 +27,10 @@ import org.aopalliance.intercept.MethodInvocation; @@ -27,7 +27,10 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
@ -41,11 +44,22 @@ import com.fasterxml.jackson.annotation.JsonIgnore; @@ -41,11 +44,22 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
* @see SpelAwareProxyProjectionFactory
* @since 1.10
*/
class ProxyProjectionFactory implements ProjectionFactory {
class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware {
private static final boolean IS_JAVA_8 = org.springframework.util.ClassUtils.isPresent("java.util.Optional",
ProxyProjectionFactory.class.getClassLoader());
private ResourceLoader resourceLoader;
/*
* (non-Javadoc)
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
*/
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/*
* (non-Javadoc)
* @see org.springframework.data.rest.core.projection.ProjectionFactory#createProjection(java.lang.Object, java.lang.Class)
@ -73,7 +87,8 @@ class ProxyProjectionFactory implements ProjectionFactory { @@ -73,7 +87,8 @@ class ProxyProjectionFactory implements ProjectionFactory {
factory.addAdvice(new TargetClassAwareMethodInterceptor(source.getClass()));
factory.addAdvice(getMethodInterceptor(source, projectionType));
return (T) factory.getProxy(getClass().getClassLoader());
return (T) factory
.getProxy(resourceLoader == null ? ClassUtils.getDefaultClassLoader() : resourceLoader.getClassLoader());
}
/*
@ -143,7 +158,7 @@ class ProxyProjectionFactory implements ProjectionFactory { @@ -143,7 +158,7 @@ class ProxyProjectionFactory implements ProjectionFactory {
/**
* Returns whether the given {@link PropertyDescriptor} describes an input property for the projection, i.e. a
* property that needs to be present on the source to be able to create reasonable projections for the type the
* descritor was looked up on.
* descriptor was looked up on.
*
* @param descriptor will never be {@literal null}.
* @return

14
src/main/java/org/springframework/data/web/ProxyingHandlerMethodArgumentResolver.java

@ -19,8 +19,10 @@ import org.springframework.beans.BeansException; @@ -19,8 +19,10 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.support.WebDataBinderFactory;
@ -34,7 +36,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver; @@ -34,7 +36,8 @@ import org.springframework.web.method.support.HandlerMethodArgumentResolver;
* @author Oliver Gierke
* @since 1.10
*/
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor implements BeanFactoryAware {
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor
implements BeanFactoryAware, ResourceLoaderAware {
private final SpelAwareProxyProjectionFactory proxyFactory;
private final ConversionService conversionService;
@ -61,6 +64,15 @@ public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodP @@ -61,6 +64,15 @@ public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodP
this.proxyFactory.setBeanFactory(beanFactory);
}
/*
* (non-Javadoc)
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
*/
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.proxyFactory.setResourceLoader(resourceLoader);
}
/*
* (non-Javadoc)
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter)

7
src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java

@ -97,6 +97,11 @@ public class SpringDataWebConfiguration extends WebMvcConfigurerAdapter { @@ -97,6 +97,11 @@ public class SpringDataWebConfiguration extends WebMvcConfigurerAdapter {
argumentResolvers.add(sortResolver());
argumentResolvers.add(pageableResolver());
argumentResolvers.add(new ProxyingHandlerMethodArgumentResolver(conversionService.getObject()));
ProxyingHandlerMethodArgumentResolver resolver = new ProxyingHandlerMethodArgumentResolver(
conversionService.getObject());
resolver.setBeanFactory(context);
resolver.setResourceLoader(context);
argumentResolvers.add(resolver);
}
}

Loading…
Cancel
Save