Created
January 7, 2013 22:23
-
-
Save anonymous/4479085 to your computer and use it in GitHub Desktop.
Simple of example of how to use injection with objects that require things which cannot always be injected.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Consumer { | |
@Inject ExampleClass.Factory exampleClassFactory; | |
public void fireZeMissles() { | |
ExampleClass ex = exampleClassFactory.createForUser(42); | |
ex.doThings(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleClass { | |
private final int userId; | |
private final FooBar fooBar; | |
private final Gizmo gizmo; | |
private ExampleClass(int userId, FooBar fooBar, Gizmo gizmo) { | |
this.userId = userId; | |
this.fooBar = fooBar; | |
this.gizmo = gizmo; | |
} | |
public void doThings() { | |
// ... | |
} | |
public static class Factory { | |
private final FooBar fooBar; | |
private final Gizmo gizmo; | |
@Inject public Factory(FooBar fooBar, Gizmo gizmo) { | |
this.fooBar = fooBar; | |
this.gizmo = gizmo; | |
} | |
public ExampleClass createForUser(int userId) { | |
return new ExampleClass(userId, fooBar, gizmo); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment