Package io.avaje.inject
Interface BeanScope
-
- All Superinterfaces:
AutoCloseable
public interface BeanScope extends 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.newBuilder().// create a BeanScope ... try (BeanScope scope = BeanScope.newBuilder() .build()) { CoffeeMaker coffeeMaker = context.get(CoffeeMaker.class); coffeeMaker.makeIt() }External dependencies
We can supporting external dependencies when creating the BeanScope. We need to do 2 things. we need to specify these via
-
1. Specify the external dependency via
@InjectModule(requires=...). Otherwise at compile time the annotation processor detects it as a missing dependency and we can't compile. - 2. Provide the dependency when creating the BeanScope
For example, given we have Pump as an externally provided dependency.
{@code // tell the annotation processor Pump is provided externally // otherwise it thinks we have a missing dependency
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description List<BeanEntry>all()Return all the bean entries from the scope.voidclose()Close the scope firing any@PreDestroylifecycle methods.<T> Tget(Class<T> type)Return a single bean given the type.<T> Tget(Class<T> type, String name)Return a single bean given the type and name.<T> Tget(Type type, String name)Return a single bean given the generic type and name.<T> List<T>list(Class<T> interfaceType)Return the list of beans that implement the interface.List<Object>listByAnnotation(Class<?> annotation)Return the list of beans that have an annotation.<T> List<T>listByPriority(Class<T> interfaceType)Return the list of beans that implement the interface sorting by priority.<T> List<T>listByPriority(Class<T> interfaceType, Class<? extends Annotation> priority)Return the beans that implement the interface sorting by the priority annotation used.static BeanScopeBuildernewBuilder()Build a bean scope with options for shutdown hook and supplying external dependencies.
-
-
-
Method Detail
-
newBuilder
static BeanScopeBuilder newBuilder()
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.newBuilder() .build()) { CoffeeMaker coffeeMaker = context.get(CoffeeMaker.class); coffeeMaker.makeIt() }
-
get
<T> T get(Class<T> type)
Return a single bean given the type.CoffeeMaker coffeeMaker = beanScope.get(CoffeeMaker.class); coffeeMaker.brew();- Parameters:
type- an interface or bean type
-
get
<T> T get(Class<T> type, String name)
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
-
get
<T> T get(Type type, String name)
Return a single bean given the generic type and name.- Parameters:
type- The generic typename- the name qualifier of a specific bean
-
listByAnnotation
List<Object> listByAnnotation(Class<?> annotation)
Return the list of beans that have an annotation.// 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
<T> List<T> list(Class<T> interfaceType)
Return the list of beans that implement the interface.// e.g. register all routes for a web framework List<WebRoute> routes = beanScope.list(WebRoute.class);- Parameters:
interfaceType- An interface class.
-
listByPriority
<T> List<T> listByPriority(Class<T> interfaceType)
Return the list of beans that implement the interface sorting by priority.
-
listByPriority
<T> List<T> listByPriority(Class<T> interfaceType, Class<? extends Annotation> priority)
Return the beans that implement the interface sorting by the priority annotation used.The priority annotation will typically be either
javax.annotation.Priorityorjakarta.annotation.Priority.- Parameters:
interfaceType- The interface type of the beans to returnpriority- The priority annotation used to sort the beans
-
all
List<BeanEntry> all()
Return all the bean entries from the scope.The bean entries include entries from the parent scope if it has one.
- Returns:
- All bean entries from the scope.
-
close
void close()
Close the scope firing any@PreDestroylifecycle methods.- Specified by:
closein interfaceAutoCloseable
-
-