com.opensymphony.xwork2.inject
Class ContainerBuilder

java.lang.Object
  extended by com.opensymphony.xwork2.inject.ContainerBuilder

public final class ContainerBuilder
extends Object

Builds a dependency injection Container. The combination of dependency type and name uniquely identifies a dependency mapping; you can use the same name for two different types. Not safe for concurrent use.

Adds the following factories by default:

Author:
crazybob@google.com (Bob Lee)

Nested Class Summary
static interface ContainerBuilder.Command
          Implemented by classes which participate in building a container.
 
Field Summary
(package private)  boolean allowDuplicates
           
private static InternalFactory<Container> CONTAINER_FACTORY
           
(package private)  boolean created
           
(package private)  Map<Key<?>,InternalFactory<?>> factories
           
private static InternalFactory<Logger> LOGGER_FACTORY
           
(package private)  List<InternalFactory<?>> singletonFactories
           
(package private)  List<Class<?>> staticInjections
           
 
Constructor Summary
ContainerBuilder()
          Constructs a new builder.
 
Method Summary
<T> ContainerBuilder
alias(Class<T> type, String alias)
          Convenience method. Equivalent to alias(type, Container.DEFAULT_NAME, type).
<T> ContainerBuilder
alias(Class<T> type, String name, String alias)
          Maps an existing factory to a new name.
private
<T> ContainerBuilder
alias(Key<T> key, Key<T> aliasKey)
          Maps an existing dependency.
private  void checkKey(Key<?> key)
          Ensures a key isn't already mapped.
private
<T> ContainerBuilder
constant(Class<T> type, String name, T value)
          Maps a constant value to the given type and name.
 ContainerBuilder constant(String name, boolean value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, char value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, Class value)
          Maps a class to the given name.
 ContainerBuilder constant(String name, double value)
          Maps a constant value to the given name.
<E extends Enum<E>>
ContainerBuilder
constant(String name, E value)
          Maps an enum to the given name.
 ContainerBuilder constant(String name, float value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, int value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, long value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, short value)
          Maps a constant value to the given name.
 ContainerBuilder constant(String name, String value)
          Maps a constant value to the given name.
 boolean contains(Class<?> type)
          Convenience method. Equivalent to contains(type, Container.DEFAULT_NAME).
 boolean contains(Class<?> type, String name)
          Returns true if this builder contains a mapping for the given type and name.
 Container create(boolean loadSingletons)
          Creates a Container instance.
private  void ensureNotCreated()
          Currently we only support creating one Container instance per builder.
<T> ContainerBuilder
factory(Class<T> type)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, type).
<T> ContainerBuilder
factory(Class<T> type, Class<? extends T> implementation)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, implementation).
<T> ContainerBuilder
factory(Class<T> type, Class<? extends T> implementation, Scope scope)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, implementation, scope).
<T> ContainerBuilder
factory(Class<T> type, Factory<? extends T> factory)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, factory, Scope.DEFAULT).
<T> ContainerBuilder
factory(Class<T> type, Factory<? extends T> factory, Scope scope)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, factory, scope).
<T> ContainerBuilder
factory(Class<T> type, Scope scope)
          Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, type, scope).
<T> ContainerBuilder
factory(Class<T> type, String name)
          Convenience method. Equivalent to factory(type, name, type).
<T> ContainerBuilder
factory(Class<T> type, String name, Class<? extends T> implementation)
          Maps an implementation class to a given dependency type and name.
<T> ContainerBuilder
factory(Class<T> type, String name, Class<? extends T> implementation, Scope scope)
          Maps an implementation class to a given dependency type and name.
<T> ContainerBuilder
factory(Class<T> type, String name, Factory<? extends T> factory)
          Convenience method. Equivalent to factory(type, name, factory, Scope.DEFAULT).
<T> ContainerBuilder
factory(Class<T> type, String name, Factory<? extends T> factory, Scope scope)
          Maps a factory to a given dependency type and name.
<T> ContainerBuilder
factory(Class<T> type, String name, Scope scope)
          Convenience method. Equivalent to factory(type, name, type, scope).
private
<T> ContainerBuilder
factory(Key<T> key, InternalFactory<? extends T> factory, Scope scope)
          Maps a dependency.
 ContainerBuilder injectStatics(Class<?>... types)
          Upon creation, the Container will inject static fields and methods into the given classes.
 void setAllowDuplicates(boolean val)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factories

final Map<Key<?>,InternalFactory<?>> factories

singletonFactories

final List<InternalFactory<?>> singletonFactories

staticInjections

final List<Class<?>> staticInjections

created

boolean created

allowDuplicates

boolean allowDuplicates

CONTAINER_FACTORY

private static final InternalFactory<Container> CONTAINER_FACTORY

LOGGER_FACTORY

private static final InternalFactory<Logger> LOGGER_FACTORY
Constructor Detail

