Skip to content

eis/minimal-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minimal DI

Build Status Coverage Status Maven Central Codebeat Analysis

This is a minimal dependency injection framework.

Reasoning

This was created out of need for simple DI. Guice weighs 600kt, Weld CDI 3M, and Spring even more. Even PicoContainer weighs 317kt. That's not what I needed - I needed a very simple DI system that would just implement the basic features and nothing more, with few simple classes. This is it.

"Minimal DI" currently weighs 23k as a .jar, which is simple enough. It doesn't need any additional dependencies which was one of its main goals.

Usage

Inject your dependencies to constructors:

import fi.eis.libraries.di.Inject;

public class CurrentApp {
    private MyDependency dependency;
    @Inject
    public CurrentApp(MyDependency dependency) {
        this.dependency = dependency;
    }
}

Or directly to your private variables:

import fi.eis.libraries.di.Inject;

public class CurrentApp {
    @Inject
    private MyDependency dependency;
}

and fire up the system using either all classes in the deployment unit:

public static void main(String args[]) {
    Context diContext = DependencyInjection.deploymentUnitContext(CurrentApp.class);
    CurrentApp app = diContext.get(CurrentApp.class);
    app.run();
}

or listing all implementation classes part of DI:

public static void main(String args[]) {
    Module module = DependencyInjection.classes(CurrentApp.class, ClassImplementingDependency.class);
    Context diContext = DependencyInjection.context(module);
    CurrentApp app = diContext.get(CurrentApp.class);
    app.run();
}

or use a Spring-style javaconfig class (version 1.1):

public static void main(String args[]) {
    Context diContext = DependencyInjection.configurationClasses(ExampleJavaConfig.class);
    CurrentApp app = diContext.get(CurrentApp.class);
    app.run();
}

That's it.

Maven dependency

This library can be used from Maven Central:

<dependency>
  <groupId>com.github.eis.libraries</groupId>
  <artifactId>minimal-di</artifactId>
  <version>1.1</version>
</dependency>