Skip to content

Commit

Permalink
#20 Create example with field of optional dependency type
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Jul 16, 2016
1 parent a2a6699 commit b694c9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test-module/src/main/java/ch/jalu/testmodule/ClassWithField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch.jalu.testmodule;

import javax.validation.Validator;

/**
* Sample class with a field from a non-provided dependency.
*/
public class ClassWithField {

private Validator validator;

public ClassWithField() {
}

public String getName(ClassWithMethodParam cwmp) {
return cwmp.getName();
}

}
8 changes: 8 additions & 0 deletions test-module/src/main/java/ch/jalu/testmodule/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@ public static void main(String... args) {
// all good
}

// Create injector
Injector injector = new InjectorBuilder().addDefaultHandlers("ch.jalu.testmodule").create();

// Instantiate class with method param
ClassWithMethodParam classWithMethodParam = injector.getSingleton(ClassWithMethodParam.class);
if (!"one service".equals(classWithMethodParam.getName())) {
throw new IllegalStateException("ClassWithMethodParam#getName was not as expected");
}

// Instantiate class with optional field (via fallback instantiation to trigger field injection scan)
ClassWithField classWithField = injector.getSingleton(ClassWithField.class);
if (!"one service".equals(classWithField.getName(classWithMethodParam))) {
throw new IllegalStateException("ClassWithField#getName was not as expected");
}
}
}

0 comments on commit b694c9a

Please sign in to comment.