diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java index a3a87f2117f8..d941bdac4c9c 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 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. @@ -22,6 +22,7 @@ import org.springframework.aop.Advisor; import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.core.SmartClassLoader; import org.springframework.lang.Nullable; /** @@ -89,7 +90,13 @@ public Object postProcessAfterInitialization(Object bean, String beanName) { } proxyFactory.addAdvisor(this.advisor); customizeProxyFactory(proxyFactory); - return proxyFactory.getProxy(getProxyClassLoader()); + + // Use original ClassLoader if bean class not locally loaded in overriding class loader + ClassLoader classLoader = getProxyClassLoader(); + if (classLoader instanceof SmartClassLoader && classLoader != bean.getClass().getClassLoader()) { + classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader(); + } + return proxyFactory.getProxy(classLoader); } // No proxy needed. diff --git a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java index 52a2678604a5..79c1fe20ca33 100644 --- a/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java +++ b/spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java @@ -459,11 +459,12 @@ protected Object createProxy(Class beanClass, @Nullable String beanName, proxyFactory.setPreFiltered(true); } - ClassLoader targetClassLoader = getProxyClassLoader(); - if (targetClassLoader instanceof SmartClassLoader && targetClassLoader != beanClass.getClassLoader()) { - targetClassLoader = ((SmartClassLoader) targetClassLoader).getOriginalClassLoader(); + // Use original ClassLoader if bean class not locally loaded in overriding class loader + ClassLoader classLoader = getProxyClassLoader(); + if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) { + classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader(); } - return proxyFactory.getProxy(targetClassLoader); + return proxyFactory.getProxy(classLoader); } /**