Interface BeanContext
-
- All Superinterfaces:
AutoCloseable,Closeable
public interface BeanContext extends Closeable
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 BeanContext
We can programmatically create a BeanContext via BeanContextBuilder.
// create a BeanContext ... try (BeanContext context = new BeanContextBuilder() .build()) { CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class); coffeeMaker.makeIt() }Implicitly used
The BeanContext is implicitly used by SystemContext. It will be created as needed and a shutdown hook will close the underlying BeanContext on JVM shutdown.
// BeanContext created as needed under the hood CoffeeMaker coffeeMaker = SystemContext.getBean(CoffeeMaker.class); coffeeMaker.brew();
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> BeanEntry<T>candidate(Class<T> type, String name)Return the wiring candidate bean with name and priority.voidclose()Close the context firing any@PreDestroymethods.<T> TgetBean(Class<T> type)Return a single bean given the type.<T> TgetBean(Class<T> type, String name)Return a single bean given the type and name.<T> List<T>getBeans(Class<T> interfaceType)Return the list of beans that implement the interface.<T> List<T>getBeansByPriority(Class<T> interfaceType)Return the list of beans that implement the interface sorting by priority.<T> List<T>getBeansByPriority(Class<T> interfaceType, Class<? extends Annotation> priority)Return the beans that implement the interface sorting by the priority annotation used.List<Object>getBeansWithAnnotation(Class<?> annotation)Return the list of beans that have an annotation.String[]getDependsOn()Return the names of modules this bean context depends on.StringgetName()Return the module name of the bean context.String[]getProvides()Return the names of module features this bean context provides.static BeanContextBuildernewBuilder()Build a bean context with options for shutdown hook and supplying test doubles.<T> List<T>sortByPriority(List<T> list)Sort the beans by javax.annotation.Priority annotation.<T> List<T>sortByPriority(List<T> list, Class<? extends Annotation> priority)Sort the beans using the given Priority annotation.voidstart()Start the context firing any@PostConstructmethods.
-
-
-
Method Detail
-
newBuilder
static BeanContextBuilder newBuilder()
Build a bean context with options for shutdown hook and supplying test doubles.We would choose to use BeanContextBuilder in test code (for component testing) as it gives us the ability to inject test doubles, mocks, spy's etc.
@Test public void someComponentTest() { MyRedisApi mockRedis = mock(MyRedisApi.class); MyDbApi mockDatabase = mock(MyDbApi.class); try (BeanContext context = BeanContext.newBuilder() .withBeans(mockRedis, mockDatabase) .build()) { // built with test doubles injected ... CoffeeMaker coffeeMaker = context.getBean(CoffeeMaker.class); coffeeMaker.makeIt(); assertThat(... } }
-
getName
String getName()
Return the module name of the bean context.- See Also:
ContextModule
-
getProvides
String[] getProvides()
Return the names of module features this bean context provides.- See Also:
ContextModule
-
getDependsOn
String[] getDependsOn()
Return the names of modules this bean context depends on.- See Also:
ContextModule
-
getBean
<T> T getBean(Class<T> type)
Return a single bean given the type.CoffeeMaker coffeeMaker = beanContext.getBean(CoffeeMaker.class); coffeeMaker.brew();- Parameters:
type- an interface or bean type
-
getBean
<T> T getBean(Class<T> type, String name)
Return a single bean given the type and name.Heater heater = beanContext.getBean(Heater.class, "electric"); heater.heat();- Parameters:
type- an interface or bean typename- the name qualifier of a specific bean
-
candidate
<T> BeanEntry<T> candidate(Class<T> type, String name)
Return the wiring candidate bean with name and priority.
-
getBeansWithAnnotation
List<Object> getBeansWithAnnotation(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 = SystemContext.getBeansWithAnnotation(Controller.class);The classic use case for this is registering controllers or routes to web frameworks like Sparkjava, Javlin, Rapidoid etc.
- Parameters:
annotation- An annotation class.
-
getBeans
<T> List<T> getBeans(Class<T> interfaceType)
Return the list of beans that implement the interface.// e.g. register all routes for a web framework List<WebRoute> routes = SystemContext.getBeans(WebRoute.class);- Parameters:
interfaceType- An interface class.
-
getBeansByPriority
<T> List<T> getBeansByPriority(Class<T> interfaceType)
Return the list of beans that implement the interface sorting by priority.
-
getBeansByPriority
<T> List<T> getBeansByPriority(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
-
sortByPriority
<T> List<T> sortByPriority(List<T> list)
Sort the beans by javax.annotation.Priority annotation.- Parameters:
list- The beans to sort by priority- Returns:
- A new list of beans sorted by priority
-
sortByPriority
<T> List<T> sortByPriority(List<T> list, Class<? extends Annotation> priority)
Sort the beans using the given Priority annotation.The priority annotation will typically be either
javax.annotation.Priorityorjakarta.annotation.Priority.- Parameters:
list- The beans to sort by prioritypriority- The priority annotation used to sort the beans- Returns:
- A new list of beans sorted by priority
-
start
void start()
Start the context firing any@PostConstructmethods.
-
close
void close()
Close the context firing any@PreDestroymethods.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
-