my dog learned polymorphism*
The moose likes Spring and the fly likes Whether or not to use method injection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Whether or not to use method injection?" Watch "Whether or not to use method injection?" New topic
Author

Whether or not to use method injection?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, I am learning spring 3.x. In method injection section they mentioned its usage and said that method injection should not be used because it causes a dependency on spring. Should we use it or not? If no, then is there any other alternative?

Thank you.


Love all, trust a few, do wrong to none.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

chaitanya karthikk wrote:Hi all, I am learning spring 3.x. In method injection section they mentioned its usage and said that method injection should not be used because it causes a dependency on spring. Should we use it or not? If no, then is there any other alternative?

Thank you.


You have to show context. I think you are taking it out of context and it makes no sense. How would it cause a dependency on Spring. Who says? What exactly are you pointing to. Where in the documentation does it say that.

Spring is a Dependency Injection container at its heart, where you do DI with either constructors, methods or instance variables, all are good choices and not one of them ties you to Spring.

Mark


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

Joined: Sep 15, 2009
Posts: 800

Mark Spritzler wrote:
You have to show context. I think you are taking it out of context and it makes no sense. How would it cause a dependency on Spring. Who says? What exactly are you pointing to. Where in the documentation does it say that.

Spring is a Dependency Injection container at its heart, where you do DI with either constructors, methods or instance variables, all are good choices and not one of them ties you to Spring.

Mark


While using method injection we have to make method call to getBean(bean) which causes the dependency. In the documentation they also mentioned that our code should not contain a method call getBean(bean) at all. However I don't know the alternate approach for this method call because I am still reading bean scopes. Please refer this section.

Thank you.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

There is another approach called Lookup method injection which actually decouples the dependency.

But there is not complete example given in the site.

Thank you.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Dependency injection can be done with setter methods. Not getter methods.

Mark
 
wood burning stoves
 
subject: Whether or not to use method injection?
 
Similar Threads
BeanInstantiationException
Are spring beans thread-safe?
Pease Point Me a Direction to Run Web Services Using the Spring Framework
Why method injection?
Building Effective Service layer using Spring+Hibernate