-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Support @Autowired-like self injection [SPR-8450] #13096
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
Comments
Chris Beams commented On review of the StackOverflow thread, I wonder whether the "workaround" approach of using I understand that this approach complicates unit-testability somewhat, but the use cases for this (self-injection) are probably few and far between and thus a reasonable tradeoff. Placing within the General Backlog where it can get voted up and receive further comments, but there is no immediate intention to resolve at this time. |
Fred Clausen commented This is a bonus to me. In my specific case, it means I don't have to mess with the default cache proxying:
|
Chris Beams commented Hi Fred, Your example helps make clear why this issue is probably best considered as an advertisement for AspectJ-based advice (as opposed to the proxy approach). By using load- or compile-time weaving and spring-aspect's built in AJ aspects, you can forget about this proxy business entirely. The proxy approach hits about 80% of the use cases, and that's why we advocate it first. But cases like this are really where bytecode-weaving shines. |
Ben Fagin commented On the one hand, calling a method on a reference to your 'real' self could probably be described as an anti-pattern. On the other hand, any day that I can avoid bytecode weaving is a good day. The idea of using a Is autowiring oneself by type safe though? Is there a risk of autowiring some other bean entirely? |
Sam Brannen commented
No, it is potentially dangerous (see below).
Yes, autowiring by type could potentially result in a different bean of the same type being injected. That's why Fred Clausen, in his example above, explicitly references the bean by name in order to ensure that the bean gets a reference to itself (potentially proxied). He does this using the bean name/ID in conjunction with |
Niels Bech Nielsen commented Given that the use case for this problem is self proxy invocation, wouldn't it be simpler to address the use case through an explicit annotation, (e.g I have found the use case a few times and keeps being amazed at how people work around the problem usually in some less than elegant way. Some by autowire of some kind or by massive delegation or using static holders. Usually the use case have been identified through a massive debugging effort, because the developer was unaware of the problem in the first place. With a specific annotation it would be easy to document the effect in proxy annotations: |
Premraj Motling commented
|
Juergen Hoeller commented For 4.2, I intend to research the options and suggest a recommended solution for this scenario... A dedicated annotation may indeed be the best compromise. Juergen |
Eduardo Simioni commented I have a use case affected by this problem, where I think the solution of having a specific annotation wouldn't work, or maybe it would, but then it would have to consider collections as well, holding self and others. In my applications there is an event mechanism, to put it very simple: All
The AbstractService triggers events on some common situations in my application. Now, naturally I'm not handling events from a service inside itself, but I need this to be implemented this way so that it is generic and duplication free. The injection works normally, but there are some corner cases that I don't recall right now where it doesn't. |
Ben Fagin commented I have been using a This is the code I use in my own projects. While not the absolute best, it has worked so far.
|
Othon Crelier commented It will be really interesting to have this solved. |
Sam Brannen commented FYI: this has been resolved in the following GitHub commit: |
Peter Rader commented Not working well in 4.3.5.RELEASE! {{ /**
}} gives: but uncomment |
Peter Rader commented Ignore last message please, using 4.3.4.RELEASE solved the problem. Looks like a different bug. |
Looks like after the fix But it won't return both i.e. non self referenced candidates that are being added with
AND candidates that are Collection/Map beans & are self referenced being added with the fix i.e.
Because if condition Is there a way to get autowired candidates of both types? |
@spring-projects-issues appreciate you feedback about #13096 (comment) Thanks |
Sam Brannen opened SPR-8450 and commented
Background
Autowiring a bean with an instance of itself is not something one would normally do, but there are cases where it might be useful -- for example, to route method calls through the proxy that wraps the bean. There are of course alternatives to this, such as using load-time weaving with AspectJ proxies instead of JDK dynamic proxies.
Note that self-autowiring by name via
@Resource
is permitted by the framework; whereas, self-autowiring by type is not permitted by the framework as can be seen in the following code snippet fromDefaultListableBeanFactory
'sfindAutowireCandidates(String, Class, DependencyDescriptor)
method.The name of the bean (i.e., the bean that's trying to autowire itself) is
beanName
. That bean is in fact an autowire candidate, but the above if-condition returnsfalse
(sincecandidateName
equals thebeanName
). Thus you simply cannot autowire a bean with itself by type (at least not as of Spring 3.1 M2).Goal
Add support for self-autowiring using
@Autowired
on fields and methods.Stack Overflow Discussion
This topic was brought to our attention via a discussion on Stack Overflow.
Affects: 3.0.5
Issue Links:
@Autowired
does not work for target bean of type Collection@Autowired
self-injection in case of ambiguity18 votes, 23 watchers
The text was updated successfully, but these errors were encountered: