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 theApplicationContextinterface.- 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 booleancontainsProperties(java.lang.String name)booleancontainsProperty(java.lang.String name)protected io.micronaut.core.convert.ConversionServicecreateConversionService()Creates the default conversion service.protected EnvironmentcreateEnvironment(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()EnvironmentgetEnvironment()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 voidinitializeContext(java.util.List<BeanDefinitionReference> contextScopeBeans, java.util.List<BeanDefinitionReference> processedBeans, java.util.List<BeanDefinitionReference> parallelBeans)Initialize the context with the givenContextscope beans.protected voidinitializeTypeConverters(BeanContext beanContext)protected voidregisterConfiguration(BeanConfiguration configuration)Registers an active configuration.<T> ApplicationContextregisterSingleton(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.StringresolveRequiredPlaceholders(java.lang.String str)Resolve the placeholders and return an Optional String if it was possible to resolve them.ApplicationContextstart()The start method will read all bean definition classes found on the classpath and initialize any pre-required state.protected voidstartEnvironment()Start the environment.ApplicationContextstop()The close method will shut down the context callingPreDestroyhooks 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: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 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:
getConversionServicein interfaceApplicationContext- Returns:
- The default conversion service
-
getEnvironment
@NonNull public Environment getEnvironment()
- Specified by:
getEnvironmentin interfaceApplicationContext- Returns:
- The application environment
-
start
@NonNull public ApplicationContext 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
-
stop
@NonNull public ApplicationContext 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
public boolean containsProperty(java.lang.String name)
- Specified by:
containsPropertyin interfaceio.micronaut.core.value.PropertyResolver
-
containsProperties
public boolean containsProperties(java.lang.String name)
- Specified by:
containsPropertiesin 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:
getPropertyin interfaceio.micronaut.core.value.PropertyResolver
-
getPropertyEntries
@NonNull public java.util.Collection<java.lang.String> getPropertyEntries(@NonNull java.lang.String name)- Specified by:
getPropertyEntriesin 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:
getPropertiesin interfaceio.micronaut.core.value.PropertyResolver
-
registerConfiguration
protected void registerConfiguration(BeanConfiguration configuration)
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(java.util.List<BeanDefinitionReference> contextScopeBeans, java.util.List<BeanDefinitionReference> processedBeans, java.util.List<BeanDefinitionReference> 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
-
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:DefaultBeanContextFind bean candidates for the given type.- Overrides:
findBeanCandidatesin 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:DefaultBeanContextFind bean candidates for the given type.- Overrides:
findBeanCandidatesin 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:DefaultBeanContextMethod that transforms iterable candidates if possible.- Overrides:
transformIterablesin 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: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
public java.util.Optional<java.lang.String> resolvePlaceholders(java.lang.String str)
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
public java.lang.String resolveRequiredPlaceholders(java.lang.String str) throws ConfigurationExceptionDescription copied from interface:PropertyPlaceholderResolverResolve the placeholders and return an Optional String if it was possible to resolve them.- Specified by:
resolveRequiredPlaceholdersin 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
-
-