Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whether or not to use method injection?

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dependency injection can be done with setter methods. Not getter methods.

Mark
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic