|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Container
Injects dependencies into constructors, methods and fields annotated with
@Inject
.
When injecting a method or constructor, you can additionally annotate its
parameters with @Inject
and specify a dependency name. When a
parameter has no annotation, the container uses the name from the method or
constructor's @Inject
annotation respectively.
For example:
class Foo { // Inject the int constant named "i". @Inject("i") int i; // Inject the default implementation of Bar and the String constant // named "s". @Inject Foo(Bar bar, @Inject("s") String s) { ... } // Inject the default implementation of Baz and the Bob implementation // named "foo". @Inject void initialize(Baz baz, @Inject("foo") Bob bob) { ... } // Inject the default implementation of Tee. @Inject void setTee(Tee tee) { ... } }
To get an instance of Foo
:
Container c = ...; Key<Foo> fooKey = Key.get(Foo.class); Factory<Foo> fooFactory = c.getFactory(fooKey); Foo foo = fooFactory.get();
ContainerBuilder
Method Summary | ||
---|---|---|
|
findBindingsByType(TypeLiteral<T> type)
Finds all bindings to the given type. |
|
|
getBinding(Key<T> key)
Gets a binding for the given key. |
|
Map<Key<?>,Binding<?>> |
getBindings()
Gets all bindings. |
|
|
getFactory(Class<T> type)
Gets the factory bound to the given type. |
|
|
getFactory(Class<T> type,
String name)
Gets the factory bound to the given type and name. |
|
|
getFactory(Key<T> key)
Gets the factory bound to the given key. |
|
|
getFactory(TypeLiteral<T> type)
Gets the factory bound to the given type. |
|
|
getFactory(TypeLiteral<T> type,
String name)
Gets the factory bound to the given type and name. |
|
|
getInstance(Class<T> type)
Gets an instance from the factory bound to the given type. |
|
|
getInstance(Class<T> type,
String name)
Gets an instance from the factory bound to the given type and name. |
|
|
getInstance(Key<T> key)
Gets an instance from the factory bound to the given key. |
|
|
getInstance(TypeLiteral<T> type)
Gets an instance from the factory bound to the given type. |
|
|
getInstance(TypeLiteral<T> type,
String name)
Gets an instance from the factory bound to the given type and name. |
|
void |
injectMembers(Object o)
Injects dependencies into the fields and methods of an existing object. |
Method Detail |
---|
void injectMembers(Object o)
<T> Factory<T> getFactory(Key<T> key)
Map<Key<?>,Binding<?>> getBindings()
<T> Binding<T> getBinding(Key<T> key)
<T> List<Binding<T>> findBindingsByType(TypeLiteral<T> type)
<T> Factory<T> getFactory(Class<T> type)
<T> Factory<T> getFactory(TypeLiteral<T> type)
<T> T getInstance(TypeLiteral<T> type)
<T> T getInstance(Class<T> type)
<T> T getInstance(Key<T> key)
<T> T getInstance(TypeLiteral<T> type, String name)
<T> T getInstance(Class<T> type, String name)
<T> Factory<T> getFactory(Class<T> type, String name)
<T> Factory<T> getFactory(TypeLiteral<T> type, String name)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |