Interface BeanScope
- All Superinterfaces:
AutoCloseable
Holds beans created by dependency injection.
The beans have singleton scope, support lifecycle methods for postConstruct and preDestroy and are created (wired) via dependency injection.
Create a BeanScope
We can programmatically create a BeanScope via BeanScope.builder().
// create a BeanScope ...
try (BeanScope scope = BeanScope.builder()
.build()) {
CoffeeMaker coffeeMaker = context.get(CoffeeMaker.class);
coffeeMaker.makeIt()
}
External dependencies
We can supporting external dependencies when creating the BeanScope.
Use the @External annotation.
For example, given we have Pump as an externally provided dependency.
class CoffeeMaker {
// tell the annotation processor Pump is provided externally at runtime
// otherwise it thinks we have a missing dependency
@External Pump pump;
}
When building the BeanScope, the dependency must be provided manually via
BeanScopeBuilder.bean(Class, Object).
// provide external dependencies ...
Pump pump = ...
try (BeanScope scope = BeanScope.builder()
.bean(Pump.class, pump)
.build()) {
CoffeeMaker coffeeMaker = context.get(CoffeeMaker.class);
coffeeMaker.makeIt()
}
-
Method Summary
Modifier and TypeMethodDescriptionall()Return all the bean entries from the scope.static BeanScopeBuilderbuilder()Build a bean scope with options for shutdown hook and supplying external dependencies.voidclose()Close the scope firing any@PreDestroylifecycle methods.booleanReturn true if the bean scope contains the given type.booleanReturn true if the bean scope contains the given type.Return the custom scope annotations contained in this bean scope.<T> TReturn a single bean given the type.<T> TReturn a single bean given the type and name.default <T> TReturn a single bean given the full generic type.<T> TReturn a single bean given the full generic type and name.<T> Optional<T> getOptional(Class<T> type) Optionally return a single bean given the type and empty if it is not found.<T> Optional<T> getOptional(Type type, @Nullable String name) Optionally return a single bean given the type and name and empty if it is not found.<T> List<T> Return the list of beans for a given type.<T> List<T> Return the list of beans that implement the given type.<T> List<T> Return the list of beans for the given type and name.listByAnnotation(Class<? extends Annotation> annotation) Return the list of beans that have an annotation.default <T> List<T> listByPriority(Class<T> type) Return the list of beans that implement the class sorting by priority.<T> List<T> listByPriority(Type type) Return the list of beans that implement the type sorting by priority.Return the beans for this type mapped by their qualifier name.
-
Method Details
-
builder
Build a bean scope with options for shutdown hook and supplying external dependencies.We can optionally:
- Provide external dependencies
- Specify a parent BeanScope
- Specify specific modules to wire
- Specify to include a shutdown hook (to fire preDestroy lifecycle methods)
- Use
forTesting()to specify mocks and spies to use when wiring tests
// create a BeanScope ... try (BeanScope scope = BeanScope.builder() .build()) { CoffeeMaker coffeeMaker = context.get(CoffeeMaker.class); coffeeMaker.makeIt() } -
get
Return a single bean given the type.CoffeeMaker coffeeMaker = beanScope.get(CoffeeMaker.class); coffeeMaker.brew();- Parameters:
type- an interface or bean type- Throws:
NoSuchElementException- When no matching bean is found
-
get
Return a single bean given the type and name.Heater heater = beanScope.get(Heater.class, "electric"); heater.heat();- Parameters:
type- an interface or bean typename- the name qualifier of a specific bean- Throws:
NoSuchElementException- When no matching bean is found
-
get
Return a single bean given the full generic type.- Parameters:
type- The generic type- Throws:
NoSuchElementException- When no matching bean is found
-
get
Return a single bean given the full generic type and name.- Parameters:
type- The generic typename- the name qualifier of a specific bean- Throws:
NoSuchElementException- When no matching bean is found
-
getOptional
Optionally return a single bean given the type and empty if it is not found.- Parameters:
type- an interface or bean type
-
getOptional
Optionally return a single bean given the type and name and empty if it is not found.- Parameters:
type- an interface or bean typename- the name qualifier of a specific bean
-
listByAnnotation
Return the list of beans that have an annotation. The annotation must have a @Retention policy of RUNTIME// e.g. register all controllers with web a framework // .. where Controller is an annotation on the beans List<Object> controllers = beanScope.listByAnnotation(Controller.class);- Parameters:
annotation- An annotation class.
-
list
-
list
-
list
-
listByPriority
Return the list of beans that implement the class sorting by priority. -
listByPriority
Return the list of beans that implement the type sorting by priority. -
map
-
all
-
contains
-
contains
-
close
void close()Close the scope firing any@PreDestroylifecycle methods.- Specified by:
closein interfaceAutoCloseable
-
customScopeAnnotations
Return the custom scope annotations contained in this bean scope.
-