Package org.apache.struts2.inject
Class ContainerBuilder
java.lang.Object
org.apache.struts2.inject.ContainerBuilder
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
Modifier and TypeClassDescriptionstatic interface
Implemented by classes which participate in building a container. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescription<T> ContainerBuilder
Convenience method. Equivalent toalias(type, Container.DEFAULT_NAME, type)
.<T> ContainerBuilder
Maps an existing factory to a new name.Maps a constant value to the given name.Maps a constant value to the given name.Maps a constant value to the given name.Maps a constant value to the given name.Maps a constant value to the given name.Maps a constant value to the given name.Maps a constant value to the given name.<E extends Enum<E>>
ContainerBuilderMaps an enum to the given name.Maps a class to the given name.Maps a constant value to the given name.boolean
Convenience method. Equivalent tocontains(type, Container.DEFAULT_NAME)
.boolean
create
(boolean loadSingletons) Creates aContainer
instance.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, type)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, implementation)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, implementation, scope)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, name, type)
.<T> ContainerBuilder
Maps an implementation class to a given dependency type and name.<T> ContainerBuilder
Maps an implementation class to a given dependency type and name.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, name, factory, Scope.PROTOTYPE)
.<T> ContainerBuilder
Maps a factory to a given dependency type and name.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, name, type, scope)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, factory, Scope.PROTOTYPE)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, factory, scope)
.<T> ContainerBuilder
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, type, scope)
.injectStatics
(Class<?>... types) Upon creation, theContainer
will inject static fields and methods into the given classes.void
setAllowDuplicates
(boolean val)
-
Constructor Details
-
ContainerBuilder
public ContainerBuilder()Constructs a new builder.
-
-
Method Details
-
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 dependencyname
- of dependencyfactory
- creates objects to injectscope
- scope of injected instances- Returns:
- this builder
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, factory, scope)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyfactory
- of dependencyscope
- scope of injected instances- Returns:
- a container builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, name, factory, Scope.PROTOTYPE)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyname
- of dependencyfactory
- of dependency- Returns:
- a container builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, factory, Scope.PROTOTYPE)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyfactory
- of dependency- Returns:
- a container builder
- See Also:
-
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 dependencyname
- of dependencyimplementation
- classscope
- scope of injected instances- Returns:
- this builder
-
factory
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 toScope.PROTOTYPE
if no annotation is found.- Type Parameters:
T
- type- Parameters:
type
- of dependencyname
- of dependencyimplementation
- class- Returns:
- this builder
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, implementation)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyimplementation
- class- Returns:
- this builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, type)
.- Type Parameters:
T
- type- Parameters:
type
- of dependency- Returns:
- this builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, name, type)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyname
- of dependency- Returns:
- this builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, implementation, scope)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyimplementation
- classscope
- the scope- Returns:
- this builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, Container.DEFAULT_NAME, type, scope)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyscope
- the scope- Returns:
- this builder
- See Also:
-
factory
Convenience method. Equivalent tofactory(type, name, type, scope)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyname
- of dependencyscope
- the scope- Returns:
- this builder
- See Also:
-
alias
Convenience method. Equivalent toalias(type, Container.DEFAULT_NAME, type)
.- Type Parameters:
T
- type- Parameters:
type
- of dependencyalias
- of dependency- Returns:
- this builder
- See Also:
-
alias
Maps an existing factory to a new name.- Type Parameters:
T
- type- Parameters:
type
- of dependencyname
- of dependencyalias
- of to the dependency- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- constant value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a constant value to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps a class to the given name.- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
constant
Maps an enum to the given name.- Type Parameters:
E
- value type- Parameters:
name
- namevalue
- value- Returns:
- this builder
-
injectStatics
Upon creation, theContainer
will inject static fields and methods into the given classes.- Parameters:
types
- for which static members will be injected- Returns:
- this builder
-
contains
- Parameters:
type
- type of classname
- name of class- Returns:
- true if this builder contains a mapping for the given type and name.
-
contains
Convenience method. Equivalent tocontains(type, Container.DEFAULT_NAME)
.- Parameters:
type
- type of class- Returns:
- true if this builder contains a mapping for the given type.
-
create
Creates aContainer
instance. Injects static members for classes which were registered usinginjectStatics(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)
-