Interface BeanStore
- All Known Subinterfaces:
WritableBeanStore
- All Known Implementing Classes:
BasicBeanStore,SpringBeanStore,StackOverlay,TestBeanStore
This interface provides a minimal contract for bean lookup operations, similar to Spring's BeanFactory interface.
Implementations of this interface can be backed by various sources:
- Spring's ApplicationContext
- Custom in-memory stores
- Other dependency injection frameworks
Example:
BeanStore
See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> Optional<T>createBeanFromMethod(Class<T> beanType, Object onClassOrObject) Convenience overload ofcreateBeanFromMethod(Class, Object, Predicate, Object...)with no filter and no extra beans.default <T> Optional<T>createBeanFromMethod(Class<T> beanType, Object onClassOrObject, Predicate<MethodInfo> filter, Object... extraBeans) Finds and invokes a factory method that produces a bean of typebeanType .<T> Optional<T>Returns a bean of the specified type.<T> Optional<T>Returns a bean of the specified type and name.getBeansOfType(Class<T> beanType) Returns all beans of the specified type, keyed by bean name.getBeanSupplier(Class<T> beanType) Returns the supplier for an unnamed bean of the specified type.getBeanSupplier(Class<T> beanType, String name) Returns the supplier for a named bean of the specified type.getBeanType(Class<T> beanType) Returns the implementation class registered for the specified bean type, if any.default StringgetMissingParams(ExecutableInfo executable, Object outer) Returns a comma-delimited list of parameter type names that cannot be resolved from this store.default Object[]getParams(ExecutableInfo executable, Object outer) Resolves every parameter of the specified executable from this store.default booleanhasAllParams(ExecutableInfo executable, Object outer) Returnstrue if this store can satisfy every parameter of the specified executable.booleanReturnstrue if this store contains at least one bean of the specified type.booleanReturnstrue if this store contains a bean of the specified type and name.default <T> Tinstantiate(Class<T> type) Convenience shortcut for instantiating a bean of the specified type using this store.default <T> Optional<T>invokeBeanMethod(Class<T> beanType, MethodInfo method, Object onClassOrObject, Object... extraBeans) Invokes one specific factory method to produce a bean of typebeanType .
-
Method Details
-
getBean
Returns a bean of the specified type.If multiple beans of the specified type exist, the behavior is implementation-dependent. Typically, implementations will return the primary bean or throw an exception.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- The bean, or
Optional.empty()if no bean of the specified type exists.
-
getBean
Returns a bean of the specified type and name.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.name- The bean name. Can benull for unnamed beans.- Returns:
- The bean, or
Optional.empty()if no bean of the specified type and name exists.
-
getBeansOfType
Returns all beans of the specified type, keyed by bean name.The map keys are the bean names, and the values are the bean instances. If no beans of the specified type exist, returns an empty map.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- A map of bean names to bean instances. Never
null .
-
hasBean
Returnstrue if this store contains at least one bean of the specified type.- Parameters:
beanType- The bean type.- Returns:
true if this store contains at least one bean of the specified type.
-
hasBean
Returnstrue if this store contains a bean of the specified type and name.- Parameters:
beanType- The bean type.name- The bean name. Can benull for unnamed beans.- Returns:
true if this store contains a bean of the specified type and name.
-
instantiate
Convenience shortcut for instantiating a bean of the specified type using this store.Equivalent to:
BeanInstantiator.
of (type ,this ).run()Use this when you don't need any of
BeanInstantiator's configuration options (subtype, factory-method names, builder hooks, etc.). For more control over instantiation, useBeanInstantiator.of(Class, BeanStore)directly.Example:
// Instantiate a bean using this store for parameter resolution MyServiceservice =beanStore .instantiate(MyService.class );- Type Parameters:
T- The bean type.- Parameters:
type- The bean type to instantiate. Must not benull .- Returns:
- The instantiated bean.
-
getBeanSupplier
Returns the supplier for an unnamed bean of the specified type.If no supplier is found in this store, searches the parent store recursively.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.- Returns:
- The supplier, or
Optional.empty()if no supplier of the specified type exists.
-
getBeanSupplier
Returns the supplier for a named bean of the specified type.If no supplier with the specified name is found in this store, searches the parent store recursively.
- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type.name- The bean name. Can benull for unnamed beans.- Returns:
- The supplier, or
Optional.empty()if no supplier of the specified type and name exists.
-
createBeanFromMethod
default <T> Optional<T> createBeanFromMethod(Class<T> beanType, Object onClassOrObject, Predicate<MethodInfo> filter, Object... extraBeans) Finds and invokes a factory method that produces a bean of typebeanType .Scans the methods of the resource class identified by
onClassOrObject :- If
onClassOrObject is aClass, onlystatic methods are eligible and the instance parameter passed to the invoked method isnull . - If
onClassOrObject is any other object, both instance and static methods on its class are eligible, and the object itself is passed as the receiver.
The search has two passes. The first pass considers public methods only. If it finds nothing, a second pass considers non-public methods (protected, package-private or private) across the class hierarchy, child class first, but only those that carry
@Bean(declared directly or inherited from a method they override; seeBeanAnnotation.find(MethodInfo)). A non-public method without@Beanis never invoked, even whenfilter isnull . Non-public methods are made accessible before invocation.The first method satisfying all of the following is invoked:
- Not deprecated.
- Return type is assignment-compatible with
beanType . - Accepted by
filter (if non-null ; otherwise any method qualifies). - All parameters can be resolved from this store plus any
extraBeans .
The default implementation returns
Optional.empty(). Override in concrete stores (seeBasicBeanStore) to enable factory-method scanning.Example:
// Filter only beanStore .createBeanFromMethod(RichLogger.class ,resource ,m ->m .hasAnnotation(Bean.class )) .ifPresent(creator ::impl);// Filter + extra bean not yet in the store beanStore .createBeanFromMethod(EncoderSet.class ,resource ,m ->m .hasAnnotation(Bean.class ),builder ) .ifPresent(x ->builder .impl(x ));// No filter, no extra beans beanStore .createBeanFromMethod(RichLogger.class ,resource );- Type Parameters:
T- The bean type.- Parameters:
beanType- The type of bean to create. Must not benull .onClassOrObject- The object instance orClasswhose methods are searched. Must not benull .filter- Optional predicate restricting which methods are eligible. Can benull (any method qualifies).extraBeans- Optional bean instances visible to parameter resolution for this call only. These are not registered in the store.- Returns:
- The created bean wrapped in an
Optional, orOptional.empty()if no matching factory method was found. - Throws:
BeanCreationException- If a matching method was found but threw an exception during invocation.- See Also:
- If
-
invokeBeanMethod
default <T> Optional<T> invokeBeanMethod(Class<T> beanType, MethodInfo method, Object onClassOrObject, Object... extraBeans) Invokes one specific factory method to produce a bean of typebeanType .Unlike
createBeanFromMethod(Class, Object, Predicate, Object...), no search is performed: the caller supplies theMethodInfoit has already chosen. Visibility is not a gate: public, protected, package-private and private methods are all invoked (non-public methods are made accessible first). Deprecation is not checked.If
onClassOrObject is aClass, onlystatic methods can be invoked. Otherwise the object is used as the receiver for instance methods.Returns
Optional.empty()when the method is not eligible: it returnsvoid , it is a bridge or synthetic method, it is an instance method andonClassOrObject is aClass, or one of its parameters cannot be resolved from this store plusextraBeans .Example:
// Invoke one specific @Bean factory method, instead of searching by return type. MethodInfom = ClassInfo.of (MyResource.class ).getDeclaredMethod(x ->x .hasName("cacheIndicator" )).get(); Optional<HealthIndicator>cache =beanStore .invokeBeanMethod(HealthIndicator.class ,m ,resource );- Type Parameters:
T- The bean type.- Parameters:
beanType- The expected bean type. Must be assignable from the method's return type. Must not benull .method- The factory method to invoke. Must not benull .onClassOrObject- The receiver object, or theClassfor static methods. Must not benull . The method's declaring class must be assignable from this object's class (or this class).extraBeans- Optional bean instances visible to parameter resolution for this call only.- Returns:
- The created bean, or
Optional.empty()if the method is not eligible. - Throws:
IllegalArgumentException- If an argument isnull ,beanType is not assignable from the method's return type, or the method does not belong to the target's class hierarchy.BeanCreationException- If the method cannot be made accessible or threw an exception during invocation.
-
createBeanFromMethod
Convenience overload ofcreateBeanFromMethod(Class, Object, Predicate, Object...)with no filter and no extra beans.- Type Parameters:
T- The bean type.- Parameters:
beanType- The type of bean to create. Must not benull .onClassOrObject- The object instance orClasswhose methods are searched. Must not benull .- Returns:
- The created bean wrapped in an
Optional, orOptional.empty()if no matching factory method was found. - Throws:
BeanCreationException- If a matching method was found but threw an exception during invocation.
-
getBeanType
Returns the implementation class registered for the specified bean type, if any.Type bindings let callers register a default implementation class to be instantiated when an actual bean of
beanType is needed. Bindings are inherited through the parent chain.The default implementation returns
Optional.empty()(no binding). Override in concrete stores (seeBasicBeanStore) to enable type binding.- Type Parameters:
T- The bean type.- Parameters:
beanType- The bean type whose implementation class is being looked up.- Returns:
- The implementation class, or
Optional.empty()if no binding exists.
-
hasAllParams
Returnstrue if this store can satisfy every parameter of the specified executable.Skips:
- The first parameter when it is an instance of
outer (inner-class outer reference). - Parameters typed as
Optional(always considered satisfied).
- Parameters:
executable- The constructor or method to inspect. Must not benull .outer- The outer object to use for inner-class param resolution. Can benull (the outer-instance skip rule is then never applied).- Returns:
true if every required parameter can be resolved from this store.
- The first parameter when it is an instance of
-
getParams
Resolves every parameter of the specified executable from this store.For
Optional-typed parameters the wrappedOptionalis returned directly; for all others the bean instance (ornull when missing) is returned. SeehasAllParams(org.apache.juneau.commons.reflect.ExecutableInfo, java.lang.Object)for the full skip rules.- Parameters:
executable- The constructor or method to resolve parameters for. Must not benull .outer- The outer object to use for inner-class param resolution. Can benull (the outer-instance skip rule is then never applied).- Returns:
- An array of resolved parameter values, one per parameter of
executable .
-
getMissingParams
Returns a comma-delimited list of parameter type names that cannot be resolved from this store.Intended for diagnostic error messages — see
hasAllParams(org.apache.juneau.commons.reflect.ExecutableInfo, java.lang.Object)for the exact match rules. Skipped parameters (outer-instance match,Optional) never appear in the result.- Parameters:
executable- The constructor or method to inspect. Must not benull .outer- The outer object to use for inner-class param resolution. Can benull (the outer-instance skip rule is then never applied).- Returns:
- A comma-delimited list of missing parameter type names, or
null when none are missing.
-