• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Conditional dependency injection

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read dependency injection in one of the artical about introduction to Spring. My understanding is that the object references are given when an object is created. If an object references are needed conditionally, how do object references are given?
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dependencies will be injected only when the instance is obtained from within the container (Spring Beanfactory for eg in this case). If you do a 'new' instead, then its just bare bones java. You will see what you coded in. Is this what you were referring to.
 
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the dependency decalartion in Spring framework below copied from an article from www.theserverside.com

<beans>
<bean id="AirlineAgency" class="com.dnene.ditutorial.common.impl.SimpleAirlineAgency" singleton="true"/>
<bean id="CabAgency" class="com.dnene.ditutorial.common.impl.SetterBasedCabAgency" singleton="true">
<property name="airlineAgency">
<ref bean="AirlineAgency"/>
</property>
</bean>
<bean id="TripPlanner" class="com.dnene.ditutorial.common.impl.SetterBasedTripPlanner" singleton="true">
<property name="airlineAgency">
<ref bean="AirlineAgency"/>
</property>
<property name="cabAgency">
<ref bean="CabAgency"/>
</property>
</bean>
</beans>



Vu, In the example above , are you referring to case where cabagency may not always refer to airlineagency bean as conditional dependency ? If it is the case can't we have another declaration as above without the ref tag inside the cabagency tag ? This will mean that there will multiple instances of cabagency , one with ref to airline agency and another without refernce to airline agency?

Karthick , since you have worked with Spring framework do you think this is possible.

Hope I am not and also not confusing others.

I feel that there is too much hype surrounding dependency injection. :roll:
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pradeep,

Guess the thread will be moved to Application Frameworks section soon.

Originally posted by Pradip Bhat:


Vu, In the example above , are you referring to case where cabagency may not always refer to airlineagency bean as conditional dependency ? If it is the case can't we have another declaration as above without the ref tag inside the cabagency tag ? This will mean that there will multiple instances of cabagency , one with ref to airline agency and another without refernce to airline agency?



Thats correct. If you have different 'id's for the beans, they will be distinct instances even though they are of the same type. singleton="true" (default setting), just tells the container to return the same instance for the given bean id.

 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Karthik. I see a problem with DI, suppose I want to replace the current airlineAgency for the Cabagency with another one after lines of execution in my code , how do I do that with spring or any other DI framework.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pradip Bhat:
Thanks Karthik. I see a problem with DI, suppose I want to replace the current airlineAgency for the Cabagency with another one after lines of execution in my code , how do I do that with spring or any other DI framework.



Only you know about the type of airline agency you want at that point of execution, the container does'nt. But ,Well, i think i s'd point out that you can get Spring to get the actual beans instance from your custom factory class by invoking the specified factory method . So if *you* can somehow pass the context information to *your* factory class , Spring can wire the dependency for you
 
Pradeep bhatt
Ranch Hand
Posts: 8944
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks karthik
reply
    Bookmark Topic Watch Topic
  • New Topic