Class DefaultApplicationContext
- All Implemented Interfaces:
ApplicationContext,BeanContext,BeanDefinitionRegistry,BeanLocator,ConfigurableApplicationContext,ConfigurableBeanContext,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
configured, initializing, LOG, LOG_LIFECYCLE, MSG_BEAN_DEFINITION, running, singletonScope, terminatingFields inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
DEFAULTFields inherited from interface io.micronaut.context.event.ApplicationEventPublisher
NO_OPFields 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> voidcollectIterableBeans(@Nullable BeanResolutionContext resolutionContext, @NonNull BeanDefinition<T> iterableBean, @NonNull Set<BeanDefinition<T>> targetSet, @NonNull Argument<T> beanType) Collects iterable beans from a given iterable.booleancontainsProperties(String name) Whether the given property or any nested properties exist for the key given key within this resolver.booleancontainsProperty(String name) Whether the given property is contained within this resolver.protected @NonNull EnvironmentcreateEnvironment(@NonNull ApplicationContextConfiguration configuration) Creates the default environment for the given environment name.protected <T> voiddestroyLifeCycleBean(LifeCycle<?> cycle, BeanDefinition<T> definition) Destroy a lifecycle bean.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.getPropertyEntries(@NonNull String name, @NonNull PropertyCatalog propertyCatalog) Returns a collection of properties entries under the given key, but .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 voidinitializeContext(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 givenContextscope beans.protected voidinitializeTypeConverters(BeanContext beanContext) protected <T> NoSuchBeanExceptionnewNoSuchBeanException(@Nullable BeanResolutionContext resolutionContext, Argument<T> beanType, @Nullable Qualifier<T> qualifier, @Nullable String message) Trigger a no such bean exception.protected voidregisterConfiguration(BeanConfiguration configuration) Registers an active configuration.protected voidRegisters 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.voidsetEnvironment(Environment environment) start()The start method will read all bean definition classes found on the classpath and initialize any pre-required state.protected voidStart the environment.stop()The close method will shut down the context callingPreDestroyhooks on loaded singletons.Methods inherited from class io.micronaut.context.DefaultBeanContext
configure, 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, getProxyTargetBean, initializeEventListeners, inject, inject, invalidateCaches, isRunning, mapOfType, mapOfType, processParallelBeans, publishEvent, publishEventAsync, refreshBean, refreshBean, registerBeanConfiguration, registerBeanDefinition, removeAttribute, resolveBeanConfigurations, resolveBeanDefinitionReferences, resolveBeanDefinitionReferences, resolveDisabledBeanMessage, resolveMetadata, setAttribute, streamOfType, streamOfType, streamOfType, streamOfTypeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.core.annotation.AnnotationMetadataResolver
resolveMetadata, resolveMetadataMethods inherited from interface io.micronaut.context.ApplicationContext
environment, registerSingleton, registerSingleton, registerSingleton, registerSingletonMethods inherited from interface io.micronaut.context.event.ApplicationEventPublisher
isEmpty, publishEvent, publishEventAsyncMethods inherited from interface io.micronaut.core.attr.AttributeHolder
getAttribute, getAttributeMethods inherited from interface io.micronaut.context.BeanContext
createBean, createBean, createBean, createBean, createBean, createBean, destroyBean, destroyBean, destroyBean, destroyBean, destroyBean, getBeanValidator, getClassLoader, getContextConfiguration, getEventPublisher, inject, refreshBean, refreshBeanMethods 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, registerBeanConfiguration, registerBeanDefinitionMethods inherited from interface io.micronaut.context.BeanLocator
findBean, findBean, findBean, findBean, findOrInstantiateBean, getBean, getBean, getBean, getBean, getBean, getBeansOfType, getBeansOfType, getBeansOfType, getBeansOfType, getProvider, getProvider, getProvider, getProvider, getProxyTargetBean, getProxyTargetBean, mapOfType, mapOfType, mapOfType, mapOfType, streamOfType, streamOfType, streamOfType, streamOfTypeMethods inherited from interface io.micronaut.context.ConfigurableBeanContext
configureMethods inherited from interface io.micronaut.context.ExecutionHandleLocator
createExecutionHandle, findExecutableMethod, findExecutionHandle, findExecutionHandle, findExecutionHandle, findProxyTargetMethod, findProxyTargetMethod, findProxyTargetMethod, getExecutableMethod, getExecutionHandle, getExecutionHandle, getProxyTargetMethod, getProxyTargetMethod, getProxyTargetMethodMethods inherited from interface io.micronaut.core.attr.MutableAttributeHolder
getAttributes, removeAttribute, setAttributeMethods inherited from interface io.micronaut.context.env.PropertyPlaceholderResolver
getPrefix, resolveOptionalPlaceholder, resolveRequiredPlaceholder, resolveRequiredPlaceholdersObjectMethods inherited from interface io.micronaut.core.value.PropertyResolver
get, getProperties, getProperty, getProperty, getProperty, getProperty, getRequiredPropertyMethods 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:BeanDefinitionRegistryRegisters 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
PostConstructhooks.If no bean definition data is found the bean is registered as is.
- Specified by:
registerSingletonin interfaceApplicationContext- Specified by:
registerSingletonin interfaceBeanContext- Specified by:
registerSingletonin interfaceBeanDefinitionRegistry- Overrides:
registerSingletonin 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:ConversionServiceProviderProvides the conversion service.- Specified by:
getConversionServicein interfaceConversionServiceProvider- Overrides:
getConversionServicein classDefaultBeanContext- Returns:
- the conversion service
-
getEnvironment
- Specified by:
getEnvironmentin interfaceApplicationContext- Returns:
- The application environment
-
setEnvironment
- Parameters:
environment- The environment
-
start
Description copied from class:DefaultBeanContextThe start method will read all bean definition classes found on the classpath and initialize any pre-required state.- Specified by:
startin interfaceApplicationContext- Specified by:
startin interfaceLifeCycle<BeanContext>- Overrides:
startin classDefaultBeanContext- Returns:
- This lifecycle component
-
registerConversionService
protected void registerConversionService()Description copied from class:DefaultBeanContextRegisters conversion service.- Overrides:
registerConversionServicein classDefaultBeanContext
-
stop
Description copied from class:DefaultBeanContextThe close method will shut down the context callingPreDestroyhooks on loaded singletons.- Specified by:
stopin interfaceApplicationContext- Specified by:
stopin interfaceLifeCycle<BeanContext>- Overrides:
stopin classDefaultBeanContext- Returns:
- This lifecycle component
-
containsProperty
Description copied from interface:PropertyResolverWhether 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.barthis method will returnfalsefor:resolver.containsProperty("foo")To check for nested properties using
PropertyResolver.containsProperties(String)instead.- Specified by:
containsPropertyin interfacePropertyResolver- Parameters:
name- The name of the property- Returns:
- True if it is
-
containsProperties
Description copied from interface:PropertyResolverWhether the given property or any nested properties exist for the key given key within this resolver.- Specified by:
containsPropertiesin interfacePropertyResolver- Parameters:
name- The name of the property- Returns:
- True if it is
-
getProperty
Description copied from interface:PropertyResolverResolve 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:
getPropertyin 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:PropertyResolverReturns a collection of properties entries under the given key. For example given the following keys:
Callingdatasource.default.url=localhost datasource.another.url=someothergetPropertyEntries(String)with a value ofdatasourcewill result in a collection containingdefaultandother.- Specified by:
getPropertyEntriesin interfacePropertyResolver- Parameters:
name- The name to resolve- Returns:
- The property entries.
-
getPropertyEntries
@NonNull public @NonNull Collection<String> getPropertyEntries(@NonNull @NonNull String name, @NonNull @NonNull PropertyCatalog propertyCatalog) Description copied from interface:PropertyResolverReturns a collection of properties entries under the given key, but . For example, if you setPropertyCatalog.RAWthen the following keys:
Callingdatasource.MyDs-1.url=localhost datasource.MyDs-2.url=someothergetPropertyEntries(String)with a value ofdatasourcewill result in a collection containingMyDs-1andMyDs-2(without normalization).- Specified by:
getPropertyEntriesin interfacePropertyResolver- Parameters:
name- The name to resolvepropertyCatalog- property catalog to use- Returns:
- The property entries.
-
getProperties
@NonNull public @NonNull Map<String,Object> getProperties(@Nullable @Nullable String name, @Nullable @Nullable StringConvention keyFormat) Description copied from interface:PropertyResolverReturn 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:
getPropertiesin 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:PropertyResolverWill return for a given pattern such asfoo.*.bar.*and array of arrays containing the variable names that match the pattern.- Specified by:
getPropertyPathMatchesin interfacePropertyResolver- Parameters:
pathPattern- The path pattern- Returns:
- An array of arrays.
-
registerConfiguration
Description copied from class:DefaultBeanContextRegisters an active configuration.- Overrides:
registerConfigurationin 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:DefaultBeanContextInitialize the context with the givenContextscope beans.- Overrides:
initializeContextin classDefaultBeanContext- Parameters:
contextScopeBeans- The context scope beansprocessedBeans- The beans that requireExecutableMethodProcessorhandlingparallelBeans- The parallel bean definitions
-
newNoSuchBeanException
protected <T> NoSuchBeanException newNoSuchBeanException(@Nullable @Nullable BeanResolutionContext resolutionContext, Argument<T> beanType, @Nullable @Nullable Qualifier<T> qualifier, @Nullable @Nullable String message) Description copied from class:DefaultBeanContextTrigger a no such bean exception. Subclasses can improve the exception with downstream diagnosis as necessary.- Overrides:
newNoSuchBeanExceptionin 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(@Nullable @Nullable BeanResolutionContext resolutionContext, @NonNull @NonNull BeanDefinition<T> iterableBean, @NonNull @NonNull Set<BeanDefinition<T>> targetSet, @NonNull @NonNull Argument<T> beanType) Description copied from class:DefaultBeanContextCollects iterable beans from a given iterable.- Overrides:
collectIterableBeansin classDefaultBeanContext- Type Parameters:
T- The bean type- Parameters:
resolutionContext- The resolution contextiterableBean- The iterabletargetSet- The target setbeanType- The bean type
-
findConcreteCandidate
protected <T> BeanDefinition<T> findConcreteCandidate(Class<T> beanType, Qualifier<T> qualifier, Collection<BeanDefinition<T>> candidates) Description copied from class:DefaultBeanContextFall back method to attempt to find a candidate for the given definitions.- Overrides:
findConcreteCandidatein 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:PropertyPlaceholderResolverResolve the placeholders and return an Optional String if it was possible to resolve them.- Specified by:
resolvePlaceholdersin interfacePropertyPlaceholderResolver- Parameters:
str- The placeholder to resolve- Returns:
- The optional string or
Optional.empty()if resolution was not possible
-
resolveRequiredPlaceholders
Description copied from interface:PropertyPlaceholderResolverResolve the placeholders and return a string if it was possible to resolve them.- Specified by:
resolveRequiredPlaceholdersin interfacePropertyPlaceholderResolver- Parameters:
str- The placeholder to resolve- Returns:
- The resolved string
- Throws:
ConfigurationException- If the placeholders could not be resolved
-
destroyLifeCycleBean
Description copied from class:DefaultBeanContextDestroy a lifecycle bean.- Overrides:
destroyLifeCycleBeanin classDefaultBeanContext- Type Parameters:
T- The bean type- Parameters:
cycle- The cycledefinition- The definition
-
initializeTypeConverters
- Parameters:
beanContext- The bean context
-