Results 1 to 8 of 8

Thread: Dependency Injection, Singleton and nonSsingleton ?

  1. #1
    Join Date
    Jul 2007
    Location
    Stockholm
    Posts
    5

    Red face Dependency Injection, Singleton and nonSsingleton ?

    Hello,

    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. 3.4.4.5 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. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote 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.

    Quote 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 3.3.8.1.

    Quote 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.

    Jörg

  3. #3
    Join Date
    Jul 2007
    Location
    Stockholm
    Posts
    5

    Default Singleton and non-singleton (Prototype)

    Hello!

    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>

    <bean id="imageHandler"
    class="com.myown.ImageHandler"
    scope="prototype">

    </bean>
    <bean id="searchHandler"
    class="com.myown.SearchHandler"
    scope="prototype">

    </bean>
    <bean id="searchBuilder"
    class="com.myown.SearchBuilder"
    scope="prototype">
    </bean>

    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

    ps.

    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 {
    this.beanFactory=beanFactory;
    }

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

  4. #4
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    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. #5
    Join Date
    Jul 2007
    Location
    Stockholm
    Posts
    5

    Default

    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. #6
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    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 http://www.interface21.com/books/


    Regards,
    Oleksandr

  7. #7
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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 at 04:00 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  8. #8

    Default

    http://static.springframework.org/sp...thod-injection

    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.

    -Satish

Posting Permissions

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