Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ljacqu/dependencyinjector

Lightweight dependency injector
https://github.com/ljacqu/dependencyinjector

application-initializer dependency dependency-injection injection injector ioc ioc-container java

Last synced: about 2 months ago
JSON representation

Lightweight dependency injector

Awesome Lists containing this project

README

        

## Lightweight Dependency Injector
[![Build Status](https://travis-ci.org/ljacqu/DependencyInjector.svg?branch=master)](https://travis-ci.org/ljacqu/DependencyInjector)
[![Coverage Status](https://coveralls.io/repos/github/ljacqu/DependencyInjector/badge.svg?branch=master)](https://coveralls.io/github/ljacqu/DependencyInjector?branch=master)
[![Javadocs](http://www.javadoc.io/badge/ch.jalu/injector.svg)](http://www.javadoc.io/doc/ch.jalu/injector)
[![Code Climate](https://codeclimate.com/github/ljacqu/DependencyInjector/badges/gpa.svg)](https://codeclimate.com/github/ljacqu/DependencyInjector)

Simple but customizable dependency injector for Java 1.8 and above.

### Why use it
- Very lightweight (only has `javax.inject` and `javax.annotation-api` as dependency)
- Allows gradual transition to injection for existing projects
- You can implement your own injection methods and behaviors
- Support for projects with optional dependencies

### Integrating it
Using Maven, you can get the injector by adding this to your pom.xml:

```xml

ch.jalu
injector
1.0

```

### Simple example
By default, the injector supports **constructor injection** and **field injection**.
Consider the following class skeletons:

```java
public class Settings {
// regular class
}

public class Messages {
private File messagesFile;

@Inject
Messages(Settings settings) {
messagesFile = new File(settings.getLanguage() + ".txt");
}
}

public class CalculationService {
@Inject
private Messages messages;

@Inject
private RoundingService roundingService;
}

public class RoundingService {
private int precision;

@Inject
RoundingService(Settings settings) {
precision = settings.getPrecision();
}
}
```

At the startup of the application, we might only care about getting an instance of `CalculationService`. All other
classes are required by it to run, but we don't immediately care about them. With the injector, we don't have to
deal with those classes and can just retrieve what we actually want:

```java
public class MyApp {

public static void main(String... args) {
Injector injector = new InjectorBuilder().addDefaultHandlers("com.example.my.project").create();
CalculationService calcService = injector.getSingleton(CalculationService.class);
calcService.performCalculation();
}
}
```

... That's all! No need to deal with creating any other classes, but you still have a setup that allows you to easily
unit test or switch a component.

--> Full, runnable example can be found [here](https://github.com/ljacqu/DependencyInjector/tree/master/injector/src/test/java/ch/jalu/injector/demo).

### Handlers
You may implement your own logic to instantiate classes and resolve dependencies. This allows you, for example, to
implement specific behavior for custom annotations.
Read more on the Wiki: [Handlers explained](https://github.com/ljacqu/DependencyInjector/wiki/Handlers)