Interface BeanContext
- All Superinterfaces:
AnnotationMetadataResolver
,ApplicationEventPublisher<Object>
,AttributeHolder
,AutoCloseable
,BeanDefinitionRegistry
,BeanLocator
,Closeable
,ConversionServiceProvider
,ExecutionHandleLocator
,LifeCycle<BeanContext>
,MutableAttributeHolder
- All Known Subinterfaces:
ApplicationContext
,InitializableBeanContext
- All Known Implementing Classes:
DefaultApplicationContext
,DefaultBeanContext
The core BeanContext abstraction which allows for dependency injection of classes annotated with
Inject
.
Apart of the standard jakarta.inject
annotations for dependency injection, additional annotations within
the io.micronaut.context.annotation
package allow control over configuration of the bean context.
- Since:
- 1.0
- Author:
- Graeme Rocher
-
Field Summary
Fields inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
DEFAULT
Fields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OP
Fields inherited from interface io.micronaut.context.ExecutionHandleLocator
EMPTY
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull BeanContext
build()
Build aBeanContext
.static @NonNull BeanContext
build
(ClassLoader classLoader) Build aBeanContext
.default <T> T
createBean
(@NonNull Class<T> beanType) Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
createBean
(@NonNull Class<T> beanType, @Nullable Qualifier<T> qualifier) Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
createBean
(@NonNull Class<T> beanType, @Nullable Qualifier<T> qualifier, @Nullable Map<String, Object> argumentValues) Creates a new instance of the given bean performing dependency injection and returning a new instance.default <T> T
createBean
(@NonNull Class<T> beanType, @Nullable Object... args) Creates a new instance of the given bean performing dependency injection and returning a new instance.default <T> T
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> void
destroyBean
(@NonNull BeanRegistration<T> beanRegistration) Destroys the given bean.default <T> T
destroyBean
(@NonNull Argument<T> beanType) Destroys the bean for the given type causing it to be re-created.<T> T
destroyBean
(@NonNull Argument<T> beanType, @Nullable Qualifier<T> qualifier) Destroys the bean for the given type causing it to be re-created.<T> T
destroyBean
(@NonNull Class<T> beanType) Destroys the bean for the given type causing it to be re-created.<T> T
destroyBean
(T bean) Destroys the given bean.Obtains the configuration for this context.default <E> @NonNull ApplicationEventPublisher<E>
getEventPublisher
(@NonNull Class<E> eventType) Obtain anApplicationEventPublisher
for the given even type.<T> T
inject
(T instance) Inject an existing instance.<T> void
refreshBean
(@NonNull BeanRegistration<T> beanRegistration) Refresh the state of the given registered bean applying dependency injection and configuration wiring again.refreshBean
(@Nullable BeanIdentifier identifier) Refresh the state of the given registered bean applying dependency injection and configuration wiring again.<T> @NonNull BeanContext
registerSingleton
(@NonNull Class<T> type, T singleton, @Nullable Qualifier<T> qualifier, boolean inject) Registers a new singleton bean at runtime.default BeanContext
registerSingleton
(@NonNull Object singleton) Registers a new singleton bean at runtime.default @NonNull BeanContext
registerSingleton
(@NonNull Object singleton, boolean inject) Registers a new singleton bean at runtime.default <T> BeanContext
registerSingleton
(Class<T> type, T singleton) Registers a new singleton bean at runtime.default <T> BeanContext
registerSingleton
(Class<T> type, T singleton, Qualifier<T> qualifier) Registers a new singleton bean at runtime.static @NonNull BeanContext
run()
Run theBeanContext
.static @NonNull BeanContext
run
(ClassLoader classLoader) Run theBeanContext
.Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadata
Methods inherited from interface io.micronaut.context.event.ApplicationEventPublisher
isEmpty, publishEvent, publishEventAsync
Methods inherited from interface io.micronaut.core.attr.AttributeHolder
getAttribute, getAttribute
Methods inherited from interface io.micronaut.context.BeanDefinitionRegistry
containsBean, containsBean, containsBean, containsBean, findBeanConfiguration, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanDefinition, findBeanRegistration, findProxyBeanDefinition, findProxyBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, getActiveBeanRegistrations, getActiveBeanRegistrations, getAllBeanDefinitions, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinition, getBeanDefinitionReferences, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistration, getBeanRegistration, getBeanRegistration, getBeanRegistrations, getBeanRegistrations, getBeanRegistrations, getDisabledBeans, getProxyTargetBeanDefinition, getProxyTargetBeanDefinition, registerBeanDefinition
Methods inherited from interface io.micronaut.context.BeanLocator
findBean, findBean, findBean, findBean, findOrInstantiateBean, getBean, getBean, getBean, getBean, getBean, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getProxyTargetBean, getProxyTargetBean, mapOfType, mapOfType, mapOfType, mapOfType, streamOfType, streamOfType, streamOfType, streamOfType
Methods inherited from interface io.micronaut.core.convert.ConversionServiceProvider
getConversionService
Methods inherited from interface io.micronaut.context.ExecutionHandleLocator
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethod
Methods inherited from interface io.micronaut.context.LifeCycle
close, isRunning, refresh, start, stop
Methods inherited from interface io.micronaut.core.attr.MutableAttributeHolder
getAttributes, removeAttribute, setAttribute
-
Method Details
-
getContextConfiguration
Obtains the configuration for this context.- Returns:
- The
BeanContextConfiguration
- Since:
- 3.0.0
-
getEventPublisher
@NonNull default <E> @NonNull ApplicationEventPublisher<E> getEventPublisher(@NonNull @NonNull Class<E> eventType) Obtain anApplicationEventPublisher
for the given even type.- Type Parameters:
E
- The event generic type- Parameters:
eventType
- The event type- Returns:
- The event publisher, never
null
-
inject
Inject an existing instance.- Type Parameters:
T
- The bean generic type- Parameters:
instance
- The instance to inject- Returns:
- The instance to inject
-
createBean
Creates a new instance of the given bean performing dependency injection and returning a new instance.Note that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean type- Returns:
- The instance
-
createBean
@NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier) Creates a new instance of the given bean performing dependency injection and returning a new instance.Note that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean typequalifier
- The qualifier- Returns:
- The instance
-
createBean
@NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier, @Nullable @Nullable Map<String, Object> argumentValues) Creates a new instance of the given bean performing dependency injection and returning a new instance.
If the bean defines any
Parameter
values then the values passed in theargumentValues
parameter will be usedNote that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean typequalifier
- The qualifierargumentValues
- The argument values- Returns:
- The instance
-
createBean
@NonNull <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Qualifier<T> qualifier, @Nullable @Nullable Object... args) Creates a new instance of the given bean performing dependency injection and returning a new instance.
If the bean defines any
Parameter
values then the values passed in theargumentValues
parameter will be usedNote that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean typequalifier
- The qualifierargs
- The argument values- Returns:
- The instance
-
createBean
@NonNull default <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Object... args) Creates a new instance of the given bean performing dependency injection and returning a new instance.
If the bean defines any
Parameter
values then the values passed in theargumentValues
parameter will be usedNote that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean typeargs
- The argument values- Returns:
- The instance
-
createBean
@NonNull default <T> T createBean(@NonNull @NonNull Class<T> beanType, @Nullable @Nullable Map<String, Object> argumentValues) Creates a new instance of the given bean performing dependency injection and returning a new instance.
If the bean defines any
Parameter
values then the values passed in theargumentValues
parameter will be usedNote that the instance returned is not saved as a singleton in the context.
- Type Parameters:
T
- The bean generic type- Parameters:
beanType
- The bean typeargumentValues
- The argument values- Returns:
- The instance
-
destroyBean
Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.- Type Parameters:
T
- The concrete class- Parameters:
beanType
- The bean type- Returns:
- The destroy instance or null if no such bean exists
-
destroyBean
Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.- Type Parameters:
T
- The concrete class- Parameters:
beanType
- The bean type- Returns:
- The destroy instance or null if no such bean exists
- Since:
- 3.0.0
-
destroyBean
@Nullable <T> T destroyBean(@NonNull @NonNull Argument<T> beanType, @Nullable @Nullable Qualifier<T> qualifier) Destroys the bean for the given type causing it to be re-created. If a singleton has been loaded it will be destroyed and removed from the context, otherwise null will be returned.- Type Parameters:
T
- The concrete class- Parameters:
beanType
- The bean typequalifier
- The qualifier- Returns:
- The destroy instance or null if no such bean exists
- Since:
- 3.0.0
-
destroyBean
Destroys the given bean.- Type Parameters:
T
- The concrete class- Parameters:
bean
- The bean- Returns:
- The destroy instance
- Since:
- 3.0.0
-
destroyBean
Destroys the given bean.- Type Parameters:
T
- The bean type- Parameters:
beanRegistration
- The bean registration- Since:
- 3.5.0
-
refreshBean
Refresh the state of the given registered bean applying dependency injection and configuration wiring again.
Note that if the bean was produced by a
Factory
then this method will refresh the factory too- Type Parameters:
T
- The concrete class- Parameters:
identifier
- TheBeanIdentifier
- Returns:
- An
Optional
of the instance if it exists for the given registration
-
refreshBean
Refresh the state of the given registered bean applying dependency injection and configuration wiring again.
Note that if the bean was produced by a
This methods skips an additional resolution of theFactory
then this method will refresh the factory tooBeanRegistration
.- Type Parameters:
T
- The concrete class- Parameters:
beanRegistration
- TheBeanRegistration
- Since:
- 3.5.0
-
getClassLoader
- Returns:
- The class loader used by this context
-
getBeanValidator
- Returns:
- Get the configured bean validator, if any.
-
registerSingleton
@NonNull <T> @NonNull BeanContext registerSingleton(@NonNull @NonNull Class<T> type, @NonNull T singleton, @Nullable @Nullable Qualifier<T> qualifier, boolean inject) Description copied from interface:BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by invoking any
PostConstruct
hooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifierinject
- Whether the singleton should be injected (defaults to true)- Returns:
- This bean context
-
registerSingleton
Description copied from interface:BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by invoking any
PostConstruct
hooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Parameters:
singleton
- The singleton bean- Returns:
- This bean context
-
registerSingleton
Description copied from interface:BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by invoking any
PostConstruct
hooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- The bean typesingleton
- The singleton beanqualifier
- The bean qualifier- Returns:
- This bean context
-
registerSingleton
Description copied from interface:BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by invoking any
PostConstruct
hooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Type Parameters:
T
- The concrete type- Parameters:
type
- the bean typesingleton
- The singleton bean- Returns:
- This bean context
-
registerSingleton
@NonNull default @NonNull BeanContext registerSingleton(@NonNull @NonNull Object singleton, boolean inject) Description copied from interface:BeanDefinitionRegistry
Registers a new singleton bean at runtime. This method expects that the bean definition data will have been compiled ahead of time.
If bean definition data is found the method will perform dependency injection on the instance followed by invoking any
PostConstruct
hooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Parameters:
singleton
- The singleton beaninject
- Whether the singleton should be injected (defaults to true)- Returns:
- This bean context
-
run
- Returns:
- The running
BeanContext
-
build
Build aBeanContext
.- Returns:
- The built, but not yet running
BeanContext
-
run
- Parameters:
classLoader
- The classloader to use- Returns:
- The running
BeanContext
-
build
Build aBeanContext
.- Parameters:
classLoader
- The classloader to use- Returns:
- The built, but not yet running
BeanContext
-