Announcement Announcement Module
Collapse
No announcement yet.
@Autowired Spring singletons are initialized more than once, wired incorrectly Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    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.

    Comment


    • #3
      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.

      Comment


      • #4
        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...

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment


            • #7
              @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.

              Comment

              Working...
              X