A Java dependency injection library supporting both static and dynamic injection
Properties file:
field1=some injection content
field2=other contentInjection 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());
}
}Properties file:
field1=some injection content
field2=other contentInjection 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());
}
}Properties file:
config_string=some config stringInjection 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());
}
}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());
}
}