Package io.micronaut.context
Class DefaultApplicationContext
- java.lang.Object
-
- io.micronaut.context.DefaultBeanContext
-
- io.micronaut.context.DefaultApplicationContext
-
- All Implemented Interfaces:
ApplicationContext
,BeanContext
,BeanDefinitionRegistry
,BeanLocator
,PropertyPlaceholderResolver
,ApplicationEventPublisher<java.lang.Object>
,ExecutionHandleLocator
,InitializableBeanContext
,LifeCycle<BeanContext>
,io.micronaut.core.annotation.AnnotationMetadataResolver
,io.micronaut.core.attr.AttributeHolder
,io.micronaut.core.attr.MutableAttributeHolder
,io.micronaut.core.value.PropertyResolver
,io.micronaut.core.value.ValueResolver<java.lang.String>
,java.io.Closeable
,java.lang.AutoCloseable
public class DefaultApplicationContext extends DefaultBeanContext implements ApplicationContext
Creates a default implementation of theApplicationContext
interface.- Since:
- 1.0
-
-
Field Summary
-
Fields inherited from class io.micronaut.context.DefaultBeanContext
initializing, LOG, LOG_LIFECYCLE, running, terminating
-
Fields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OP
-
Fields inherited from interface io.micronaut.context.ExecutionHandleLocator
EMPTY
-
-
Constructor Summary
Constructors Constructor Description DefaultApplicationContext(ApplicationContextConfiguration configuration)
Construct a new ApplicationContext for the given environment name and classloader.DefaultApplicationContext(io.micronaut.core.io.scan.ClassPathResourceLoader resourceLoader, java.lang.String... environmentNames)
Construct a new ApplicationContext for the given environment name and classloader.DefaultApplicationContext(java.lang.String... environmentNames)
Construct a new ApplicationContext for the given environment name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
containsProperties(java.lang.String name)
boolean
containsProperty(java.lang.String name)
protected io.micronaut.core.convert.ConversionService
createConversionService()
Creates the default conversion service.protected Environment
createEnvironment(ApplicationContextConfiguration configuration)
Creates the default environment for the given environment name.protected <T> java.util.Collection<BeanDefinition<T>>
findBeanCandidates(BeanResolutionContext resolutionContext, io.micronaut.core.type.Argument<T> beanType, boolean filterProxied, java.util.function.Predicate<BeanDefinition<T>> predicate)
Find bean candidates for the given type.protected <T> java.util.Collection<BeanDefinition<T>>
findBeanCandidates(BeanResolutionContext resolutionContext, io.micronaut.core.type.Argument<T> beanType, BeanDefinition<?> filter, boolean filterProxied)
Find bean candidates for the given type.protected <T> BeanDefinition<T>
findConcreteCandidate(java.lang.Class<T> beanType, Qualifier<T> qualifier, java.util.Collection<BeanDefinition<T>> candidates)
Fall back method to attempt to find a candidate for the given definitions.io.micronaut.core.convert.ConversionService<?>
getConversionService()
Environment
getEnvironment()
java.util.Map<java.lang.String,java.lang.Object>
getProperties(java.lang.String name, io.micronaut.core.naming.conventions.StringConvention keyFormat)
<T> java.util.Optional<T>
getProperty(java.lang.String name, io.micronaut.core.convert.ArgumentConversionContext<T> conversionContext)
java.util.Collection<java.lang.String>
getPropertyEntries(java.lang.String name)
protected void
initializeContext(java.util.List<BeanDefinitionReference> contextScopeBeans, java.util.List<BeanDefinitionReference> processedBeans, java.util.List<BeanDefinitionReference> parallelBeans)
Initialize the context with the givenContext
scope beans.protected void
initializeTypeConverters(BeanContext beanContext)
protected void
registerConfiguration(BeanConfiguration configuration)
Registers an active configuration.<T> ApplicationContext
registerSingleton(java.lang.Class<T> type, T singleton, Qualifier<T> qualifier, boolean inject)
Registers a new singleton bean at runtime.java.util.Optional<java.lang.String>
resolvePlaceholders(java.lang.String str)
Resolve the placeholders and return an Optional String if it was possible to resolve them.java.lang.String
resolveRequiredPlaceholders(java.lang.String str)
Resolve the placeholders and return an Optional String if it was possible to resolve them.ApplicationContext
start()
The start method will read all bean definition classes found on the classpath and initialize any pre-required state.protected void
startEnvironment()
Start the environment.ApplicationContext
stop()
The close method will shut down the context callingPreDestroy
hooks on loaded singletons.protected <T> java.util.Collection<BeanDefinition<T>>
transformIterables(BeanResolutionContext resolutionContext, java.util.Collection<BeanDefinition<T>> candidates, boolean filterProxied)
Method that transforms iterable candidates if possible.-
Methods inherited from class io.micronaut.context.DefaultBeanContext
containsBean, containsBean, createBean, createBean, createBean, createBean, createCustomScopeRegistry, createExecutionHandle, destroyBean, destroyBean, destroyBean, destroyBean, doCreateBean, doCreateBean, doCreateBean, finalizeConfiguration, findBean, findBean, findBean, findBean, findBeanCandidates, findBeanCandidatesForInstance, findBeanConfiguration, findBeanDefinition, findBeanDefinition, findBeanRegistration, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyBeanDefinition, findProxyBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetBeanDefinition, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getActiveBeanRegistration, getActiveBeanRegistrations, getActiveBeanRegistrations, getAllBeanDefinitions, getAttribute, getAttribute, getAttributes, getBean, getBean, getBean, getBean, getBean, getBean, getBean, getBean, getBeanDefinition, getBeanDefinitionReferences, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanDefinitions, getBeanRegistration, getBeanRegistration, getBeanRegistration, getBeanRegistration, getBeanRegistrations, getBeanRegistrations, getBeanRegistrations, getBeanRegistrations, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getBeanValidator, getClassLoader, getContextConfiguration, getProxyTargetBean, getProxyTargetBean, getProxyTargetBean, initializeEventListeners, inject, inject, invalidateCaches, isRunning, processParallelBeans, publishEvent, publishEventAsync, refreshBean, refreshBean, registerBeanDefinition, removeAttribute, resolveBeanConfigurations, resolveBeanDefinitionReferences, resolveBeanDefinitionReferences, resolveMetadata, setAttribute, streamOfType, streamOfType, streamOfType, streamOfType
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadata
-
Methods inherited from interface io.micronaut.context.ApplicationContext
environment, registerSingleton, registerSingleton, registerSingleton, registerSingleton
-
Methods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, publishEvent, publishEventAsync, refreshBean, refreshBean
-
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
-
Methods inherited from interface io.micronaut.core.attr.MutableAttributeHolder
getAttributes, removeAttribute, setAttribute
-
Methods inherited from interface io.micronaut.context.env.PropertyPlaceholderResolver
getPrefix, resolveRequiredPlaceholder
-
-
-
-
Constructor Detail
-
DefaultApplicationContext
public DefaultApplicationContext(@NonNull java.lang.String... environmentNames)
Construct a new ApplicationContext for the given environment name.- Parameters:
environmentNames
- The environment names
-
DefaultApplicationContext
public DefaultApplicationContext(@NonNull io.micronaut.core.io.scan.ClassPathResourceLoader resourceLoader, @NonNull java.lang.String... environmentNames)
Construct a new ApplicationContext for the given environment name and classloader.- Parameters:
environmentNames
- The environment namesresourceLoader
- The class loader
-
DefaultApplicationContext
public DefaultApplicationContext(@NonNull ApplicationContextConfiguration configuration)
Construct a new ApplicationContext for the given environment name and classloader.- Parameters:
configuration
- The application context configuration
-
-
Method Detail
-
registerSingleton
@NonNull public <T> ApplicationContext 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 interfaceApplicationContext
- Specified by:
registerSingleton
in interfaceBeanContext
- Specified by:
registerSingleton
in interfaceBeanDefinitionRegistry
- Overrides:
registerSingleton
in classDefaultBeanContext
- 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
-
createEnvironment
@NonNull protected Environment createEnvironment(@NonNull ApplicationContextConfiguration configuration)
Creates the default environment for the given environment name.- Parameters:
configuration
- The application context configuration- Returns:
- The environment instance
-
createConversionService
@NonNull protected io.micronaut.core.convert.ConversionService createConversionService()
Creates the default conversion service.- Returns:
- The conversion service
-
getConversionService
@NonNull public io.micronaut.core.convert.ConversionService<?> getConversionService()
- Specified by:
getConversionService
in interfaceApplicationContext
- Returns:
- The default conversion service
-
getEnvironment
@NonNull public Environment getEnvironment()
- Specified by:
getEnvironment
in interfaceApplicationContext
- Returns:
- The application environment
-
start
@NonNull public ApplicationContext start()
Description copied from class:DefaultBeanContext
The start method will read all bean definition classes found on the classpath and initialize any pre-required state.- Specified by:
start
in interfaceApplicationContext
- Specified by:
start
in interfaceLifeCycle<BeanContext>
- Overrides:
start
in classDefaultBeanContext
- Returns:
- This lifecycle component
-
stop
@NonNull public ApplicationContext stop()
Description copied from class:DefaultBeanContext
The close method will shut down the context callingPreDestroy
hooks on loaded singletons.- Specified by:
stop
in interfaceApplicationContext
- Specified by:
stop
in interfaceLifeCycle<BeanContext>
- Overrides:
stop
in classDefaultBeanContext
- Returns:
- This lifecycle component
-
containsProperty
public boolean containsProperty(java.lang.String name)
- Specified by:
containsProperty
in interfaceio.micronaut.core.value.PropertyResolver
-
containsProperties
public boolean containsProperties(java.lang.String name)
- Specified by:
containsProperties
in interfaceio.micronaut.core.value.PropertyResolver
-
getProperty
public <T> java.util.Optional<T> getProperty(java.lang.String name, io.micronaut.core.convert.ArgumentConversionContext<T> conversionContext)
- Specified by:
getProperty
in interfaceio.micronaut.core.value.PropertyResolver
-
getPropertyEntries
@NonNull public java.util.Collection<java.lang.String> getPropertyEntries(@NonNull java.lang.String name)
- Specified by:
getPropertyEntries
in interfaceio.micronaut.core.value.PropertyResolver
-
getProperties
@NonNull public java.util.Map<java.lang.String,java.lang.Object> getProperties(@Nullable java.lang.String name, @Nullable io.micronaut.core.naming.conventions.StringConvention keyFormat)
- Specified by:
getProperties
in interfaceio.micronaut.core.value.PropertyResolver
-
registerConfiguration
protected void registerConfiguration(BeanConfiguration configuration)
Description copied from class:DefaultBeanContext
Registers an active configuration.- Overrides:
registerConfiguration
in classDefaultBeanContext
- Parameters:
configuration
- The configuration to register
-
startEnvironment
protected void startEnvironment()
Start the environment.
-
initializeContext
protected void initializeContext(java.util.List<BeanDefinitionReference> contextScopeBeans, java.util.List<BeanDefinitionReference> processedBeans, java.util.List<BeanDefinitionReference> parallelBeans)
Description copied from class:DefaultBeanContext
Initialize the context with the givenContext
scope beans.- Overrides:
initializeContext
in classDefaultBeanContext
- Parameters:
contextScopeBeans
- The context scope beansprocessedBeans
- The beans that requireExecutableMethodProcessor
handlingparallelBeans
- The parallel bean definitions
-
findBeanCandidates
protected <T> java.util.Collection<BeanDefinition<T>> findBeanCandidates(BeanResolutionContext resolutionContext, io.micronaut.core.type.Argument<T> beanType, BeanDefinition<?> filter, boolean filterProxied)
Description copied from class:DefaultBeanContext
Find bean candidates for the given type.- Overrides:
findBeanCandidates
in classDefaultBeanContext
- Type Parameters:
T
- The bean generic type- Parameters:
resolutionContext
- The current resolution contextbeanType
- The bean typefilter
- A bean definition to filter outfilterProxied
- Whether to filter out bean proxy targets- Returns:
- The candidates
-
findBeanCandidates
protected <T> java.util.Collection<BeanDefinition<T>> findBeanCandidates(BeanResolutionContext resolutionContext, io.micronaut.core.type.Argument<T> beanType, boolean filterProxied, java.util.function.Predicate<BeanDefinition<T>> predicate)
Description copied from class:DefaultBeanContext
Find bean candidates for the given type.- Overrides:
findBeanCandidates
in classDefaultBeanContext
- Type Parameters:
T
- The bean generic type- Parameters:
resolutionContext
- The current resolution contextbeanType
- The bean typefilterProxied
- Whether to filter out bean proxy targetspredicate
- The predicate to filter candidates- Returns:
- The candidates
-
transformIterables
protected <T> java.util.Collection<BeanDefinition<T>> transformIterables(BeanResolutionContext resolutionContext, java.util.Collection<BeanDefinition<T>> candidates, boolean filterProxied)
Description copied from class:DefaultBeanContext
Method that transforms iterable candidates if possible.- Overrides:
transformIterables
in classDefaultBeanContext
- Type Parameters:
T
- The bean type- Parameters:
resolutionContext
- The resolution contextcandidates
- The candidates.filterProxied
- Whether to filter proxied.- Returns:
- The candidates
-
findConcreteCandidate
protected <T> BeanDefinition<T> findConcreteCandidate(java.lang.Class<T> beanType, Qualifier<T> qualifier, java.util.Collection<BeanDefinition<T>> candidates)
Description copied from class:DefaultBeanContext
Fall back method to attempt to find a candidate for the given definitions.- Overrides:
findConcreteCandidate
in classDefaultBeanContext
- Type Parameters:
T
- The generic time- Parameters:
beanType
- The bean typequalifier
- The qualifiercandidates
- The candidates, always more than 1- Returns:
- The concrete bean definition
-
resolvePlaceholders
public java.util.Optional<java.lang.String> resolvePlaceholders(java.lang.String str)
Description copied from interface:PropertyPlaceholderResolver
Resolve the placeholders and return an Optional String if it was possible to resolve them.- Specified by:
resolvePlaceholders
in interfacePropertyPlaceholderResolver
- Parameters:
str
- The placeholder to resolve- Returns:
- The optional string or
Optional.empty()
if resolution was not possible
-
resolveRequiredPlaceholders
public java.lang.String resolveRequiredPlaceholders(java.lang.String str) throws ConfigurationException
Description copied from interface:PropertyPlaceholderResolver
Resolve the placeholders and return an Optional String if it was possible to resolve them.- Specified by:
resolveRequiredPlaceholders
in interfacePropertyPlaceholderResolver
- Parameters:
str
- The placeholder to resolve- Returns:
- The optional string or
Optional.empty()
if resolution was not possible - Throws:
ConfigurationException
- If the placeholders could not be resolved
-
initializeTypeConverters
protected void initializeTypeConverters(BeanContext beanContext)
- Parameters:
beanContext
- The bean context
-
-