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<Object>,ExecutionHandleLocator,InitializableBeanContext,LifeCycle<BeanContext>,io.micronaut.core.annotation.AnnotationMetadataResolver,io.micronaut.core.attr.AttributeHolder,io.micronaut.core.attr.MutableAttributeHolder,io.micronaut.core.convert.ConversionServiceProvider,io.micronaut.core.value.PropertyResolver,io.micronaut.core.value.ValueResolver<String>,Closeable,AutoCloseable
Creates a default implementation of the
ApplicationContext interface.- Since:
- 1.0
-
Field Summary
Fields inherited from class io.micronaut.context.DefaultBeanContext
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 io.micronaut.core.io.scan.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(BeanResolutionContext resolutionContext, BeanDefinition<T> iterableBean, Set<BeanDefinition<T>> targetSet) Collects iterable beans from a given iterable.booleancontainsProperties(String name) booleancontainsProperty(String name) protected @NonNull EnvironmentcreateEnvironment(@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.@NonNull io.micronaut.core.convert.MutableConversionService@NonNull EnvironmentgetProperties(@Nullable String name, @Nullable io.micronaut.core.naming.conventions.StringConvention keyFormat) <T> Optional<T>getProperty(String name, io.micronaut.core.convert.ArgumentConversionContext<T> conversionContext) @NonNull Collection<String>getPropertyEntries(@NonNull String name) getPropertyPathMatches(String pathPattern) 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, io.micronaut.core.type.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.<T> @NonNull ApplicationContextregisterSingleton(@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) @NonNull ApplicationContextstart()The start method will read all bean definition classes found on the classpath and initialize any pre-required state.protected voidStart the environment.@NonNull ApplicationContextstop()The close method will shut down the context callingPreDestroyhooks 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, getProxyTargetBean, initializeEventListeners, inject, inject, invalidateCaches, isRunning, mapOfType, mapOfType, processParallelBeans, publishEvent, publishEventAsync, refreshBean, refreshBean, 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, registerBeanDefinitionMethods 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, streamOfTypeMethods 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 io.micronaut.core.io.scan.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
@NonNull public @NonNull io.micronaut.core.convert.MutableConversionService getConversionService()- Specified by:
getConversionServicein interfaceio.micronaut.core.convert.ConversionServiceProvider- Overrides:
getConversionServicein classDefaultBeanContext
-
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
- Specified by:
containsPropertyin interfaceio.micronaut.core.value.PropertyResolver
-
containsProperties
- Specified by:
containsPropertiesin interfaceio.micronaut.core.value.PropertyResolver
-
getProperty
public <T> Optional<T> getProperty(String name, io.micronaut.core.convert.ArgumentConversionContext<T> conversionContext) - Specified by:
getPropertyin interfaceio.micronaut.core.value.PropertyResolver
-
getPropertyEntries
- Specified by:
getPropertyEntriesin interfaceio.micronaut.core.value.PropertyResolver
-
getProperties
@NonNull public @NonNull Map<String,Object> getProperties(@Nullable @Nullable String name, @Nullable @Nullable io.micronaut.core.naming.conventions.StringConvention keyFormat) - Specified by:
getPropertiesin interfaceio.micronaut.core.value.PropertyResolver
-
getPropertyPathMatches
- Specified by:
getPropertyPathMatchesin interfaceio.micronaut.core.value.PropertyResolver
-
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, io.micronaut.core.type.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(BeanResolutionContext resolutionContext, BeanDefinition<T> iterableBean, Set<BeanDefinition<T>> targetSet) 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 set
-
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
-
initializeTypeConverters
- Parameters:
beanContext- The bean context
-