Skip to content

Latest commit

History

31 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Injector

Injector Build Status

A Java dependency injection library supporting both static and dynamic injection

Static Field Injection

Properties file:

field1=some injection content
field2=other content

Injection code:

publicclassMain {
classInjectTo {
@ConfigProperty(property = "field1")
@InjectprivatestaticStringfield1;
@ConfigProperty@InjectprivatestaticStringfield2;
publicStringgetField1() {
returnfield1;
}
publicStringgetField2() {
returnfield2;
}
}
publicstaticvoidmain() {
InjectTosource = newInjectTo();
newStaticBindingFactory()
.setInjectionSource("resources/configuration.properties")
.requestInjection(InjectionGroup.of(InjectTo.class))
.build();
// Prints: some injection contentSystem.out.println(source.getField1());
// Prints: other contentSystem.out.println(source.getField2());
}
}

Dynamic Field Injection

Properties file:

field1=some injection content
field2=other content

Injection code:

publicclassMain {
classInjectTo {
@ConfigProperty(property = "field1")
@InjectprivateStringfield1;
@ConfigProperty@InjectprivateStringfield2;
publicStringgetField1() {
returnfield1;
}
publicStringgetField2() {
returnfield2;
}
}
publicstaticvoidmain() {
InjectTosource = newInjectTo();
InjectTootherSource = newInjectTo();
newDynamicBindingFactory()
.setInjectionSource("resources/configuration.properties")
.requestInjection(InjectionGroup.of(InjectTo.class, source))
.build();
// Prints: some injection contentSystem.out.println(source.getField1());
// Prints: other contentSystem.out.println(source.getField2());
// Prints: nullSystem.out.println(otherSource.getField1());
// Prints: nullSystem.out.println(otherSource.getField2());
}
}

Constructor Injection

Properties file:

config_string=some config string

Injection code:

publicclassTextEditor {
privateSpellCheckerspellChecker;
privateStringconfigString;
@InjectpublicTextEditor(@ConfigProperty(property="config_string") StringconfigString, SpellCheckerspellChecker) {
this.configString = configString;
this.spellChecker = spellChecker;
}
publicStringmakeSpellCheck() {
returnspellChecker.checkSpelling();
}
publicStringgetConfigString() {
returnconfigString;
}
}
publicclassTextEditorModuleextendsAbstractModule {
@Overridepublicvoidconfigure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
publicinterfaceSpellChecker {
voidcheckSpelling();
}
publicclassSpellCheckerImplimplementsSpellChecker {
@OverridepublicvoidcheckSpelling() {
return"Called checkSpelling()";
}
}
publicclassMain {
publicstaticvoidmain() {
Injectorinjector = Injector.createInjector(newTextEditorModule())
.setInjectionSource("resources/configuration.properties");
TextEditoreditor = injector.getInstance(TextEditor.class);
// Prints: Called checkSpelling()System.out.println(editor.makeSpellCheck());
// Prints: some config stringSystem.out.println(editor.getConfigString());
}
}

Method Injection

classTextEditor {
privateSpellCheckerspellChecker;
@InjectpublicTextEditor( SpellCheckerspellChecker) {
this.spellChecker = spellChecker;
}
publicvoidmakeSpellCheck(){
spellChecker.checkSpelling();
} }
classTextEditorModuleextendsAbstractModule {
@Overridepublicvoidconfigure() { bind(String.class)
.annotatedWith(Names.named("JDBC"))
.toInstance("jdbc:mysql://localhost:5326/emp");
} }
@ImplementedBy(SpellCheckerImpl.class)
interfaceSpellChecker {
publicvoidcheckSpelling();
}
classSpellCheckerImplimplementsSpellChecker {
privateStringdbUrl;
publicSpellCheckerImpl(){}
@InjectpublicvoidsetDbUrl(@Named("JDBC") StringdbUrl){
this.dbUrl = dbUrl;
}
@OverridepublicvoidcheckSpelling() { return"Called checkSpelling() with URL: " + dbUrl;
}
}
publicclassMain {
publicstaticvoidmain() {
Injectorinjector = Injector.createInjector(newTextEditorModule());
TextEditoreditor = injector.getInstance(TextEditor.class);
// Prints: Called checkSpelling() with URL: jdbc:mysql://localhost:5326/empSystem.out.println(editor.makeSpellCheck());
}
}

About

A Java dependency injection library supporting both static and dynamic injection. More or less a Guice clone.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Contributors

Languages