Class BeanStore
- Direct Known Subclasses:
BeanStore.Void,SpringBeanStore
A simple storage database for beans keyed by type and name. Used to retrieve and instantiate beans using an injection-like API. It's similar in concept to the injection framework of Spring but greatly simplified in function and not intended to implement a full-fledged injection framework.
Beans can be stored with or without names. Named beans are typically resolved using
the
Beans are added through the following methods:
add(Class,Object)add(Class,Object,String)addBean(Class,Object)addBean(Class,Object,String)addSupplier(Class,Supplier)addSupplier(Class,Supplier,String)
Beans are retrieved through the following methods:
Beans are created through the following methods:
createBean(Class)createMethodFinder(Class)createMethodFinder(Class,Class)createMethodFinder(Class,Object)
Notes:
- Bean stores can be nested using
BeanStore.Builder.parent(BeanStore). - Bean stores can be made read-only using
BeanStore.Builder.readOnly(). - Bean stores can be made thread-safe using
BeanStore.Builder.threadSafe().
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static final classNon-existent bean store. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TSame asaddBean(Class,Object)but returns the bean instead of this object for fluent calls.<T> TSame asaddBean(Class,Object,String)but returns the bean instead of this object for fluent calls.<T> BeanStoreAdds an unnamed bean of the specified type to this factory.<T> BeanStoreAdds a named bean of the specified type to this factory.<T> BeanStoreaddSupplier(Class<T> beanType, Supplier<T> bean) Adds a supplier for an unnamed bean of the specified type to this factory.<T> BeanStoreaddSupplier(Class<T> beanType, Supplier<T> bean, String name) Adds a supplier for a named bean of the specified type to this factory.clear()Clears out all bean in this bean store.static BeanStore.Buildercreate()Static creator.<T> BeanCreator<T>createBean(Class<T> beanType) Instantiates a bean creator.protected <T> BeanStoreEntry<T>createEntry(Class<T> type, Supplier<T> bean, String name) Creates an entry in this store for the specified bean.<T> BeanCreateMethodFinder<T>createMethodFinder(Class<T> beanType) Create a method finder for finding bean creation methods.<T> BeanCreateMethodFinder<T>createMethodFinder(Class<T> beanType, Class<?> resourceClass) Create a method finder for finding bean creation methods.<T> BeanCreateMethodFinder<T>createMethodFinder(Class<T> beanType, Object resource) Create a method finder for finding bean creation methods.<T> Optional<T>Returns the unnamed bean of the specified type.<T> Optional<T>Returns the named bean of the specified type.getMissingParams(ExecutableInfo executable) Given an executable, returns a list of types that are missing from this factory.Object[]getParams(ExecutableInfo executable) Returns the corresponding beans in this factory for the specified param types.booleanhasAllParams(ExecutableInfo executable) Given the list of param types, returnstrue if this factory has all the parameters for the specified executable.booleanReturnstrue if this store contains the specified unnamed bean type.booleanReturnstrue if this store contains the specified named bean type.static BeanStoreStatic creator.static BeanStoreStatic creator.removeBean(Class<?> beanType) Removes an unnamed bean from this store.removeBean(Class<?> beanType, String name) Removes a named bean from this store.<T> Stream<BeanStoreEntry<T>>Returns all the beans in this store of the specified type.toString()
-
Field Details
-
INSTANCE
Static read-only reusable instance.
-
-
Constructor Details
-
BeanStore
Constructor.- Parameters:
builder- The builder containing the settings for this bean.
-
-
Method Details
-
create
Static creator.- Returns:
- A new
BeanStore.Builderobject.
-
of
Static creator.- Parameters:
parent- Parent bean store. Can benull if this is the root resource.- Returns:
- A new
BeanStoreobject.
-
of
Static creator.- Parameters:
parent- Parent bean store. Can benull if this is the root resource.outer- The outer bean used when instantiating inner classes. Can benull .- Returns:
- A new
BeanStoreobject.
-
add
Same asaddBean(Class,Object)but returns the bean instead of this object for fluent calls.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean. Can benull .- Returns:
- The bean.
-
add
Same asaddBean(Class,Object,String)but returns the bean instead of this object for fluent calls.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean. Can benull .name- The bean name if this is a named bean. Can benull .- Returns:
- The bean.
-
addBean
Adds an unnamed bean of the specified type to this factory.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean. Can benull .- Returns:
- This object.
-
addBean
Adds a named bean of the specified type to this factory.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean. Can benull .name- The bean name if this is a named bean. Can benull .- Returns:
- This object.
-
addSupplier
Adds a supplier for an unnamed bean of the specified type to this factory.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean supplier.- Returns:
- This object.
-
addSupplier
Adds a supplier for a named bean of the specified type to this factory.- Type Parameters:
T- The class to associate this bean with.- Parameters:
beanType- The class to associate this bean with.bean- The bean supplier.name- The bean name if this is a named bean. Can benull .- Returns:
- This object.
-
clear
Clears out all bean in this bean store.Does not affect the parent bean store.
- Returns:
- This object.
-
createBean
Instantiates a bean creator.See Also:
BeanCreatorfor usage.
- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create.- Returns:
- A new bean creator.
-
createMethodFinder
Create a method finder for finding bean creation methods.Same as
createMethodFinder(Class,Object)but usesBeanStore.Builder.outer(Object)as the resource bean.See Also:
BeanCreateMethodFinderfor usage.
- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create.- Returns:
- The method finder. Never
null .
-
createMethodFinder
Create a method finder for finding bean creation methods.Same as
createMethodFinder(Class,Class)but looks for only static methods on the specified resource class and not also instance methods within the context of a bean.See Also:
BeanCreateMethodFinderfor usage.
- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create.resourceClass- The class containing the bean creator method.- Returns:
- The method finder. Never
null .
-
createMethodFinder
Create a method finder for finding bean creation methods.See Also:
BeanCreateMethodFinderfor usage.
- Type Parameters:
T- The bean type to create.- Parameters:
beanType- The bean type to create.resource- The class containing the bean creator method.- Returns:
- The method finder. Never
null .
-
getBean
Returns the unnamed bean of the specified type.- Type Parameters:
T- The type of bean to return.- Parameters:
beanType- The type of bean to return.- Returns:
- The bean.
-
getBean
Returns the named bean of the specified type.- Type Parameters:
T- The type of bean to return.- Parameters:
beanType- The type of bean to return.name- The bean name. Can benull .- Returns:
- The bean.
-
getMissingParams
Given an executable, returns a list of types that are missing from this factory.- Parameters:
executable- The constructor or method to get the params for.- Returns:
- A comma-delimited list of types that are missing from this factory, or
null if none are missing.
-
getParams
Returns the corresponding beans in this factory for the specified param types.- Parameters:
executable- The constructor or method to get the params for.- Returns:
- The corresponding beans in this factory for the specified param types.
-
hasAllParams
Given the list of param types, returnstrue if this factory has all the parameters for the specified executable.- Parameters:
executable- The constructor or method to get the params for.- Returns:
- A comma-delimited list of types that are missing from this factory.
-
hasBean
Returnstrue if this store contains the specified unnamed bean type.- Parameters:
beanType- The bean type to check.- Returns:
true if this store contains the specified unnamed bean type.
-
hasBean
Returnstrue if this store contains the specified named bean type.- Parameters:
beanType- The bean type to check.name- The bean name.- Returns:
true if this store contains the specified named bean type.
-
removeBean
Removes an unnamed bean from this store.- Parameters:
beanType- The bean type being removed.- Returns:
- This object.
-
removeBean
Removes a named bean from this store.- Parameters:
beanType- The bean type being removed.name- The bean name to remove.- Returns:
- This object.
-
stream
Returns all the beans in this store of the specified type.Returns both named and unnamed beans.
The results from the parent bean store are appended to the list of beans from this beans store.
- Type Parameters:
T- The bean type to return.- Parameters:
beanType- The bean type to return.- Returns:
- The bean entries. Never
null .
-
properties
-
toString
-
createEntry
Creates an entry in this store for the specified bean.Subclasses can override this method to create their own entry subtypes.
- Type Parameters:
T- The class type to associate with the bean.- Parameters:
type- The class type to associate with the bean.bean- The bean supplier.name- Optional name to associate with the bean. Can benull .- Returns:
- A new bean store entry.
-