Skip to content

Commit c49f835

Browse files
committed
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 parent 26e398e commit c49f835

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@
2727
import org.springframework.aop.framework.Advised;
2828
import org.springframework.aop.framework.ProxyFactory;
2929
import org.springframework.beans.BeanUtils;
30+
import org.springframework.context.ResourceLoaderAware;
31+
import org.springframework.core.io.ResourceLoader;
3032
import org.springframework.util.Assert;
33+
import org.springframework.util.ClassUtils;
3134

3235
/**
3336
* A {@link ProjectionFactory} to create JDK proxies to back interfaces and handle method invocations on them. By
@@ -39,11 +42,22 @@
3942
* @see SpelAwareProxyProjectionFactory
4043
* @since 1.10
4144
*/
42-
class ProxyProjectionFactory implements ProjectionFactory {
45+
class ProxyProjectionFactory implements ProjectionFactory, ResourceLoaderAware {
4346

4447
private static final boolean IS_JAVA_8 = org.springframework.util.ClassUtils.isPresent("java.util.Optional",
4548
ProxyProjectionFactory.class.getClassLoader());
4649

50+
private ResourceLoader resourceLoader;
51+
52+
/*
53+
* (non-Javadoc)
54+
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
55+
*/
56+
@Override
57+
public void setResourceLoader(ResourceLoader resourceLoader) {
58+
this.resourceLoader = resourceLoader;
59+
}
60+
4761
/*
4862
* (non-Javadoc)
4963
* @see org.springframework.data.rest.core.projection.ProjectionFactory#createProjection(java.lang.Object, java.lang.Class)
@@ -71,7 +85,8 @@ public <T> T createProjection(Class<T> projectionType, Object source) {
7185
factory.addAdvice(new TargetAwareMethodInterceptor(source.getClass()));
7286
factory.addAdvice(getMethodInterceptor(source, projectionType));
7387

74-
return (T) factory.getProxy(getClass().getClassLoader());
88+
return (T) factory
89+
.getProxy(resourceLoader == null ? ClassUtils.getDefaultClassLoader() : resourceLoader.getClassLoader());
7590
}
7691

7792
/*
@@ -141,7 +156,7 @@ protected MethodInterceptor postProcessAccessorInterceptor(MethodInterceptor int
141156
/**
142157
* Returns whether the given {@link PropertyDescriptor} describes an input property for the projection, i.e. a
143158
* property that needs to be present on the source to be able to create reasonable projections for the type the
144-
* descritor was looked up on.
159+
* descriptor was looked up on.
145160
*
146161
* @param descriptor will never be {@literal null}.
147162
* @return

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import org.springframework.beans.MutablePropertyValues;
2020
import org.springframework.beans.factory.BeanFactory;
2121
import org.springframework.beans.factory.BeanFactoryAware;
22+
import org.springframework.context.ResourceLoaderAware;
2223
import org.springframework.core.MethodParameter;
2324
import org.springframework.core.convert.ConversionService;
25+
import org.springframework.core.io.ResourceLoader;
2426
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
2527
import org.springframework.web.bind.WebDataBinder;
2628
import org.springframework.web.bind.support.WebDataBinderFactory;
@@ -34,7 +36,8 @@
3436
* @author Oliver Gierke
3537
* @since 1.10
3638
*/
37-
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor implements BeanFactoryAware {
39+
public class ProxyingHandlerMethodArgumentResolver extends ModelAttributeMethodProcessor
40+
implements BeanFactoryAware, ResourceLoaderAware {
3841

3942
private final SpelAwareProxyProjectionFactory proxyFactory;
4043
private final ConversionService conversionService;
@@ -61,6 +64,15 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
6164
this.proxyFactory.setBeanFactory(beanFactory);
6265
}
6366

67+
/*
68+
* (non-Javadoc)
69+
* @see org.springframework.context.ResourceLoaderAware#setResourceLoader(org.springframework.core.io.ResourceLoader)
70+
*/
71+
@Override
72+
public void setResourceLoader(ResourceLoader resourceLoader) {
73+
this.proxyFactory.setResourceLoader(resourceLoader);
74+
}
75+
6476
/*
6577
* (non-Javadoc)
6678
* @see org.springframework.web.method.support.HandlerMethodArgumentResolver#supportsParameter(org.springframework.core.MethodParameter)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
9797
argumentResolvers.add(sortResolver());
9898
argumentResolvers.add(pageableResolver());
9999

100-
argumentResolvers.add(new ProxyingHandlerMethodArgumentResolver(conversionService.getObject()));
100+
ProxyingHandlerMethodArgumentResolver resolver = new ProxyingHandlerMethodArgumentResolver(
101+
conversionService.getObject());
102+
resolver.setBeanFactory(context);
103+
resolver.setResourceLoader(context);
104+
105+
argumentResolvers.add(resolver);
101106
}
102107
}

0 commit comments

Comments
 (0)