Skip to content

Commit 5cd525a

Browse files
committed
Enforce use of ClassLoader.loadClass in case of temporary ClassLoader
Issue: SPR-17452
1 parent 2ac4355 commit 5cd525a

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ public void clearMetadataCache() {
13621362
@Nullable
13631363
protected Class<?> resolveBeanClass(final RootBeanDefinition mbd, String beanName, final Class<?>... typesToMatch)
13641364
throws CannotLoadBeanClassException {
1365+
13651366
try {
13661367
if (mbd.hasBeanClass()) {
13671368
return mbd.getBeanClass();
@@ -1391,13 +1392,16 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
13911392
throws ClassNotFoundException {
13921393

13931394
ClassLoader beanClassLoader = getBeanClassLoader();
1394-
ClassLoader classLoaderToUse = beanClassLoader;
1395+
ClassLoader dynamicLoader = beanClassLoader;
1396+
boolean freshResolve = false;
1397+
13951398
if (!ObjectUtils.isEmpty(typesToMatch)) {
13961399
// When just doing type checks (i.e. not creating an actual instance yet),
13971400
// use the specified temporary class loader (e.g. in a weaving scenario).
13981401
ClassLoader tempClassLoader = getTempClassLoader();
13991402
if (tempClassLoader != null) {
1400-
classLoaderToUse = tempClassLoader;
1403+
dynamicLoader = tempClassLoader;
1404+
freshResolve = true;
14011405
if (tempClassLoader instanceof DecoratingClassLoader) {
14021406
DecoratingClassLoader dcl = (DecoratingClassLoader) tempClassLoader;
14031407
for (Class<?> typeToMatch : typesToMatch) {
@@ -1406,6 +1410,7 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
14061410
}
14071411
}
14081412
}
1413+
14091414
String className = mbd.getBeanClassName();
14101415
if (className != null) {
14111416
Object evaluated = evaluateBeanDefinitionString(className, mbd);
@@ -1415,18 +1420,31 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
14151420
return (Class<?>) evaluated;
14161421
}
14171422
else if (evaluated instanceof String) {
1418-
return ClassUtils.forName((String) evaluated, classLoaderToUse);
1423+
className = (String) evaluated;
1424+
freshResolve = true;
14191425
}
14201426
else {
14211427
throw new IllegalStateException("Invalid class name expression result: " + evaluated);
14221428
}
14231429
}
1424-
// When resolving against a temporary class loader, exit early in order
1425-
// to avoid storing the resolved Class in the bean definition.
1426-
if (classLoaderToUse != beanClassLoader) {
1427-
return ClassUtils.forName(className, classLoaderToUse);
1430+
if (freshResolve) {
1431+
// When resolving against a temporary class loader, exit early in order
1432+
// to avoid storing the resolved Class in the bean definition.
1433+
if (dynamicLoader != null) {
1434+
try {
1435+
return dynamicLoader.loadClass(className);
1436+
}
1437+
catch (ClassNotFoundException ex) {
1438+
if (logger.isTraceEnabled()) {
1439+
logger.trace("Could not load class [" + className + "] from " + dynamicLoader + ": " + ex);
1440+
}
1441+
}
1442+
}
1443+
return ClassUtils.forName(className, dynamicLoader);
14281444
}
14291445
}
1446+
1447+
// Resolve regularly, caching the result in the BeanDefinition...
14301448
return mbd.resolveBeanClass(beanClassLoader);
14311449
}
14321450

0 commit comments

Comments
 (0)