This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?/
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.
Joined: Nov 10, 2007
Perfect Christian. Yes I tried it John. Thanks for replying guys. [ December 07, 2008: Message edited by: Arjun Reddy ]