com.opensymphony.xwork2.inject

## Class ContainerBuilder

- java.lang.Object
- - 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:

  - Injects the current `Container`.
  - Injects the `Logger` for the injected member's declaring class.
  Author:
    crazybob@google.com (Bob Lee)

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `ContainerBuilder.Command` Implemented by classes which participate in building a container.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ContainerBuilder()` Constructs a new builder.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `<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.
    `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)`
    `Container` | `create(boolean loadSingletons)` Creates a `Container` instance.
    `<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.PROTOTYPE)`.
    `<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.PROTOTYPE)`.
    `<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)`.
    `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`

- - ### Constructor Detail

    - #### ContainerBuilder

          public ContainerBuilder()

      Constructs a new builder.

  - ### Method Detail

    - #### 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.
      Type Parameters:
        `T` - type
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `factory` - of dependency
        `scope` - scope of injected instances
      Returns:
        a container builder
      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.PROTOTYPE)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `name` - of dependency
        `factory` - of dependency
      Returns:
        a container builder
      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.PROTOTYPE)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `factory` - of dependency
      Returns:
        a container builder
      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.
      Type Parameters:
        `T` - type
      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.PROTOTYPE` if no annotation is found.

      Type Parameters:
        `T` - type
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `implementation` - class
      Returns:
        this builder
      See Also:
        `factory(Class, String, Class)`

    - #### factory

          public <T> ContainerBuilder factory(Class<T> type)

      Convenience method. Equivalent to `factory(type, Container.DEFAULT_NAME, type)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
      Returns:
        this builder
      See Also:
        `factory(Class, String, Class)`

    - #### factory

          public <T> ContainerBuilder factory(Class<T> type,
                                              String name)

      Convenience method. Equivalent to `factory(type, name, type)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `name` - of dependency
      Returns:
        this builder
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `implementation` - class
        `scope` - the scope
      Returns:
        this builder
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `scope` - the scope
      Returns:
        this builder
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `name` - of dependency
        `scope` - the scope
      Returns:
        this builder
      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)`.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `alias` - of dependency
      Returns:
        this builder
      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.
      Type Parameters:
        `T` - type
      Parameters:
        `type` - of dependency
        `name` - of dependency
        `alias` - of to the dependency
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           String value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           int value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           long value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           boolean value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           double value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - constant value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           float value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           short value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           char value)

      Maps a constant value to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public ContainerBuilder constant(String name,
                                           Class value)

      Maps a class to the given name.
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### constant

          public <E extends Enum<E>> ContainerBuilder constant(String name,
                                                               E value)

      Maps an enum to the given name.
      Type Parameters:
        `E` - value type
      Parameters:
        `name` - name
        `value` - value
      Returns:
        this builder

    - #### 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
      Returns:
        this builder

    - #### contains

          public boolean contains(Class<?> type,
                                  String name)

      Parameters:
        `type` - type of class
        `name` - name of class
      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)`.
      Parameters:
        `type` - type of class
      Returns:
        true if this builder contains a mapping for the given type.

    - #### 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.
      Returns:
        this builder
      Throws:
        `IllegalStateException` - if called more than once

    - #### setAllowDuplicates

          public void setAllowDuplicates(boolean val)

Copyright © 2000–2025 Apache Software Foundation. All rights reserved.