Simplest lightly dependency injection library for java ever! Processing @Inject and optionally @Singleton annotations from JSR-330 Minimal supported java version: 1.5
repositories {
jcenter() // available in bintray
// or
mavenCentral() // also available in maven central
}
dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
<dependencies>
<dependency>
<groupId>com.github.daggerok</groupId>
<artifactId>daggerok-context</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
<!-- only for bintray use case: -->
<repositories>
<repository>
<id>jcentral</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
@Singleton
public class MyRepository {
public String repositoryMethod() {
return "MyRepository.repositoryMethod";
}
}
public class MyClient {
private final HashMap<String, Object> config;
@Inject
public MyClient(HashMap<String, Object> config) {
this.config = config;
}
public String clientMethod() {
return "MyClient.clientMethod" + config.size();
}
}
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();
}
}
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"));
}
}
-
DaggerokContext#create()
-
DaggerokContext#create(Class…)
-
DaggerokContext#create(Package…)
-
DaggerokContext#create(String…)
// 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());
-
DaggerokContext#withBasePackageClasses(Class…)
-
DaggerokContext#withBasePackageNames(String…)
-
DaggerokContext#withBasePackages(Package…)
-
DaggerokContext#withComponents(Annotation)
-
DaggerokContext#withInjectors(Annotation)
-
DaggerokContext#failOnInjectNullRef(boolean)
-
DaggerokContext#failOnBeanCreationError(boolean)
-
DaggerokContext#failOnUnknownReflectionsErrors(boolean)
final DaggerokContext applicationContext = DaggerokContext.create();
// ...
applicationContext.withBasePackageNames("my.app");
applicationContext.withBasePackageClasses(my.app.Config);
applicationContext.withBasePackages(Package.getPackage("my.other.app.pkg"));
applicationContext.withComponents(Singleton.class);
applicationContext.withInjectors(Inject.class);
applicationContext.failOnInjectNullRef(false);
applicationContext.failOnBeanCreationError(false);
applicationContext.failOnUnknownReflectionsErrors(false);
-
DaggerokContext#register(String, Object)
-
DaggerokContext#register(Class, Object)
// 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!");
-
DaggerokContext#initialize()
DaggerokContext.create("")
.initialize();
DaggerokContext applicationContext = DaggerokContext.create(String.class)
.failOnInjectNullRef(true)
.register(String.class, "Hello, World!")
.initialize();
System.out.println(applicationContext.getBean(String.class));
-
DaggerokContext#getBean(Class)
-
DaggerokContext#getBean(String, Class)
-
DaggerokContext#getBean(String)
// get bean by class
MyRepository myRepository = applicationContext.getBean(MyRepository.class);
Map<String, String> map = applicationContext.getBean(Map.class);
String string = applicationContext.getBean(String.class);
// get named beans
Map<String, String> map = applicationContext.getBean("java.util.Map", Map.class);
HashMap<String, String> myOtherMap = applicationContext.getBean("myOtherMap", HashMap.class);
// get named beans (unchecked)
Map<String, String> map = applicationContext.getBean("java.util.Map");
HashMap<String, String> myOtherMap = applicationContext.getBean("myOtherMap");
String string = applicationContext.getBean("java.lan.String");
String oneMoreString = applicationContext.getBean("oneMoreString");
-
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!
-
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 …unless you found a bug :)
repositories {
maven { url "https://dl.bintray.com/daggerok/daggerok" }
}
dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile "com.github.daggerok:daggerok-context:1.0.4"
}
<repositories>
<repository>
<id>bintray-daggerok-daggerok</id>
<url>https://dl.bintray.com/daggerok/daggerok</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.daggerok</groupId>
<artifactId>daggerok-context</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.daggerok</groupId>
<artifactId>daggerok-context</artifactId>
<version>1.0.4</version>
</dependency>
</dependencies>
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 :))