Jul 19th, 2012, 05:32 AM
@Autowired Spring singletons are initialized more than once, wired incorrectly
This post is a spin-off from my SO post  which contains full details.
The short story is that I have a bean graph with singleton beans (let's call them A, B and C), @Autowired to each other as circular dependencies. The trouble is that after initial app context startup beans B and C contain different @Autowired instances of bean A.
My investigation shows that this problem is specific to @Autowiring and, unfortunately, this can not be reproduced on a minimized scale.
Does this look like a bug in Spring to anyone? Or is this expected (and if so - documented)?
Jul 19th, 2012, 06:38 AM
I doubt it is a bug but something in your setup (but that might be just me). Could you post the full logging and configuration and how you are testing things, there isn't much to go in in the post.
Jul 19th, 2012, 07:02 AM
I realize the minified version doesn't replicate the same bug, but unfortunately I can not post the entire project which is hundreds of thousands of LOC (of non-open-source commercial product).
Just the app context bootstrap log is ten megabytes. Log grep result against the offending bean (which gets initialized more than once and wired as different instances) is ~50Kb, posted here: http://pastebin.com/kbyVdCpT. The offending bean id is vyre.publishing.path.PathWorker (same as interface class name), implemented in vyre.publishing.path.PathWorkerImpl. Hope this helps.
The log lines correspond to what I have discovered by enabling the debug. For each public no args constructor invocation, Creating shared instance of singleton bean 'vyre.publishing.path.PathWorker' is logged. My understanding is that singleton beans shouldn't be instantiated more than once and the log/debugging confirms the opposite.
Jul 19th, 2012, 07:12 AM
As stated I doubt it is a bug and more or less something introduced through your setup (else I would expect that due to the vast amount of Spring usage it would have popped up before). Looking at the snippet it appears as if the context gets initialized a couple of times at least from what I see, are you sure the context is initialized only once?
Also are you sure that the same bean isn't component-scanned and declared in xml and you aren't scanning/loading xml twice (both DispatcherServlet and ContextLoaderListener?).
Its quite hard to debug without actual code/configuration or a test case which reproduces this so the only thing I can do is give some pointers...
Jul 19th, 2012, 09:23 AM
I'm actually pretty sure the context is only initialized and bean is only defined once. The ContextLoaderListener is actually used in order to provide other webapps with the same application context rather than create another one.
As far as I know, mixing <context:annotation-config/> and <context:component-scan/> should not result duplicate bean definitions, at least according to this thread  on SO.
Last but not least - thanks for looking into this Marten. If anyone else has any other ideas, please let me know.
Jul 19th, 2012, 11:09 AM
Please find the example attached
I have attached an example which shows how to autowire the beans. If followed in the way which has been depicted in the attachment, you problem might get resolved. Please let me know if you need more help on this.
Jul 19th, 2012, 11:16 AM
@kotak25kesava - with all the due respect I have to say I know how autowiring works, and I'm not sure how simple hello world example applies to the question I have asked.
Tags for this Thread