Announcement Announcement Module
No announcement yet.
Dependency Injection, Singleton and nonSsingleton ? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dependency Injection, Singleton and nonSsingleton ?


    Working with an application that is implemented in 2 different ways;
    using applicationContext-j2EE.xml + applicationContext-App1-j2ee.xml + applicationContext-App2-j2ee.xml.
    Using applicationContext-j2EE.xml as the 'parent' for the 2 different application, setting things that are common - such as the database-access and soforth.

    My problem is when I am using a singleton, a class that works as a manager, this class does not change during the session - I use this class to store my userProfile or userView if u like and some other stuff.
    Into this class I am injecting 3 other classes ( 2 of them extend an parent-class same for app1 and app2, 1 of them impl. a interface common for app1 and app2) which are depending on search-criteria I make, so they change every time my user fills in the search box and hits submit and his/hers search-results are visible.

    Here I am today:
    Making the first search with arguments; everything looks just great ....
    Making the second search with some other-arguments; everything looks as in the first search.
    So my injection is not working the way I thought it would be, everything is instantiated when jetty is brought up in the air and I cannot do a darn thing about it.

    Solution ?
    Should I be using 'method injection' here as described in chapter 3.3.8 and 3.3.81 in the reference ? or should I use 'scoped beans as dependencies' as described in ch. using <aop:scoped-proxy/> ?
    If I choose either way, is the injection to my 'manager'-class done in the same way, using setters ?

    Hopefully you have understood my little problem here, the description in the reference is quite meager and the book 'pro spring' from apress does not say much about this. does any of the examples from spring go into this ?

    best regards, trott

  • #2
    Originally posted by trott View Post
    Should I be using 'method injection' [..] or should I use 'scoped beans as dependencies' [..]?
    Not to your particular problem, but ... scoped bean proxies is probably the easier and more obvious solution. I only switched to method injection in one case where the injected bean needs to be serializable - the proxies are not.

    Originally posted by trott View Post
    If I choose either way, is the injection to my 'manager'-class done in the same way, using setters ?
    No, there is a reason why it is actually called "lookup method injection". Actually it is a getter which is replaced by Spring. See the sample in the reference section

    Originally posted by trott View Post
    Hopefully you have understood my little problem here
    Unfortunately it was too confusing/ complex. At least for 1 AM If the above does not help, providing some stripped down code would help to understand.



    • #3
      Singleton and non-singleton (Prototype)


      Could simplify the problem.
      We can say that I have only one applicationContext-j2ee.xml.

      Here I have the following bean:
      <bean id="image" class="com.myown.ImageImpl">
      <property name="imageHandler" ref="imageHandler" />
      <property name="searchHandler" ref="searchHandler" />
      <property name="searchBuilder" ref="searchBuilder" />


      <bean id="imageHandler"

      <bean id="searchHandler"

      <bean id="searchBuilder"
      Where 'image' is a Singleton, I only need one instance in my app.
      But its dependencies; 'imageHandler','searchHandler' and 'searchBuilder' are prototypes, they should be instanced everytime the user uses the class 'image'.In the class 'com.myown.ImageImpl' I have created setters for the dependencies, but these dependencies are only created once - so I am always stuck with my first search-criteria; let say if I search the first time with the keyword 'spring' and I get 10 results related to the spring-context, the second time I search and use the keyword 'dog' I get the same 10 results as the first time related to the spring-context and soforth.

      Did put in the scope as prototype ( see thread by Xaeryan with the title 'Setter injection only happens once?' ) for my dependencies, but nothing happens.
      If I skip IoC and code with :
      ' com.myown.ImageHandle imageHandler = new com.myown.ImageHandler () '.
      within my com.myown.ImageImpl class then everything works just fine, I search for 'spring' and get a related searchresult and I search for 'dog' and get related searchresult.

      So my problem is that the dependencies only happens once.

      regards, Trott


      I did fix this by using the interface BeanFactoryAware.
      And private SearchHandler createSearchHandler() {
      return (SearchHandler)this.beanFactory.getBean("searchHan dler") ;

      public void setBeanFactory(BeanFactory beanFactory) throws BeansException {

      But is there another way solving this ?
      Last edited by trott; Jul 20th, 2007, 06:59 AM. Reason: -Solved this one


      • #4
        Sure, those dependecies are created once only. And it shall be so.
        All injection occurs only during object instantiation and not during followind object usage. Really, Spring does not control those usage in any way and nver mutate already instantiated object. If you need such mutation you need to do it progrmmmatically, i.e. as if Spring does not exists.

        Moreover, imagine that dependencies of the singleton will be mutated each time when you use this object (BTW, what is "use" in this ontext - just call of some object's methods or fetching of this object from context? Llatter you normally shall not do). And there are 2 threads that "use" that object. Then assume that dependecy injection occurs for first property from first thread, then for first property from second thread, then for second property from first thred, then from second property from second thread. You finish with object in the inconsistent state and your searches likely would bring shit.


        • #5
          Hello al0,

          thanks for your reply.
          Do you recommend any further reading, so that I can catch on - beeing able to fully understand the theory.Cause I am writing an application with 2 branches, if it is ok to use that term here, and the branches are much alike except for they seach in different datasources and have a bit different business logic. So I have the same base-classes for the apps, then I inject different classes ( as in fetchImageApp1 and fetchImageApp2 ) where they are needed in my manager and soforth.

          regards, T


          • #6
            Sorry, I have not understand your question, so would give just general reading suggestion.
            1. Rod Johnson "J2EE Desing and Development" and
            2. Rod Johnson and Juergen Hoeller "J2EE without EJB"
            3. Spring Reference
            4. Craig Walls and Ryan Breidenbach "Spring in Action"
            5. Or just look here



            • #7
              Along the same lines, I'd have a read through the meta forum as there are lots of useful links on there.
              Last edited by karldmoore; Aug 27th, 2007, 05:00 PM.


              • #8

                Take a look at the above ref from spring documentation. It talks about the case of a singleton bean having dependencies on prototype beans and the spring way of solving it.

                Short answer is the way you have wired up your beans will always cause the first instance of your prototype to be set into you singleton , and since the top level is a singleton , no more prototyping of the dependent beans happens afterwards.

                So either you change your code to use look-up method injection or another abstraction inbetween which guarantees a prototype (using a ServiceLocatorFactoryBean) or make your image factory prototype.