This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes Spring dependency injection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring dependency injection" Watch "Spring dependency injection" New topic

Spring dependency injection

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi All,

I implemented a small spring dependency injection program and I have one question. Can spring dependency injection be achieved without using interfaces at all (Not having any interface in ya spring application)?? I think the answer is NO. Just wanted to know if I am right or wrong. what do you guys say?/


Be Humble... Be Nice.
Hussein Baghdadi
clojure forum advocate

Joined: Nov 08, 2003
Posts: 3476

Did you try it?
K Aditi
Ranch Hand

Joined: Mar 17, 2008
Posts: 89
Practically speaking, I do not think not using interfaces would hamper dependency injection in spring. I feel it is more of a design issue.

Christian Gossart
Ranch Hand

Joined: Mar 13, 2008
Posts: 34
Well, one of the most common use of DI is to easily switch from ClassBImpl1 to ClassBImpl2 ... to ClassBMock.
This relies on the fact that a common handler (let say 'interface' in Java) can be used to manipulate these different class B implementations.

Technically, you can do it without a common interface.
If your class A has a class B instance variable, defined like this :

you'll have however some difficulties to switch to a mock implementation just through configuration. You'll need to change your class A code to the following :

and ensure that this specific implementation provide the same contract that was offered by ClassBImpl1 (or change the corresponding code).

Using a ClassB interface allows you to change the implementation dependencies with no code modification, relying on the public contract.

Christian Gossart
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Perfect Christian. Yes I tried it John. Thanks for replying guys.
[ December 07, 2008: Message edited by: Arjun Reddy ]
Consider Paul's rocket mass heater.
subject: Spring dependency injection
Similar Threads
DI in Ruby/C#
Spring supports Dependency Injection thru PicoContainer ?
Is backing Bean Replacing Command Pattern?
Spring DAO
ejb 3.0 vs spring