Interface BeanContext
-
- All Superinterfaces:
io.micronaut.core.annotation.AnnotationMetadataResolver
,ApplicationEventPublisher<java.lang.Object>
,io.micronaut.core.attr.AttributeHolder
,java.lang.AutoCloseable
,BeanDefinitionRegistry
,BeanLocator
,java.io.Closeable
,ExecutionHandleLocator
,LifeCycle<BeanContext>
,io.micronaut.core.attr.MutableAttributeHolder
- All Known Subinterfaces:
ApplicationContext
,InitializableBeanContext
- All Known Implementing Classes:
DefaultApplicationContext
,DefaultBeanContext
public interface BeanContext extends LifeCycle<BeanContext>, ExecutionHandleLocator, BeanLocator, BeanDefinitionRegistry, ApplicationEventPublisher<java.lang.Object>, io.micronaut.core.annotation.AnnotationMetadataResolver, io.micronaut.core.attr.MutableAttributeHolder
The core BeanContext abstraction which allows for dependency injection of classes annotated with
Inject
.Apart of the standard
javax.inject
annotations for dependency injection, additional annotations within theio.micronaut.context.annotation
package allow control over configuration of the bean context.- Since:
- 1.0
-
-
Field Summary
-
Fields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OP
-
Fields inherited from interface io.micronaut.context.ExecutionHandleLocator
EMPTY
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description static BeanContext
build()
Build aBeanContext
.static BeanContext
build(java.lang.ClassLoader classLoader)
Build aBeanContext
.default <T> T
createBean(java.lang.Class<T> beanType)
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
createBean(java.lang.Class<T> beanType, Qualifier<T> qualifier)
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
createBean(java.lang.Class<T> beanType, Qualifier<T> qualifier, java.lang.Object... args)
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> T
createBean(java.lang.Class<T> beanType, Qualifier<T> qualifier, java.util.Map<java.lang.String,java.lang.Object> argumentValues)
Creates a new instance of the given bean performing dependency injection and returning a new instance.default <T> T
createBean(java.lang.Class<T> beanType, java.lang.Object... args)
Creates a new instance of the given bean performing dependency injection and returning a new instance.default <T> T
createBean(java.lang.Class<T> beanType, java.util.Map<java.lang.String,java.lang.Object> argumentValues)
Creates a new instance of the given bean performing dependency injection and returning a new instance.<T> void
destroyBean(BeanRegistration<T> beanRegistration)
Destroys the given bean.default <T> T
destroyBean(io.micronaut.core.type.Argument<T> beanType)
Destroys the bean for the given type causing it to be re-created.<T> T
destroyBean(io.micronaut.core.type.Argument<T> beanType, Qualifier<T> qualifier)
Destroys the bean for the given type causing it to be re-created.<T> T
destroyBean(java.lang.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.BeanDefinitionValidator
getBeanValidator()
java.lang.ClassLoader
getClassLoader()
BeanContextConfiguration
getContextConfiguration()
Obtains the configuration for this context.default <E> ApplicationEventPublisher<E>
getEventPublisher(java.lang.Class<E> eventType)
Obtain anApplicationEventPublisher
for the given even type.<T> T
inject(T instance)
Inject an existing instance.void
publishEvent(java.lang.Object event)
Deprecated.Preferred way is to use event typedApplicationEventPublisher<MyEventType>
default java.util.concurrent.Future<java.lang.Void>
publishEventAsync(java.lang.Object event)
Deprecated.Preferred way is to use event typedApplicationEventPublisher<MyEventType>
<T> void
refreshBean(BeanRegistration<T> beanRegistration)
Refresh the state of the given registered bean applying dependency injection and configuration wiring again.<T> java.util.Optional<T>
refreshBean(BeanIdentifier identifier)
Refresh the state of the given registered bean applying dependency injection and configuration wiring again.default <T> BeanContext
registerSingleton(java.lang.Class<T> type, T singleton)
Registers a new singleton bean at runtime.default <T> BeanContext
registerSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier)
Registers a new singleton bean at runtime.<T> BeanContext
registerSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier, boolean inject)
Registers a new singleton bean at runtime.default BeanContext
registerSingleton(java.lang.Object singleton)
Registers a new singleton bean at runtime.default BeanContext
registerSingleton(java.lang.Object singleton, boolean inject)
Registers a new singleton bean at runtime.static BeanContext
run()
Run theBeanContext
.static BeanContext
run(java.lang.ClassLoader classLoader)
Run theBeanContext
.-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadata
-
Methods inherited from interface io.micronaut.context.BeanDefinitionRegistry
containsBean, containsBean, containsBean, containsBean, findBeanConfiguration, 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, 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, streamOfType, streamOfType, streamOfType, streamOfType
-
Methods inherited from interface io.micronaut.context.ExecutionHandleLocator
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethod
-
-
-
-
Method Detail
-
getContextConfiguration
@NonNull BeanContextConfiguration getContextConfiguration()
Obtains the configuration for this context.- Returns:
- The
BeanContextConfiguration
- Since:
- 3.0.0
-
getEventPublisher
@NonNull default <E> ApplicationEventPublisher<E> getEventPublisher(@NonNull java.lang.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
-
publishEvent
@Deprecated void publishEvent(java.lang.Object event)
Deprecated.Preferred way is to use event typedApplicationEventPublisher<MyEventType>
Publish the given event. The event will be published synchronously and only return once all listeners have consumed the event.- Specified by:
publishEvent
in interfaceApplicationEventPublisher<java.lang.Object>
- Parameters:
event
- The event to publish
-
publishEventAsync
@Deprecated default java.util.concurrent.Future<java.lang.Void> publishEventAsync(java.lang.Object event)
Deprecated.Preferred way is to use event typedApplicationEventPublisher<MyEventType>
Publish the given event. The event will be published asynchronously. A future is returned that can be used to check whether the event completed successfully or not.- Specified by:
publishEventAsync
in interfaceApplicationEventPublisher<java.lang.Object>
- Parameters:
event
- The event to publish- Returns:
- A future that completes when the event is published
-
inject
@NonNull <T> T inject(@NonNull T instance)
Inject an existing instance.- Type Parameters:
T
- The bean generic type- Parameters:
instance
- The instance to inject- Returns:
- The instance to inject
-
createBean
@NonNull default <T> T createBean(@NonNull java.lang.Class<T> beanType)
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 java.lang.Class<T> beanType, @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 java.lang.Class<T> beanType, @Nullable Qualifier<T> qualifier, @Nullable java.util.Map<java.lang.String,java.lang.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 java.lang.Class<T> beanType, @Nullable Qualifier<T> qualifier, @Nullable java.lang.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 java.lang.Class<T> beanType, @Nullable java.lang.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 java.lang.Class<T> beanType, @Nullable java.util.Map<java.lang.String,java.lang.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
@Nullable <T> T destroyBean(@NonNull java.lang.Class<T> beanType)
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
@Nullable default <T> T destroyBean(@NonNull io.micronaut.core.type.Argument<T> beanType)
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 io.micronaut.core.type.Argument<T> beanType, @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
@NonNull <T> T destroyBean(@NonNull T bean)
Destroys the given bean.- Type Parameters:
T
- The concrete class- Parameters:
bean
- The bean- Returns:
- The destroy instance
- Since:
- 3.0.0
-
destroyBean
@NonNull <T> void destroyBean(@NonNull BeanRegistration<T> beanRegistration)
Destroys the given bean.- Type Parameters:
T
- The bean type- Parameters:
beanRegistration
- The bean registration- Since:
- 3.5.0
-
refreshBean
@NonNull <T> java.util.Optional<T> refreshBean(@Nullable BeanIdentifier identifier)
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
@NonNull <T> void refreshBean(@NonNull BeanRegistration<T> beanRegistration)
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
@NonNull java.lang.ClassLoader getClassLoader()
- Returns:
- The class loader used by this context
-
getBeanValidator
@NonNull BeanDefinitionValidator getBeanValidator()
- Returns:
- Get the configured bean validator, if any.
-
registerSingleton
@NonNull <T> BeanContext registerSingleton(@NonNull java.lang.Class<T> type, @NonNull T singleton, @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
default BeanContext registerSingleton(@NonNull java.lang.Object singleton)
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
default <T> BeanContext registerSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier)
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
default <T> BeanContext registerSingleton(java.lang.Class<T> type, T singleton)
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 BeanContext registerSingleton(@NonNull java.lang.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
@NonNull static BeanContext run()
- Returns:
- The running
BeanContext
-
build
@NonNull static BeanContext build()
Build aBeanContext
.- Returns:
- The built, but not yet running
BeanContext
-
run
@NonNull static BeanContext run(java.lang.ClassLoader classLoader)
- Parameters:
classLoader
- The classloader to use- Returns:
- The running
BeanContext
-
build
@NonNull static BeanContext build(java.lang.ClassLoader classLoader)
Build aBeanContext
.- Parameters:
classLoader
- The classloader to use- Returns:
- The built, but not yet running
BeanContext
-
-