Results 1 to 7 of 7

Thread: @Autowired Spring singletons are initialized more than once, wired incorrectly

  1. #1
    Join Date
    Jul 2012
    Posts
    4

    Default @Autowired Spring singletons are initialized more than once, wired incorrectly

    Hi,

    This post is a spin-off from my SO post [1] 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)?

    1. http://stackoverflow.com/questions/1...of-a-singleton

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2012
    Posts
    4

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2012
    Posts
    4

    Default

    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 [1] on SO.

    Last but not least - thanks for looking into this Marten. If anyone else has any other ideas, please let me know.

    1. http://stackoverflow.com/a/7456501/7345

  6. #6

    Smile Please find the example attached

    Hi,

    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.

    Thanks
    Attached Files Attached Files

  7. #7
    Join Date
    Jul 2012
    Posts
    4

    Default

    @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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •