Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AopUtils.getTargetClass returns a proxy class for @Configuration beans [SPR-15573] #20132

Closed
spring-projects-issues opened this issue May 21, 2017 · 1 comment
Assignees
Labels
status: declined A suggestion or change that we don't feel we should currently apply

Comments

@spring-projects-issues
Copy link
Collaborator

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:

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

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

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.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: declined A suggestion or change that we don't feel we should currently apply labels Jan 11, 2019
@spring-projects-issues spring-projects-issues removed the type: bug A general bug label Jan 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

2 participants