Skip to content

Commit

Permalink
Apply original ClassLoader in AbstractAdvisingBeanPostProcessor as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller authored and lxbzmy committed Mar 26, 2022
1 parent c075a4b commit 1229997
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 1229997

Please sign in to comment.