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:

  • EasyDI Example

    | tagged as
    Summary:

    This tutorial will show you the usage of Easy-DI. All files for this example can be found in the github-project in the package /src/test/java/eu/lestard/easydi/examples/coffee Let's assume this class diagram for a coffee machine application: uml-class-diagram In this class diagram we can see the dependencies of each class: The CoffeeMachine has a reference to the WaterTank and the CoffeePowderProvider. The CoffeePowderProvider has a reference to the BeanContainer and to the Mill.
    ...

  • Dependency Injection in less then 50 lines of code

    | tagged as
    Summary:

    I'm a big fan of the Dependency Injection design pattern and I'm using it a lot. I'm saying "design pattern" because when I'm thinking and talking about "DI" I don't think about frameworks, big containers and a lot of magic. Instead, for me DI is a simple concept of how you write your classes and put them together. In the past I've used many DI frameworks like Guice, CDI/Weld, EJB and Spring. In other use cases (for example my SnakeFX game) I've done the DI by hand: Simply put all dependencies of a class into the constructor of this class. In the main method you can now start to instantiate the classes you need starting by the ones that have no dependencies and can therefore be instantiated without constructor parameters needed. It's easy and this way you can't create cyclic dependencies by accident. Look at an old version of the DependencyInjector class of SnakeFX to see what I mean. Learning how to do the DI by hand was a real eye-opener for me. You can learn that DI is no magic at all and you will lose all your fear from the big buzzword "Dependency Injection". It's nothing special. Simply a design pattern.
    ...