Class DefaultApplicationContext
- All Implemented Interfaces:
ApplicationContext
,BeanContext
,BeanDefinitionRegistry
,BeanLocator
,PropertyPlaceholderResolver
,ApplicationEventPublisher<Object>
,ExecutionHandleLocator
,InitializableBeanContext
,LifeCycle<BeanContext>
,AnnotationMetadataResolver
,AttributeHolder
,MutableAttributeHolder
,ConversionServiceProvider
,PropertyResolver
,ValueResolver<String>
,Closeable
,AutoCloseable
ApplicationContext
interface.- Since:
- 1.0
- Author:
- Graeme Rocher
-
Field Summary
Fields inherited from class io.micronaut.context.DefaultBeanContext
initializing, LOG, LOG_LIFECYCLE, MSG_BEAN_DEFINITION, running, singletonScope, terminating
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
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultApplicationContext
(@NonNull ApplicationContextConfiguration configuration) Construct a new ApplicationContext for the given environment name and classloader.DefaultApplicationContext
(@NonNull ClassPathResourceLoader resourceLoader, @NonNull String... environmentNames) Construct a new ApplicationContext for the given environment name and classloader.DefaultApplicationContext
(@NonNull String... environmentNames) Construct a new ApplicationContext for the given environment name. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> void
collectIterableBeans
(BeanResolutionContext resolutionContext, BeanDefinition<T> iterableBean, Set<BeanDefinition<T>> targetSet) Collects iterable beans from a given iterable.boolean
containsProperties
(String name) Whether the given property or any nested properties exist for the key given key within this resolver.boolean
containsProperty
(String name) Whether the given property is contained within this resolver.protected @NonNull Environment
createEnvironment
(@NonNull ApplicationContextConfiguration configuration) Creates the default environment for the given environment name.protected <T> BeanDefinition<T>
findConcreteCandidate
(Class<T> beanType, Qualifier<T> qualifier, Collection<BeanDefinition<T>> candidates) Fall back method to attempt to find a candidate for the given definitions.Provides the conversion service.getProperties
(@Nullable String name, @Nullable StringConvention keyFormat) Return all the properties under the given key.<T> Optional<T>
getProperty
(String name, ArgumentConversionContext<T> conversionContext) Resolve the given property for the given name, type and generic type arguments.getPropertyEntries
(@NonNull String name) Returns a collection of properties entries under the given key.getPropertyPathMatches
(String pathPattern) Will return for a given pattern such asfoo.*.bar.*
and array of arrays containing the variable names that match the pattern.protected void
initializeContext
(List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> contextScopeBeans, List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> processedBeans, List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> parallelBeans) Initialize the context with the givenContext
scope beans.protected void
initializeTypeConverters
(BeanContext beanContext) protected <T> NoSuchBeanException
newNoSuchBeanException
(@Nullable BeanResolutionContext resolutionContext, Argument<T> beanType, Qualifier<T> qualifier, String message) Trigger a no such bean exception.protected void
registerConfiguration
(BeanConfiguration configuration) Registers an active configuration.protected void
Registers conversion service.registerSingleton
(@NonNull Class<T> type, T singleton, @Nullable Qualifier<T> qualifier, boolean inject) Registers a new singleton bean at runtime.Resolve the placeholders and return an Optional String if it was possible to resolve them.Resolve the placeholders and return a string if it was possible to resolve them.void
setEnvironment
(Environment environment) start()
The start method will read all bean definition classes found on the classpath and initialize any pre-required state.protected void
Start the environment.stop()
The close method will shut down the context callingPreDestroy
hooks on loaded singletons.Methods inherited from class io.micronaut.context.DefaultBeanContext
containsBean, containsBean, createBean, createBean, createBean, createBean, createCustomScopeRegistry, createExecutionHandle, destroyBean, destroyBean, destroyBean, destroyBean, finalizeConfiguration, findBean, findBean, findBean, findBean, findBeanCandidates, 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, getDisabledBeans, getProxyTargetBean, getProxyTargetBean, getProxyTargetBean, initializeEventListeners, inject, inject, invalidateCaches, isRunning, mapOfType, mapOfType, processParallelBeans, publishEvent, publishEventAsync, refreshBean, refreshBean, registerBeanDefinition, removeAttribute, resolveBeanConfigurations, resolveBeanDefinitionReferences, resolveBeanDefinitionReferences, resolveDisabledBeanMessage, 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.event.ApplicationEventPublisher
isEmpty, publishEvent, publishEventAsync
Methods inherited from interface io.micronaut.core.attr.AttributeHolder
getAttribute, getAttribute
Methods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, 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, 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.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, resolveOptionalPlaceholder, resolveRequiredPlaceholder, resolveRequiredPlaceholdersObject
Methods inherited from interface io.micronaut.core.value.PropertyResolver
get, getProperties, getProperty, getProperty, getProperty, getProperty, getRequiredProperty
Methods inherited from interface io.micronaut.core.value.ValueResolver
get, get, get
-
Constructor Details
-
DefaultApplicationContext
Construct a new ApplicationContext for the given environment name.- Parameters:
environmentNames
- The environment names
-
DefaultApplicationContext
public DefaultApplicationContext(@NonNull @NonNull ClassPathResourceLoader resourceLoader, @NonNull @NonNull String... environmentNames) Construct a new ApplicationContext for the given environment name and classloader.- Parameters:
environmentNames
- The environment namesresourceLoader
- The class loader
-
DefaultApplicationContext
Construct a new ApplicationContext for the given environment name and classloader.- Parameters:
configuration
- The application context configuration
-
-
Method Details
-
registerSingleton
@NonNull public <T> @NonNull ApplicationContext 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 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 @NonNull Environment createEnvironment(@NonNull @NonNull ApplicationContextConfiguration configuration) Creates the default environment for the given environment name.- Parameters:
configuration
- The application context configuration- Returns:
- The environment instance
-
getConversionService
Description copied from interface:ConversionServiceProvider
Provides the conversion service.- Specified by:
getConversionService
in interfaceConversionServiceProvider
- Overrides:
getConversionService
in classDefaultBeanContext
- Returns:
- the conversion service
-
getEnvironment
- Specified by:
getEnvironment
in interfaceApplicationContext
- Returns:
- The application environment
-
setEnvironment
- Parameters:
environment
- The environment
-
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
-
registerConversionService
protected void registerConversionService()Description copied from class:DefaultBeanContext
Registers conversion service.- Overrides:
registerConversionService
in classDefaultBeanContext
-
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
Description copied from interface:PropertyResolver
Whether the given property is contained within this resolver.
Note that this method will return false for nested properties. In other words given a key of
foo.bar
this method will returnfalse
for:resolver.containsProperty("foo")
To check for nested properties using
PropertyResolver.containsProperties(String)
instead.- Specified by:
containsProperty
in interfacePropertyResolver
- Parameters:
name
- The name of the property- Returns:
- True if it is
-
containsProperties
Description copied from interface:PropertyResolver
Whether the given property or any nested properties exist for the key given key within this resolver.- Specified by:
containsProperties
in interfacePropertyResolver
- Parameters:
name
- The name of the property- Returns:
- True if it is
-
getProperty
Description copied from interface:PropertyResolver
Resolve the given property for the given name, type and generic type arguments.
Implementers can choose to implement more intelligent type conversion by analyzing the typeArgument.
- Specified by:
getProperty
in interfacePropertyResolver
- Type Parameters:
T
- The concrete type- Parameters:
name
- The nameconversionContext
- The conversion context- Returns:
- An optional containing the property value if it exists
-
getPropertyEntries
Description copied from interface:PropertyResolver
Returns a collection of properties entries under the given key. For example given the following keys:datasource.default.url=localhost datasource.another.url=someother
getPropertyEntries(String)
with a value ofdatasource
will result in a collection containingdefault
andother
.- Specified by:
getPropertyEntries
in interfacePropertyResolver
- Parameters:
name
- The name to resolve- Returns:
- The property entries.
-
getProperties
@NonNull public @NonNull Map<String,Object> getProperties(@Nullable @Nullable String name, @Nullable @Nullable StringConvention keyFormat) Description copied from interface:PropertyResolver
Return all the properties under the given key. By default, Micronaut stores keys in keb-case, such that normalized lookups are more efficient. You can obtain the raw key values by passing inStringConvention.RAW
.- Specified by:
getProperties
in interfacePropertyResolver
- Parameters:
name
- The namekeyFormat
- The key format to use for the keys. Default is kebab-case.- Returns:
- The properties
-
getPropertyPathMatches
Description copied from interface:PropertyResolver
Will return for a given pattern such asfoo.*.bar.*
and array of arrays containing the variable names that match the pattern.- Specified by:
getPropertyPathMatches
in interfacePropertyResolver
- Parameters:
pathPattern
- The path pattern- Returns:
- An array of arrays.
-
registerConfiguration
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(List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> contextScopeBeans, List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> processedBeans, List<io.micronaut.context.DefaultBeanContext.BeanDefinitionProducer> 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
-
newNoSuchBeanException
protected <T> NoSuchBeanException newNoSuchBeanException(@Nullable @Nullable BeanResolutionContext resolutionContext, Argument<T> beanType, Qualifier<T> qualifier, String message) Description copied from class:DefaultBeanContext
Trigger a no such bean exception. Subclasses can improve the exception with downstream diagnosis as necessary.- Overrides:
newNoSuchBeanException
in classDefaultBeanContext
- Type Parameters:
T
- The type of the bean- Parameters:
resolutionContext
- The resolution contextbeanType
- The bean typequalifier
- The qualifiermessage
- A message to use- Returns:
- A no such bean exception
-
collectIterableBeans
protected <T> void collectIterableBeans(BeanResolutionContext resolutionContext, BeanDefinition<T> iterableBean, Set<BeanDefinition<T>> targetSet) Description copied from class:DefaultBeanContext
Collects iterable beans from a given iterable.- Overrides:
collectIterableBeans
in classDefaultBeanContext
- Type Parameters:
T
- The bean type- Parameters:
resolutionContext
- The resolution contextiterableBean
- The iterabletargetSet
- The target set
-
findConcreteCandidate
protected <T> BeanDefinition<T> findConcreteCandidate(Class<T> beanType, Qualifier<T> qualifier, 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
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
Description copied from interface:PropertyPlaceholderResolver
Resolve the placeholders and return a string if it was possible to resolve them.- Specified by:
resolveRequiredPlaceholders
in interfacePropertyPlaceholderResolver
- Parameters:
str
- The placeholder to resolve- Returns:
- The resolved string
- Throws:
ConfigurationException
- If the placeholders could not be resolved
-
initializeTypeConverters
- Parameters:
beanContext
- The bean context
-