Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/daggerok/daggerok-context

Simplest dependency injection library ever! Small and fast, supports java 1.5+, processing @Inject (and optionally @Singleton) annotation, JSR-330
https://github.com/daggerok/daggerok-context

application-configuration application-context bean beans cdi dependency-injection dependency-injection-container inject inversion-of-control ioc singleton

Last synced: 4 days ago
JSON representation

Simplest dependency injection library ever! Small and fast, supports java 1.5+, processing @Inject (and optionally @Singleton) annotation, JSR-330

Awesome Lists containing this project

README

        

= daggerok-context java 1.5+
:toc:
:toc-placement!:

//tag::content[]
image:https://travis-ci.org/daggerok/daggerok-context.svg?branch=master["Build Status", link="https://travis-ci.org/daggerok/daggerok-context"]
image:https://api.bintray.com/packages/daggerok/daggerok/daggerok-context/images/download.svg[link="https://bintray.com/bintray/jcenter?filterByPkgName=daggerok-context"]
image:https://jitpack.io/v/daggerok/daggerok-context.svg["JitPack", link="https://jitpack.io/#daggerok/daggerok-context"]
image:https://maven-badges.herokuapp.com/maven-central/com.github.daggerok/daggerok-context/badge.svg?style=plastic["Maven Central", link="https://maven-badges.herokuapp.com/maven-central/com.github.daggerok/daggerok-context"]

toc::[]

== description
Simplest lightly dependency injection library for java ever!
Processing @Inject and optionally @Singleton annotations from JSR-330
Minimal supported java version: 1.5

== installation

image:https://www.bintray.com/docs/images/bintray_badge_color.png["daggerok-context", link="https://bintray.com/daggerok/daggerok/daggerok-context?source=watch"]

.build.gradle
[source,gradle]
----
repositories {
jcenter() // available in bintray
// or
mavenCentral() // also available in maven central
}

dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
----

//image:http://maven.apache.org/images/maven-logo-black-on-white.png["daggerok-context", link="https://maven-badges.herokuapp.com/maven-central/com.github.daggerok/daggerok-context"]
//image:http://maven.apache.org/images/maven-logo-black-on-white.png["daggerok-context", link="https://search.maven.org/beta/artifact/com.github.daggerok/daggerok-context/1.0.4/jar"]
image:http://maven.apache.org/images/maven-logo-black-on-white.png["daggerok-context", link="https://search.maven.org/artifact/com.github.daggerok/daggerok-context/1.0.4/jar"]

.pom.xml
[source,xml]
----


com.github.daggerok
daggerok-context
1.0.4


jcentral
https://jcenter.bintray.com

----

== usage
.MyRepostory.java - let's say we have repository component (annotation @Singleton is optional)
[source,java]
----
@Singleton
public class MyRepository {

public String repositoryMethod() {
return "MyRepository.repositoryMethod";
}
}
----

.MyClient.java - we also have another component with auto injectior, i.e: creates new empty HashMap automatically if it's not exists using default constructor
[source,java]
----
public class MyClient {

private final HashMap config;

@Inject
public MyClient(HashMap config) {
this.config = config;
}

public String clientMethod() {
return "MyClient.clientMethod" + config.size();
}
}
----

.MyService.java - lastly, we have service with two injectors MyRepository / MyClient Annotation @Inject is required. Class can have only one constructor with all injectors configuration
[source,java]
----
public class MyService {

private final MyClient myClient;
private final MyRepository myRepository;

@Inject
public MyService(MyClient myClient, MyRepository myRepository) {
this.myClient = myClient;
this.myRepository = myRepository;
}

public String serviceMethod() {
return myClient.clientMethod() + myRepository.repositoryMethod();
}
}
----

.MyAppTest.class - and finally test application
[source,java]
----
public class MyAppTest {

@Test
public void test() {

DaggerokContext applicationContext = DaggerokContext.create(MyAppTest.class)
.initialize();
/*
initialize() method will do:

1. scan everithyng in base package of MyAppTest
2. create MyRepository instance in applicationContext
3. using default constructor create HashMap instance in applicationContext
4. inject HashMap and create MyClient instance in applicationContext
5. inject MyRepository and MyClient and create MyService instance in applicationContext
*/

MyService myService = applicationContext.getBean(MyService.class);
String actual = myService.serviceMethod();

assertTrue(actual.contains("MyClient.clinetMethod"));
assertTrue(actual.contains("MyRepository.repositoryMethod"));
assertTrue(actual.contains("0"));

HashMap config = applicationContext.getBean(HashMap.class);
config.put("message", "hello");
assertTrue(myService.serviceMethod().contains("1"));
}
}
----

== public API overview
=== Entry point: create uninitialized context using:
. `DaggerokContext#create()`
. `DaggerokContext#create(Class...)`
. `DaggerokContext#create(Package...)`
. `DaggerokContext#create(String...)`

.many ways create context
[source,java]
----
// empty context with single DaggerokContext bean registered:
DaggerokContext.create();

// by base class:
DaggerokContext.create(MyApp.class);

// by base packages:
DaggerokContext.create(MyApp.class.getPackage(), Package.getPackages());

// create context by packages:
DaggerokContext.create("my.app", "my.other.app");

// we are not recommend create context from empty package, but it's possible :)
DaggerokContext.create("");

