title: Implementing Dependency Inversion Principle using Maven and Spring
body: <p>As per this Wikipedia article: <a href="https://en.wikipedia.org/wiki/Dependency_inversion_principle#DIP_implementations" rel="nofollow noreferrer">Implementing Dependency Inversion Principle</a> can be done in two ways:</p>
  
  <ul>
  <li>Having an abstraction of a low level component in a separate package upon which both high level and low level components depend.</li>
  <li>Having the abstraction of the low level component reside in the same package of the high level component.</li>
  </ul>
  
  <p>The following illustration depicts the dependencies before and after DIP using the two approaches:</p>
  
  <p><strong><em>Before DIP:</em></strong>
  The repository resides in a separate maven module and has no interface, and the service has a direct dependency on the Repository implementation. 
  <a href="https://i.stack.imgur.com/mFeUq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mFeUq.png" alt="Dependencies before DIP"></a></p>
  
  <p><strong><em>Approach 1:</em></strong> An interface (abstraction) of the repository is introduced. The implementation of that interface is another module, and both the service and the repository implementation have direct dependency on the interface.
  <a href="https://i.stack.imgur.com/5nOBL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5nOBL.png" alt="DIP Implementation Approach 1"></a></p>
  
  <p><strong><em>Approach 2:</em></strong> In this approach, the interface resides in the same package of the service. The <a href="http://www.martinfowler.com/eaaCatalog/separatedInterfaceSketch.gif" rel="nofollow noreferrer">diagram</a> used by Fowler to describe <a href="http://www.martinfowler.com/eaaCatalog/separatedInterface.html" rel="nofollow noreferrer">Separated Interface</a> pattern seems to be also an example of this approach.
  <a href="https://i.stack.imgur.com/pbNgP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pbNgP.png" alt="DIP Implementation Approach 1"></a></p>
  
  <p>I've been following <strong><em>Approach1</em></strong> and because I used spring's JavaConfig, the services module had to have a ,maven dependency to both the infrastructure interfaces and implementation modules. Apart from my <code>@Configuration</code> file, there's absolutely no reference to any infrastructure concrete implementation.</p>
  
  <p>I'm currently considering switching to <strong><em>Approach2</em></strong>, but obviously it won't work with JavaConfig as I will end up having direct references in the code to the interface implementation module resulting in a cyclic dependency, something build tools like maven can't deal with.</p>
  
  <p>The question is how can configure spring and maven to achieve <strong><em>Approach2</em></strong>? Is there a way I can ask spring to <strong><em>scan for components</em></strong> that are not added as a maven dependency? Will that require changes in the way I'm using maven?</p>