Package io.micronaut.context.env
Class DefaultEnvironment
java.lang.Object
io.micronaut.context.env.PropertySourcePropertyResolver
io.micronaut.context.env.DefaultEnvironment
- All Implemented Interfaces:
Environment,LifeCycle<Environment>,ConversionService,MutableConversionService,ResourceLoader,PropertyResolver,ValueResolver<String>,Closeable,AutoCloseable
The default implementation of the Environment interface. Configures a named environment.
- Since:
- 1.0
- Author:
- Graeme Rocher, rvanderwerf
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final MutableConversionServiceprotected final List<PropertySource>protected final ClassPathResourceLoaderFields inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
catalog, conversionService, log, nonGenerated, NULL_ENTRY, propertyPlaceholderResolver, propertySources, rawCatalogFields inherited from interface io.micronaut.core.convert.ConversionService
SHAREDFields inherited from interface io.micronaut.context.env.Environment
AMAZON_EC2, ANDROID, AZURE, BARE_METAL, BOOTSTRAP_CONTEXT_PROPERTY, BOOTSTRAP_NAME, BOOTSTRAP_NAME_PROPERTY, CLI, CLOUD, CLOUD_FOUNDRY, CLOUD_PLATFORM_PROPERTY, DEDUCE_CLOUD_ENVIRONMENT_ENV, DEDUCE_CLOUD_ENVIRONMENT_PROPERTY, DEDUCE_ENVIRONMENT_ENV, DEDUCE_ENVIRONMENT_PROPERTY, DEFAULT_NAME, DEVELOPMENT, DIGITAL_OCEAN, ENVIRONMENTS_ENV, ENVIRONMENTS_PROPERTY, FUNCTION, GAE, GOOGLE_COMPUTE, HEROKU, HOSTNAME, IBM, KUBERNETES, MICRONAUT, ORACLE_CLOUD, PROPERTY_SOURCES_KEY, TEST -
Constructor Summary
ConstructorsConstructorDescriptionDefaultEnvironment(@NonNull ApplicationContextConfiguration configuration) Construct a new environment for the given configuration.DefaultEnvironment(@NonNull ApplicationContextConfiguration configuration, boolean logEnabled) Construct a new environment for the given configuration. -
Method Summary
Modifier and TypeMethodDescriptionaddConfigurationExcludes(@Nullable String... names) Exclude configurations by name.addConfigurationIncludes(String... names) Exclude configurations by name.<S,T> void addConverter(Class<S> sourceType, Class<T> targetType, TypeConverter<S, T> typeConverter) Adds a type converter.<S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Function<S, T> typeConverter) Adds a type converter.protected voidaddDefaultPropertySources(List<PropertySource> propertySources) Adds default property sources.addPackage(String pkg) Add an application package.addPropertySource(PropertySource propertySource) Add aPropertySourceto this resolver.addPropertySource(String name, Map<String, ? super Object> values) Add a property source for the given map.<S,T> boolean canConvert(Class<S> sourceType, Class<T> targetType) Return whether the given source type is convertible to the given target type.voidclose()Delegates toLifeCycle.stop().<T> Optional<T>convert(Object object, Class<T> targetType, ConversionContext context) Attempts to convert the given object to the given target type.<S,T> Optional<T> convert(S object, Class<? super S> sourceType, Class<T> targetType, ConversionContext context) Attempts to convert the given object to the given target type from the given source type.protected BeanIntrospectionScannercreateAnnotationScanner(ClassLoader classLoader) Creates the default annotation scanner.Constructs a new resource loader designed to load resources from the given path.Should respect the order as provided.protected List<PropertySource>getPropertyEntry(String name) Resolve information about a property.Obtains thePropertySourceLoaderinstances.protected StringgetResource(String path) Obtains the URL to a given resource.getResourceAsStream(String path) Obtains a resource as a stream.getResources(String path) Obtains all resources with the given name.booleanisActive(BeanConfiguration configuration) Whether the current environment includes the given configuration.booleanCheck whether the given class is present within this environment.booleanprotected List<PropertySource>readPropertySourceList(String name) protected List<PropertySource>Resolve the property sources for files passed via system property and system env.protected StringReads the value of MICRONAUT_CONFIG_FILES environment variable.protected SoftServiceLoader<PropertySourceLoader>protected voidreadPropertySources(String name) Refresh the environment from the list ofPropertySourceinstances and return a diff of the changes.removePropertySource(PropertySource propertySource) Removes a property source from this environment.scan(Class<? extends Annotation> annotation) Scan the current environment for classes annotated with the given annotation.scan(Class<? extends Annotation> annotation, String... packages) Scan the current environment for classes annotated with the given annotation.protected booleanprotected booleanstart()Starts the lifecyle component.stop()Stops the life cycle component.booleansupportsPrefix(String path) Methods inherited from class io.micronaut.context.env.PropertySourcePropertyResolver
containsProperties, containsProperty, getAllProperties, getProperties, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, processPropertySource, resetCaches, resolveEntriesForKey, resolveSubMap, resolveSubMap, resolveSubPropertiesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.core.convert.ConversionService
convert, convert, convert, convert, convertRequired, convertRequired, convertRequiredMethods inherited from interface io.micronaut.context.env.Environment
addPackageMethods inherited from interface io.micronaut.core.value.PropertyResolver
containsProperties, containsProperty, get, getProperties, getProperties, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyEntries, getPropertyEntries, getPropertyPathMatches, getRequiredPropertyMethods inherited from interface io.micronaut.core.value.ValueResolver
get, get, get
-
Field Details
-
resourceLoader
-
refreshablePropertySources
-
mutableConversionService
-
-
Constructor Details
-
DefaultEnvironment
Construct a new environment for the given configuration.- Parameters:
configuration- The configuration
-
DefaultEnvironment
public DefaultEnvironment(@NonNull @NonNull ApplicationContextConfiguration configuration, boolean logEnabled) Construct a new environment for the given configuration.- Parameters:
configuration- The configurationlogEnabled- flag to enable or disable logger
-
-
Method Details
-
isPresent
Description copied from interface:EnvironmentCheck whether the given class is present within this environment.- Specified by:
isPresentin interfaceEnvironment- Parameters:
className- The class name- Returns:
- True if it is
-
getPlaceholderResolver
- Specified by:
getPlaceholderResolverin interfaceEnvironment- Returns:
- The placeholder resolver
-
scan
Description copied from interface:EnvironmentScan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.- Specified by:
scanin interfaceEnvironment- Parameters:
annotation- The annotation to scan- Returns:
- The classes
-
scan
Description copied from interface:EnvironmentScan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.- Specified by:
scanin interfaceEnvironment- Parameters:
annotation- The annotation to scanpackages- The packages to scan- Returns:
- The classes
-
getClassLoader
- Specified by:
getClassLoaderin interfaceEnvironment- Returns:
- The class loader for the environment
-
isActive
Description copied from interface:EnvironmentWhether the current environment includes the given configuration.- Specified by:
isActivein interfaceEnvironment- Parameters:
configuration- The configuration- Returns:
- True if it does
-
addPropertySource
Description copied from class:PropertySourcePropertyResolverAdd aPropertySourceto this resolver.- Specified by:
addPropertySourcein interfaceEnvironment- Overrides:
addPropertySourcein classPropertySourcePropertyResolver- Parameters:
propertySource- ThePropertySourceto add- Returns:
- This
PropertySourcePropertyResolver
-
removePropertySource
Description copied from interface:EnvironmentRemoves a property source from this environment.- Specified by:
removePropertySourcein interfaceEnvironment- Parameters:
propertySource- The property source- Returns:
- This environment
-
addPropertySource
Description copied from class:PropertySourcePropertyResolverAdd a property source for the given map.- Specified by:
addPropertySourcein interfaceEnvironment- Overrides:
addPropertySourcein classPropertySourcePropertyResolver- Parameters:
name- The name of the property sourcevalues- The values- Returns:
- This environment
-
addPackage
Description copied from interface:EnvironmentAdd an application package. Application packages are candidates for scanning for tools that need it (such as JPA).- Specified by:
addPackagein interfaceEnvironment- Parameters:
pkg- The package to add- Returns:
- This environment
-
addConfigurationExcludes
Description copied from interface:EnvironmentExclude configurations by name.- Specified by:
addConfigurationExcludesin interfaceEnvironment- Parameters:
names- The names of the configuration- Returns:
- This environment
-
addConfigurationIncludes
Description copied from interface:EnvironmentExclude configurations by name.- Specified by:
addConfigurationIncludesin interfaceEnvironment- Parameters:
names- The names of the configuration- Returns:
- This environment
-
getPackages
- Specified by:
getPackagesin interfaceEnvironment- Returns:
- The application packages
-
getActiveNames
Description copied from interface:EnvironmentShould respect the order as provided.- Specified by:
getActiveNamesin interfaceEnvironment- Returns:
- The active environment names
-
getPropertySources
- Specified by:
getPropertySourcesin interfaceEnvironment- Returns:
- The active property sources
-
start
Description copied from interface:LifeCycleStarts the lifecyle component.- Specified by:
startin interfaceLifeCycle<Environment>- Returns:
- This lifecycle component
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceLifeCycle<Environment>- Returns:
- Whether the component is running
-
stop
Description copied from interface:LifeCycleStops the life cycle component.- Specified by:
stopin interfaceLifeCycle<Environment>- Returns:
- This lifecycle component
-
refreshAndDiff
Description copied from interface:EnvironmentRefresh the environment from the list ofPropertySourceinstances and return a diff of the changes.- Specified by:
refreshAndDiffin interfaceEnvironment- Returns:
- The values that changed
-
convert
Description copied from interface:ConversionServiceAttempts to convert the given object to the given target type. If conversion fails or is not possible an emptyOptionalis returned.- Specified by:
convertin interfaceConversionService- Type Parameters:
T- The target type- Parameters:
object- The object to converttargetType- The target typecontext- The conversion context- Returns:
- The optional
-
convert
public <S,T> Optional<T> convert(S object, Class<? super S> sourceType, Class<T> targetType, ConversionContext context) Description copied from interface:ConversionServiceAttempts to convert the given object to the given target type from the given source type. If conversion fails or is not possible an emptyOptionalis returned.- Specified by:
convertin interfaceConversionService- Type Parameters:
S- The source typeT- The target type- Parameters:
object- The object to convertsourceType- The source typetargetType- The target typecontext- The conversion context- Returns:
- The optional
-
canConvert
Description copied from interface:ConversionServiceReturn whether the given source type is convertible to the given target type.- Specified by:
canConvertin interfaceConversionService- Type Parameters:
S- The generic source typeT- The target source type- Parameters:
sourceType- The source typetargetType- The target type- Returns:
- True if it can be converted
-
addConverter
public <S,T> void addConverter(Class<S> sourceType, Class<T> targetType, TypeConverter<S, T> typeConverter) Description copied from interface:MutableConversionServiceAdds a type converter.- Specified by:
addConverterin interfaceMutableConversionService- Type Parameters:
S- The source generic typeT- The target generic type- Parameters:
sourceType- The source typetargetType- The target typetypeConverter- The type converter
-
addConverter
public <S,T> void addConverter(Class<S> sourceType, Class<T> targetType, Function<S, T> typeConverter) Description copied from interface:MutableConversionServiceAdds a type converter.- Specified by:
addConverterin interfaceMutableConversionService- Type Parameters:
S- The source generic typeT- The target generic type- Parameters:
sourceType- The source typetargetType- The target typetypeConverter- The type converter
-
getMutableConversionService
- Returns:
- The mutable conversion service.
-
getResourceAsStream
Description copied from interface:ResourceLoaderObtains a resource as a stream.- Specified by:
getResourceAsStreamin interfaceResourceLoader- Parameters:
path- The path- Returns:
- An optional resource
-
getResource
Description copied from interface:ResourceLoaderObtains the URL to a given resource.- Specified by:
getResourcein interfaceResourceLoader- Parameters:
path- The path- Returns:
- An optional resource
-
getResources
Description copied from interface:ResourceLoaderObtains all resources with the given name.- Specified by:
getResourcesin interfaceResourceLoader- Parameters:
path- The name of the resource- Returns:
- A stream of URLs
-
supportsPrefix
- Specified by:
supportsPrefixin interfaceResourceLoader- Parameters:
path- The path to a resource including a prefix appended by a colon. Ex (classpath:, file:)- Returns:
- Whether the given resource loader supports the prefix
-
forBase
Description copied from interface:ResourceLoaderConstructs a new resource loader designed to load resources from the given path. Requested resources will be loaded within the context of the given path.- Specified by:
forBasein interfaceResourceLoader- Parameters:
basePath- The path to load resources- Returns:
- The new
ResourceLoader
-
shouldDeduceEnvironments
protected boolean shouldDeduceEnvironments()- Returns:
- Whether environment names and packages should be deduced
-
shouldDeduceCloudEnvironment
protected boolean shouldDeduceCloudEnvironment()- Returns:
- Whether cloud environment should be deduced based on environment variable, system property or configuration
-
createAnnotationScanner
Creates the default annotation scanner.- Parameters:
classLoader- The class loader- Returns:
- The scanner
-
getPropertySourceRootName
- Returns:
- The property source root name
-
readPropertySources
- Parameters:
name- The name to read property sources
-
getConstantPropertySources
- Returns:
- Property sources created at build time
-
readPropertySourceListKeyFromEnvironment
Reads the value of MICRONAUT_CONFIG_FILES environment variable.- Returns:
- The comma-separated list of files
-
readPropertySourceListFromFiles
Resolve the property sources for files passed via system property and system env.- Parameters:
files- The comma separated list of files- Returns:
- The list of property sources for each file
-
readPropertySourceList
- Parameters:
name- The name to resolver property sources- Returns:
- The list of property sources
-
addDefaultPropertySources
Adds default property sources.- Parameters:
propertySources- The list of property sources
-
readPropertySourceLoaders
- Returns:
- Loaded properties as a
SoftServiceLoader
-
getPropertyEntry
Description copied from interface:EnvironmentResolve information about a property.- Specified by:
getPropertyEntryin interfaceEnvironment- Parameters:
name- The property name- Returns:
- The entry.
-
getPropertySourceLoaders
Obtains thePropertySourceLoaderinstances.- Specified by:
getPropertySourceLoadersin interfaceEnvironment- Returns:
- A collection of
PropertySourceLoader
-
close
public void close()Description copied from interface:LifeCycleDelegates toLifeCycle.stop().- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceLifeCycle<Environment>- Overrides:
closein classPropertySourcePropertyResolver
-