You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just discovered that AopUtils.getTargetClass will return the proxy class (not the target class) when requesting the bean for an @Configuration class.
Looking at the code for getTargetClass, it expects the object to be an instance of SpringProxy, but it is instead just an instance of ConfigurationClassEnhancer.EnhancedConfiguration — which does not implement SpringProxy.
The following simple Spring Boot application demonstrates this issue:
package example;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import javax.annotation.PostConstruct;
import java.util.Arrays;
@SpringBootApplication
public class ExampleApplication {
@Autowired
private ExampleConfiguration exampleConfiguration;
public static void main(String[] args) {
new SpringApplicationBuilder().sources(ExampleApplication.class).build().run();
}
@PostConstruct
public void demonstrateIssue() {
// class example.ExampleConfiguration$$EnhancerBySpringCGLIB$$2a89a1c2
System.out.println(AopUtils.getTargetClass(exampleConfiguration));
// class example.ExampleConfiguration
System.out.println(AopUtils.getTargetClass(exampleConfiguration).getSuperclass());
// [interface org.springframework.context.annotation.ConfigurationClassEnhancer$EnhancedConfiguration]
System.out.println(Arrays.toString(AopUtils.getTargetClass(exampleConfiguration).getInterfaces()));
}
}
package example;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ExampleConfiguration {
}
Affects: 4.3.8
The text was updated successfully, but these errors were encountered:
This is by design: AopUtils.getTargetClass strictly only operates on AOP proxies with a distinct generated proxy instance and a separate unmodified target instance backing it. Configuration classes on the other hand are a single instance of a generated subclass and therefore not an AOP proxy in that sense; there is semantically no separate instance of an unmodified target class here.
If you'd like a common way to obtain the original class for CGLIB-generated classes, consider ClassUtils.getUserClass: This works for configuration classes as well as target-class proxies.
M. Justin opened SPR-15573 and commented
I just discovered that AopUtils.getTargetClass will return the proxy class (not the target class) when requesting the bean for an
@Configuration
class.Looking at the code for getTargetClass, it expects the object to be an instance of SpringProxy, but it is instead just an instance of ConfigurationClassEnhancer.EnhancedConfiguration — which does not implement SpringProxy.
The following simple Spring Boot application demonstrates this issue:
Affects: 4.3.8
The text was updated successfully, but these errors were encountered: