Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ensure that a given dependency is included in my app with the Spring Framework?

This is going to be a tough question to describe, but here goes.

We are using the Delphi Spring Framework. (http://code.google.com/p/delphi-spring-framework/)

Let's say I have UnitA that declares InterfaceA which is implemented by ClassA.

Similarly, I have UnitB that declares InterfaceB which is implemented by ClassB.

Both registered their interface and their class with the Spring Container in their respective initialization sections.

InterfaceA has a dependency on InterfaceB, but because we are using Spring, UnitA doesn't have UnitB in its uses clause. In other words, we've done our job -- we've decoupled UnitA and UnitB, but we still are able to have InterfaceA depend on InterfaceB.

However, given the above scenario, we need to make sure that both UnitA and UnitB are included in the project so that the dependencies can be resolved.

Imagine, now, that we start a new project. That new project uses UnitA, but the developer doesn't realize that if one is to use UnitA, one also has to include UnitB in the project. There will be no compiler error, because the dependency is resolved at run time, not compile time.

And herein lies the question: What is the right way to ensure that this dependency on UnitB is known before the app gets deployed?

We can foresee a situation in a complex app where, despite thorough testing, a given code path isn't executed possibly for a long time, and this missing dependency isn't discovered before deployment.

We've implemented a system where each interface resolution call is accompanied by a Requires call that checks and raises an exception at startup, ensuring we'll see the error. But we are wondering if there is a "best practice" or standard way to detect this or otherwise deal with this issue.

Added: Is this an issue in Java and other languages?

like image 931
Nick Hodges Avatar asked Aug 31 '11 12:08

Nick Hodges


People also ask

How is dependency injection achieved within the Spring Framework?

The Spring-Core module is responsible for injecting dependencies through either Constructor or Setter methods. The design principle of Inversion of Control emphasizes keeping the Java classes independent of each other and the container frees them from object creation and maintenance.

Which spring-boot feature include all the dependent libraries you need?

The starters are basically a set of dependency descriptors tagged under a single banner, called starter name, such as spring-boot-starter-web. This starter includes all the dependent libraries required for developing a Spring Web application.

Which package in Spring Framework architecture provides the dependency injection feature?

Core container Core (spring-core) is the core of the framework that power features such as Inversion of Control and dependency injection.


1 Answers

You need to use a dependency management solution like Maven or Ivy. Once you do this, you will be able to say that UnitA depends on UnitB and once someone adds the UnitA as a dependency, the tool (either Maven or Ivy) will be forced to download the dependency and include it in your project.

Maven itself has an Eclipse plugin that is able to detect even if you already have the other project at your current workspace.

like image 144
Maurício Linhares Avatar answered Sep 17 '22 22:09

Maurício Linhares