-
Notifications
You must be signed in to change notification settings - Fork 37.7k
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
Support late detection of ApplicationListener interface in objects returned from @Bean methods [SPR-6060] #10728
Comments
Chris Beams commented Attaching updated version of original repro class. This is up-to-date with Spring 3 support for |
Chris Beams commented Attaching SPR6060Tests and aop-config.xml - a more concise reproduction of the issues in question. |
Chris Beams commented After reviewing this issue, the behavior in question is not actually a bug, but rather simply the way the Spring container works. When a bean class is declared in XML, the class is evaluated to see if it implements any Spring-specific interfaces, such as However, in As a workaround, you could modify the It is at least conceivable that the framework could evaluate bean instances after creation (as opposed to bean classes before creation) to determine whether In the meantime, you have two options:
|
Thomas commented Returning the concrete type is equivalent to what is happening when declaring the bean in XML, even though it is not intuitive when using the "nice" Java based configuration approach. It initially appears rather strange when the type returned from the application context is not the type declared on the method (JdkDynamicAopProxy which erases concrete type). Cases where the caller would expect and cast to the concrete class will fail. Again, this is just what would happen with XML, but seems rather counter intuitive when doing everything in Java. I had to use option (1.) in the actual application I work on, just returning the concrete class implementing all interfaces (2.) would not work. I was not able to reproduce it in a simple test case. If I ever find out why I will update this ticket, perhaps the issue will magically go away when moving to Spring 3.x.. I read now there is the need to use |
Chris Beams commented Hi Thomas,
Agreed. Ideally, it would be possible to return an abstract type or interface and still have container-specific interfaces recognized against the concrete bean type. This issue remains open to prompt further looking into that possibility
As you mention, this is exactly the way it works in XML, and is usually not a problem because folks preferentially assign beans to an interface rather than to the concrete bean type. Keep in mind, however, that if you need/want to assign to the concrete type, you can always switch away from using JDK dynamic proxies and go with the CGLIB-based subclass proxy approach. This is done through the
Yes -
There are a number of decisions to be made here, but we do intend to shore up this gap. In the meantime, Thank you, and please keep the comments coming. It's rather important to know how |
Juergen Hoeller commented After a bit of research, this seems to affect the ApplicationListener interface only: All other common container interfaces are detected against the instantiated object, independent from the declared type. ApplicationListeners get detected through by-type retrieval against bean definitions. As a special case, we also check inner bean instances (since Spring 3.0). So it seems we just need to add an instance check for top-level beans as well, for cases where haven't detected the ApplicationListener interface against the declared type. Juergen |
Juergen Hoeller commented Fixed as of 3.0.1: ApplicationListeners will get detected lazily as well now, through a revision of ApplicationContextAwareProcessor's listener detection. Juergen |
Thomas opened SPR-6060* and commented
Issue attempting to directly implement multiple unrelated interfaces (business interface plus ApplicationListener) in a JavaConfig managed bean (JavaConfig 1.0.0.M4).
Either the generated proxy and or behavior of the container with regard to ApplicationListener support are not as expected.
Attached is a single class test case to demonstrate the issue. Switch return type in configuration and observe the change in behavior:
Affects: 2.5.6
Reference URL: http://forum.springsource.org/showthread.php?t=76853
Attachments:
The text was updated successfully, but these errors were encountered: