aspose file tools*
The moose likes Spring and the fly likes not getting how exactly spring Depedency Injection works. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "not getting how exactly spring Depedency Injection works." Watch "not getting how exactly spring Depedency Injection works." New topic
Author

not getting how exactly spring Depedency Injection works.

prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
hi all,
i am new to spring and going through Pro Spring 2.5 book.
but after studying the dependency injection topic i got a confusion. here it goes.
suppose i have a Car object which depends on a Tyre object. Tyre object has one property called radiusOfTyre. so both the classes look like this.


now my question is that for different different Car object Tyre can be different. according to spring dependency injection we can inject Tyre object to Car object directly using following configuration :



in the above scenario if we get a Car bean then we will get a Tyre bean already injected but the Tyre bean will not have any value assigned for radiusOfTyre. then what is the use of injecting a bean which doesn't have value for its property?
if we initialize the value of radiusOfTyre in the xml file itself using setter injection then it will be hard coded value and whenever we get a Car bean we will get the same Tyre bean with same radius everytime which is not feasible.

i know my question is little long but i don't have any other way to explain it.

thanks,
praz
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

If radius of a Tire can be different then it isn't going to be a property that you want to set in your configuration. And for Car as a Singleton in configuration, you only get one car, so that car will have just one radius to its tires.

In 99.99% of the time objects that are domain objects will not be configured in a Spring configuration. But other classes that don't hold state will be perfect beans in xml.

And in your xml you posted you have an error. It should be


Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
hey mark,
thanks a lot for the reply.

Mark Spritzler wrote:If radius of a Tire can be different then it isn't going to be a property that you want to set in your configuration.


does the above line mean that using spring we should (or we can) configure only system properties like db detail etc. but injecting a Tyre bean without setting the radius property won't make any sense and if we are not injecting the dependency mean we cant use the DI part of Spring.

Mark Spritzler wrote: And for Car as a Singleton in configuration, you only get one car, so that car will have just one radius to its tires.

yes for this i agree but what if i configure the Car bean scope as 'prototype' then ill get different object every time so my question is valid at that time.

Mark Spritzler wrote: And in your xml you posted you have an error.

i am sorry for the error.

thanks,
pratz
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Well, to me your objects you have there are domain objects, and I don't configure domain objects in a Spring configuration. I will put my Service, Repository, Controller, Utility, Helper, TransactionManager, DataSource classes in my configuration where I want just one instance of those and they are stateless and don't hold state.

a Tire in your example is holding state the radius of that one tire and you might have many tires with different radiuses. Holding state then requires thread safety, which I won't have, so no, it wouldn't be an object I configure in Spring.

My Service needs some Repositories(DAO) and those classes need a DataSource to access the database, those are beans I define and inject as dependencies to each other.

Mark
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
hey mark,
nice to listen from you again.

thanks a lot for the reply. this has resolved a lot of confusions. with this ill go on with spring learning and will get back to you if anything else comes.

thanks,
pratz
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
Hey Mark,
how are you?

i have one more question for you. while reading about spring i went through one more property i.e. scope="prototype".

below is my question.

Mark Spritzler wrote:Well, to me your objects you have there are domain objects, and I don't configure domain objects in a Spring configuration. I will put my Service, Repository, Controller, Utility, Helper, TransactionManager, DataSource classes in my configuration where I want just one instance of those and they are stateless and don't hold state.


as you said you wont configure domain objects in your configuration files. if you configure only Service, Repository, Controller, Utility, Helper, TransactionManager, DataSource classes then i think you want these objects to be singleton. if you want them to be singleton then what is the use of scope="prototype" property? can you please give a real time example where you have used this property to create your bean non-singleton.

thanks,
Pratz
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
hi Mark,
can you please give your valuable comments on my question.
waiting to listen word from you.

thanks,
pratz
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Exactly. I don't have any beans that are scoped prototype. Just think of the definition of the word "prototype", seems like an interesting choice in words.

Mark
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
ok so do you mean to say that you will NEVER use scope="prototype" property in your bean configuration?

thanks,
pratz
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

prateek sharmaa wrote:ok so do you mean to say that you will NEVER use scope="prototype" property in your bean configuration?

thanks,
pratz


never is a harsh word, but yes I try to avoid using it unless absolutely necessary, and I have yet to come across a situation/use case where I needed that.

Mark
prateek sharmaa
Ranch Hand

Joined: May 15, 2010
Posts: 38
Mark Spritzler wrote:
prateek sharmaa wrote:ok so do you mean to say that you will NEVER use scope="prototype" property in your bean configuration?

thanks,
pratz


never is a harsh word, but yes I try to avoid using it unless absolutely necessary, and I have yet to come across a situation/use case where I needed that.

Mark


oh ok fine.

thanks,
pratz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not getting how exactly spring Depedency Injection works.