File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 629
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: 3479

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: 629
Perfect Christian. Yes I tried it John. Thanks for replying guys.
[ December 07, 2008: Message edited by: Arjun Reddy ]
I agree. Here's the link:
subject: Spring dependency injection
It's not a secret anymore!