// we also do not recommend create context for all packages in classpath, and yes, it's possible too :)
DaggerokContext.create(Package.getPackages());
----

=== User configurations:
. `DaggerokContext#withBasePackageClasses(Class...)`
. `DaggerokContext#withBasePackageNames(String...)`
. `DaggerokContext#withBasePackages(Package...)`
. `DaggerokContext#withComponents(Annotation)`
. `DaggerokContext#withInjectors(Annotation)`
. `DaggerokContext#failOnInjectNullRef(boolean)`
. `DaggerokContext#failOnBeanCreationError(boolean)`
. `DaggerokContext#failOnUnknownReflectionsErrors(boolean)`

.create simple (empty) context and add base packages configurations for scan
[source,java]
----
final DaggerokContext applicationContext = DaggerokContext.create();
// ...
applicationContext.withBasePackageNames("my.app");
applicationContext.withBasePackageClasses(my.app.Config);
applicationContext.withBasePackages(Package.getPackage("my.other.app.pkg"));
----

.set custom component annotation
[source,java]
----
applicationContext.withComponents(Singleton.class);
----

.set custom injector annotation
[source,java]
----
applicationContext.withInjectors(Inject.class);
----

.fail on inject null bean
[source,java]
----
applicationContext.failOnInjectNullRef(false);
----

.fail on bean creation error Class.newInstance()
[source,java]
----
applicationContext.failOnBeanCreationError(false);
----

.fail on unknown Reflections library errors
[source,java]
----
applicationContext.failOnUnknownReflectionsErrors(false);
----

=== Manual beans registration:
. `DaggerokContext#register(String, Object)`
. `DaggerokContext#register(Class, Object)`

.manually bean register
[source,java]
----
// by class:
applicationContext.register(MyRepostory.class, new MyRepository())
.register("java.util.Map", singletonMap("hello", "world"))
.register(String.class, "Hello, World!");

// by name:
applicationContext.register("my.app.MyBean", new MyBean("custom bean initialization..."))
.register("java.lang.String", "Hey, y0!");
----

=== Search, create and inject everything we can:
. `DaggerokContext#initialize()`

.minimal required configuration
[source,java]
----
DaggerokContext.create("")
.initialize();
----

.other possible configuration
[source,java]
----
DaggerokContext applicationContext = DaggerokContext.create(String.class)
.failOnInjectNullRef(true)
.register(String.class, "Hello, World!")
.initialize();

System.out.println(applicationContext.getBean(String.class));
----

=== Get bean from context - could be used before initialize() if bean was previously manually added:
. `DaggerokContext#getBean(Class)`
. `DaggerokContext#getBean(String, Class)`
. `DaggerokContext#getBean(String)`

.build application context
[source,java]
----
// get bean by class
MyRepository myRepository = applicationContext.getBean(MyRepository.class);
Map map = applicationContext.getBean(Map.class);
String string = applicationContext.getBean(String.class);

// get named beans
Map map = applicationContext.getBean("java.util.Map", Map.class);
HashMap myOtherMap = applicationContext.getBean("myOtherMap", HashMap.class);

// get named beans (unchecked)
Map map = applicationContext.getBean("java.util.Map");
HashMap myOtherMap = applicationContext.getBean("myOtherMap");
String string = applicationContext.getBean("java.lan.String");
String oneMoreString = applicationContext.getBean("oneMoreString");
----

== why?

* no more magic!
* no more xml!
* no more weight dependencies!
* no more evil field injections!
* no more abstract modules!
* no more plugins configurations!
* no more annotation processing configurations!
* no more custom annotations clones! use standards, use JSR-330!
* no more specific build configurations! single dependency only!

=== it's really simple

* JSR-330: supports only @Inject
* all class-based registration creates singletons
* supports custom named beans registration

It's simple. Simple means fast, less bugs, more fun. It's doing one thing and doing it well
link:https://github.com/daggerok/daggerok-context/issues[...unless you found a bug :)]

== other installation variants
=== gradle bintray.daggerok
.gradle setup (build.gradle)
[source,gradle]
----
repositories {
maven { url "https://dl.bintray.com/daggerok/daggerok" }
}

dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
----
=== gradle jitpack
.gradle setup (build.gradle)
[source,gradle]
----
repositories {
maven { url "https://jitpack.io" }
}

dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
----
=== maven bintray/daggerok
.maven setup (pom.xml)
[source,xml]
----


bintray-daggerok-daggerok
https://dl.bintray.com/daggerok/daggerok


com.github.daggerok
daggerok-context
1.0.4

----
=== maven jitpack
.maven setup (pom.xml)
[source,xml]
----


jitpack.io
https://jitpack.io


com.github.daggerok
daggerok-context
1.0.4

----

== TODO
* short public API description with examples or documentation
* publish to mavenCentral

== contribution
Feel free extend and contribute to add more functionality like Named Qualifier.
Personally I'd like to keep it simple as possible.
On really big projects therese days you probably would like to use something like
Guice, Dagger, CDI from JavaEE or Spring from spring-boot, or maybe even PicoContainer, who knows :))

* link:./VERSIONS.adoc[versions]
* link:./BINTRAY.adoc[bintray]
* link:./JITPACK.adoc[JitPack]
//end::content[]