Easy-DI

A Dependency-Injection library for small Java projects. The goal is to provide a dependency injection mechanism with as few configuration needed as possible.

The source code can be found on github: https://github.com/lestard/EasyDI

The code to use EasyDI looks like this:

// create an instance of EasyDI
EasyDI easyDI = new EasyDI();

// get an instance of your class (Car in this example)
Car myCar = easyDI.getInstance(Car.class);

// do something useful with your instance.
myCar.drive();

Other Features

Detection of cyclic dependencies

EasyDI will find out cyclic dependencies in your classes. For example when a class A has a dependency to class B but B has also a dependency to A you have a cyclic dependency. EasyDI won’t instantiate these classes but show you an error message. This can be seen as a feature because cyclic dependencies are a sign of bad code. But of cause you can see this as a limitation too.

Singleton

You can mark classes as Singleton. EasyDI will then only create one instance of this class and reuse this instance for every request.

Classes can be marked as Singleton either with the annotation javax.inject.Singleton on the class or with the EasyDI.markAsSingleton(ThirdParty.class) method. This can be useful when you can’t modify the source code of the class that should be a singleton.

Providers

You can register a provider for a given class type that creates an instance of the given type. This can be useful if you want to make some configuration to the created instance before it is used as dependency. Another usecase is to integrate third-party libraries that only provide their components only by factory methods or as a classical Singleton.

Limitations

EasyDI has the following limitations:

  • Java 8 only
  • Only Constructor-Injection, no Setter/Field injection
  • No cyclic dependencies
  • Uses some JSR-330 annotations (javax.inject.*) but is not a compliant implementation of JSR-330 (f.e. there are no Modules in EasyDI)

Related Articles: