Class BeanInstantiator<T>
- Type Parameters:
T- The bean type being created.
Uses a BeanStore to find available ways to construct beans via injection of beans from the store.
This class is instantiated through the following method:
Example:
Bean Creation Order:
The creator attempts to instantiate beans in the following order:
- Using a Builder (if a builder is found or specified):
- Calls a
build() /create() /get() method on the builder that returns the bean type.- The method is required - if no such method exists, bean creation fails.
- If the method has
@Inject or@Autowired annotation, its parameters are resolved from the bean store. - If the method does not have
@Inject annotation, it must have no parameters.
- Calls a
- Without a Builder:
- Calls a static no-arg
getInstance() method on the bean class. - Calls a public constructor with parameters that can be resolved from the bean store.
- Falls back to a protected constructor with parameters that can be resolved from the bean store.
- Falls back to a package-private constructor with parameters that can be resolved from the bean store. Private constructors are deliberately excluded.
- Calls a static no-arg
Builder Detection:
Builders are detected in the following priority order:
- Explicitly set via
BeanInstantiator.Builder.builder(Object)orBeanInstantiator.Builder.builder(Class). @Builderannotation on the bean subtype.@Builderannotation on the bean type.- Auto-detection:
- Static
create() orbuilder() method that returns a builder type (method names can be customized viaBeanInstantiator.Builder.builderMethodNames(String...)). - Inner class named
Builder .
- Static
Dependency Injection:
Both builder instances and the final bean have automatic dependency injection performed via
ClassInfo.inject(Object, BeanStore), which processes @Inject and @Autowired annotations
on fields and methods.
This means that after a bean is created, all of its fields and methods annotated with @Inject or @Autowired
will automatically be populated with beans from the bean store.
Caching:
By default, each call to run() creates a new bean instance. To enable caching, call BeanInstantiator.Builder.cached().
When caching is enabled, bean instances are cached using Memoizer, so multiple calls to run()
will return the same instance unless reset() is called or configuration changes. Explicit implementations set via
BeanInstantiator.Builder.impl(Object) and explicit builder instances set via BeanInstantiator.Builder.builder(Object) are preserved
across resets.
Thread Safety:
Configuration is performed on the inner BeanInstantiator.Builder which is not thread-safe. Configure on a single
thread, then call BeanInstantiator.Builder.build() to produce an immutable BeanInstantiator that is safe for
shared concurrent use. The BeanInstantiator.Builder.run() shorthand is equivalent to build().run() and is
appropriate for one-shot bean creation from a single thread.
Notes:
- Builders must have a
build() ,create() , orget() method that returns the bean type. This method is required - constructors accepting builders are no longer supported. - The builder's
build() /create() /get() method may have@Inject or@Autowired annotation to allow dependency injection of parameters from the bean store. - If the builder's
build() /create() /get() method does not have@Inject annotation, it must have no parameters. - All constructors and methods except
OptionalandListparameters must have beans available in the store. - If multiple constructors/methods are found, the one with the most matching parameters is used.
- Deprecated and @BeanIgnore-annotated methods/constructors are ignored.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder forBeanInstantiator.static enumLifecycle scope for the bean being created. -
Field Summary
FieldsModifier and TypeFieldDescriptionDefault build method names used for builder build methods (instance methods on builder that build/return the bean).Default builder class names for auto-detection.Default builder method names used for builder factory methods (static methods that create/return a builder).Default factory method names used for bean instantiation. -
Method Summary
Modifier and TypeMethodDescriptionConverts this creator into a memoizer.Attempts to create the bean, returning anOptionalinstead of throwing an exception on failure.Converts this creator into a supplier.static <T> TcreateOrDefault(Class<T> beanType, T defaultValue) Instantiates a bean of the given type, or returns a default value if the type isnull .static <T> TcreateOrNull(Class<T> beanType) Instantiates a bean of the given type, or returnsnull if the type isnull or instantiation fails.protected ClassInfoReturns the bean subtype class info if specified, or the normal bean type if not.Returns the list of bean subtype classes, including the bean subtype, bean type, and all classes in between.<B> Optional<B>Returns the builder instance if one has been created or specified.protected ClassInfoReturns the builder type class info determined by builder type detection.Returns the list of builder types, including the primary builder type and any builder types found in the builder's parent hierarchy.Returns an unmodifiable list of debug log entries from the last bean creation attempt.getName()Returns the name of the bean being created.static <T> BeanInstantiator.Builder<T>Creates a new bean creator builder.static <T> BeanInstantiator.Builder<T>Creates a new bean creator builder with a parent bean store.static <T> BeanInstantiator.Builder<T>Creates a new bean creator builder with a parent bean store, name, and enclosing instance.static <T> Optional<T>optionalOf(Class<T> beanType) Instantiates a bean of the given type and wraps it in anOptional, or returnsOptional.empty()if the type isnull or instantiation fails.reset()Resets the cached bean instance and the inner Builder's resolved memoizers.run()Creates the bean.
-
Field Details
-
DEFAULT_FACTORY_METHOD_NAMES
Default factory method names used for bean instantiation. -
DEFAULT_BUILDER_METHOD_NAMES
Default builder method names used for builder factory methods (static methods that create/return a builder). -
DEFAULT_BUILD_METHOD_NAMES
Default build method names used for builder build methods (instance methods on builder that build/return the bean). -
DEFAULT_BUILDER_CLASS_NAMES
Default builder class names for auto-detection.
-
-
Method Details
-
of
Creates a new bean creator builder.- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Must not benull .- Returns:
- A new bean creator builder.
-
of
Creates a new bean creator builder with a parent bean store.- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Must not benull .parentStore- The parent bean store to use for resolving dependencies. Can benull (the internal bean store then has no parent fallback).- Returns:
- A new bean creator builder.
-
of
public static <T> BeanInstantiator.Builder<T> of(Class<T> beanType, BeanStore parentStore, String name, Object enclosingInstance) Creates a new bean creator builder with a parent bean store, name, and enclosing instance.- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Must not benull .parentStore- The parent bean store to use for resolving dependencies. Can benull (the internal bean store then has no parent fallback).name- The bean name. Can benull for unnamed beans.enclosingInstance- The enclosing instance object. Can benull (the outer-instance skip rule is then never applied).- Returns:
- A new bean creator builder.
-
createOrNull
Instantiates a bean of the given type, or returnsnull if the type isnull or instantiation fails.Shorthand for the common pattern:
cls ==null ?null : BeanInstantiator.of (cls ).fallback(() ->null ).run()- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Can benull .- Returns:
- A new bean instance, or
null if the type isnull or instantiation fails.
-
optionalOf
Instantiates a bean of the given type and wraps it in anOptional, or returnsOptional.empty()if the type isnull or instantiation fails.Shorthand for the common pattern:
cls ==null ? Optional.empty() : BeanInstantiator.of (cls ).asOptional()- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Can benull .- Returns:
- An
Optionalcontaining the created bean, orOptional.empty()if the type isnull or instantiation fails.
-
createOrDefault
Instantiates a bean of the given type, or returns a default value if the type isnull .Shorthand for the common pattern:
cls ==null ?defaultValue : BeanInstantiator.of (cls ).run()- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create. Can benull .defaultValue- The value to return ifbeanTypeisnull . Can benull .- Returns:
- A new bean instance, or
defaultValueif the type isnull .
-
run
Creates the bean.After creating the bean, this method automatically injects dependencies into fields and methods annotated with
@Injector@AutowiredusingClassInfo.inject(Object, BeanStore).- Returns:
- A new bean with all dependencies injected.
- Throws:
ExecutableException- if bean could not be created.
-
asOptional
Attempts to create the bean, returning anOptionalinstead of throwing an exception on failure.- Returns:
- An
Optionalcontaining the created bean, orOptional.empty()if creation failed.
-
asMemoizer
Converts this creator into a memoizer.- Returns:
- A resettable supplier that caches the created bean until reset.
-
asSupplier
Converts this creator into a supplier.- Returns:
- A supplier that returns the results of the
run()method.
-
reset
Resets the cached bean instance and the inner Builder's resolved memoizers.- Returns:
- This object.
-
getName
Returns the name of the bean being created.- Returns:
- The bean name, or
null if not set.
-
getBeanSubType
Returns the bean subtype class info if specified, or the normal bean type if not.- Returns:
- The bean subtype class info, or the bean type class info if no subtype was specified.
-
getBeanSubTypes
Returns the list of bean subtype classes, including the bean subtype, bean type, and all classes in between.- Returns:
- A list of bean subtype class info objects, never
null .
-
getBuilder
Returns the builder instance if one has been created or specified.- Type Parameters:
B- The builder type.- Returns:
- The builder instance wrapped in an
Optional, or an empty optional if no builder exists.
-
getBuilderType
Returns the builder type class info determined by builder type detection.- Returns:
- The builder type class info, or
null if no builder type was determined.
-
getBuilderTypes
Returns the list of builder types, including the primary builder type and any builder types found in the builder's parent hierarchy.- Returns:
- A list of builder type class info objects, never
null .
-
getDebugLog
Returns an unmodifiable list of debug log entries from the last bean creation attempt.- Returns:
- An unmodifiable list of debug log entries.
-