@@ -1362,6 +1362,7 @@ public void clearMetadataCache() {
1362
1362
@ Nullable
1363
1363
protected Class <?> resolveBeanClass (final RootBeanDefinition mbd , String beanName , final Class <?>... typesToMatch )
1364
1364
throws CannotLoadBeanClassException {
1365
+
1365
1366
try {
1366
1367
if (mbd .hasBeanClass ()) {
1367
1368
return mbd .getBeanClass ();
@@ -1391,13 +1392,16 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
1391
1392
throws ClassNotFoundException {
1392
1393
1393
1394
ClassLoader beanClassLoader = getBeanClassLoader ();
1394
- ClassLoader classLoaderToUse = beanClassLoader ;
1395
+ ClassLoader dynamicLoader = beanClassLoader ;
1396
+ boolean freshResolve = false ;
1397
+
1395
1398
if (!ObjectUtils .isEmpty (typesToMatch )) {
1396
1399
// When just doing type checks (i.e. not creating an actual instance yet),
1397
1400
// use the specified temporary class loader (e.g. in a weaving scenario).
1398
1401
ClassLoader tempClassLoader = getTempClassLoader ();
1399
1402
if (tempClassLoader != null ) {
1400
- classLoaderToUse = tempClassLoader ;
1403
+ dynamicLoader = tempClassLoader ;
1404
+ freshResolve = true ;
1401
1405
if (tempClassLoader instanceof DecoratingClassLoader ) {
1402
1406
DecoratingClassLoader dcl = (DecoratingClassLoader ) tempClassLoader ;
1403
1407
for (Class <?> typeToMatch : typesToMatch ) {
@@ -1406,6 +1410,7 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
1406
1410
}
1407
1411
}
1408
1412
}
1413
+
1409
1414
String className = mbd .getBeanClassName ();
1410
1415
if (className != null ) {
1411
1416
Object evaluated = evaluateBeanDefinitionString (className , mbd );
@@ -1415,18 +1420,31 @@ private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToM
1415
1420
return (Class <?>) evaluated ;
1416
1421
}
1417
1422
else if (evaluated instanceof String ) {
1418
- return ClassUtils .forName ((String ) evaluated , classLoaderToUse );
1423
+ className = (String ) evaluated ;
1424
+ freshResolve = true ;
1419
1425
}
1420
1426
else {
1421
1427
throw new IllegalStateException ("Invalid class name expression result: " + evaluated );
1422
1428
}
1423
1429
}
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 );
1428
1444
}
1429
1445
}
1446
+
1447
+ // Resolve regularly, caching the result in the BeanDefinition...
1430
1448
return mbd .resolveBeanClass (beanClassLoader );
1431
1449
}
1432
1450
0 commit comments