ContainerBuilder

public ContainerBuilder()
Constructs a new builder.

Method Detail

factory

private <T> ContainerBuilder factory(Key<T> key,
                                     InternalFactory<? extends T> factory,
                                     Scope scope)
Maps a dependency. All methods in this class ultimately funnel through here.


checkKey

private void checkKey(Key<?> key)
Ensures a key isn't already mapped.


factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name,
                                    Factory<? extends T> factory,
                                    Scope scope)
Maps a factory to a given dependency type and name.

Parameters:
type - of dependency
name - of dependency
factory - creates objects to inject
scope - scope of injected instances
Returns:
this builder

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    Factory<? extends T> factory,
                                    Scope scope)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, factory, scope).

See Also:
factory(Class, String, Factory, Scope)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name,
                                    Factory<? extends T> factory)
Convenience method. Equivalent to factory(type, name, factory, Scope.DEFAULT).

See Also:
factory(Class, String, Factory, Scope)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    Factory<? extends T> factory)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, factory, Scope.DEFAULT).

See Also:
factory(Class, String, Factory, Scope)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name,
                                    Class<? extends T> implementation,
                                    Scope scope)
Maps an implementation class to a given dependency type and name. Creates instances using the container, recursively injecting dependencies.

Parameters:
type - of dependency
name - of dependency
implementation - class
scope - scope of injected instances
Returns:
this builder

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name,
                                    Class<? extends T> implementation)
Maps an implementation class to a given dependency type and name. Creates instances using the container, recursively injecting dependencies.

Sets scope to value from Scoped annotation on the implementation class. Defaults to Scope.DEFAULT if no annotation is found.

Parameters:
type - of dependency
name - of dependency
implementation - class
Returns:
this builder

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    Class<? extends T> implementation)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, implementation).

See Also:
factory(Class, String, Class)

factory

public <T> ContainerBuilder factory(Class<T> type)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, type).

See Also:
factory(Class, String, Class)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name)
Convenience method. Equivalent to factory(type, name, type).

See Also:
factory(Class, String, Class)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    Class<? extends T> implementation,
                                    Scope scope)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, implementation, scope).

See Also:
factory(Class, String, Class, Scope)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    Scope scope)
Convenience method. Equivalent to factory(type, Container.DEFAULT_NAME, type, scope).

See Also:
factory(Class, String, Class, Scope)

factory

public <T> ContainerBuilder factory(Class<T> type,
                                    String name,
                                    Scope scope)
Convenience method. Equivalent to factory(type, name, type, scope).

See Also:
factory(Class, String, Class, Scope)

alias

public <T> ContainerBuilder alias(Class<T> type,
                                  String alias)
Convenience method. Equivalent to alias(type, Container.DEFAULT_NAME, type).

See Also:
alias(Class, String, String)

alias

public <T> ContainerBuilder alias(Class<T> type,
                                  String name,
                                  String alias)
Maps an existing factory to a new name.

Parameters:
type - of dependency
name - of dependency
alias - of to the dependency
Returns:
this builder

alias

private <T> ContainerBuilder alias(Key<T> key,
                                   Key<T> aliasKey)
Maps an existing dependency. All methods in this class ultimately funnel through here.


constant

public ContainerBuilder constant(String name,
                                 String value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 int value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 long value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 boolean value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 double value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 float value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 short value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 char value)
Maps a constant value to the given name.


constant

public ContainerBuilder constant(String name,
                                 Class value)
Maps a class to the given name.


constant

public <E extends Enum<E>> ContainerBuilder constant(String name,
                                                     E value)
Maps an enum to the given name.


constant

private <T> ContainerBuilder constant(Class<T> type,
                                      String name,
                                      T value)
Maps a constant value to the given type and name.


injectStatics

public ContainerBuilder injectStatics(Class<?>... types)
Upon creation, the Container will inject static fields and methods into the given classes.

Parameters:
types - for which static members will be injected

contains

public boolean contains(Class<?> type,
                        String name)
Returns true if this builder contains a mapping for the given type and name.


contains

public boolean contains(Class<?> type)
Convenience method. Equivalent to contains(type, Container.DEFAULT_NAME).


create

public Container create(boolean loadSingletons)
Creates a Container instance. Injects static members for classes which were registered using injectStatics(Class...).

Parameters:
loadSingletons - If true, the container will load all singletons now. If false, the container will lazily load singletons. Eager loading is appropriate for production use while lazy loading can speed development.
Throws:
IllegalStateException - if called more than once

ensureNotCreated

private void ensureNotCreated()
Currently we only support creating one Container instance per builder. If we want to support creating more than one container per builder, we should move to a "factory factory" model where we create a factory instance per Container. Right now, one factory instance would be shared across all the containers, singletons synchronize on the container when lazy loading, etc.


setAllowDuplicates

public void setAllowDuplicates(boolean val)


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.