类 AbstractEnvironment
- 所有已实现的接口:
ConfigurableEnvironment,ConfigurablePropertyResolver,Environment,IterablePropertyResolver,PropertyResolver,Iterable<String>
- 直接已知子类:
StandardEnvironment
Environment implementations. Supports the notion of
reserved default profile names and enables specifying active and default profiles
through the Environment.KEY_ACTIVE_PROFILES and
Environment.KEY_DEFAULT_PROFILES properties.
Concrete subclasses differ primarily on which PropertySource objects they
add by default. AbstractEnvironment adds none. Subclasses should contribute
property sources through the protected customizePropertySources(PropertySources)
hook, while clients should customize using ConfigurableEnvironment.getPropertySources()
and working against the PropertySources API.
See ConfigurableEnvironment javadoc for usage examples.
- 从以下版本开始:
- 4.0
- 作者:
- Chris Beams, Juergen Hoeller, Harry Yang
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明private final LinkedHashSet<String>private final LinkedHashSet<String>private static final Loggerprivate final ConfigurablePropertyResolverprivate final PropertySources从接口继承的字段 cn.taketoday.core.env.Environment
DEFAULT_PROFILE, ENVIRONMENT_BEAN_NAME, KEY_ACTIVE_PROFILES, KEY_DEFAULT_PROFILES, KEY_IGNORE_GETENV, SYSTEM_ENVIRONMENT_BEAN_NAME, SYSTEM_PROPERTIES_BEAN_NAME -
构造器概要
构造器限定符构造器说明Create a newEnvironmentinstance, calling back tocustomizePropertySources(PropertySources)during construction to allow subclasses to contribute or manipulatePropertySourceinstances as appropriate.protectedAbstractEnvironment(PropertySources propertySources) Create a newEnvironmentinstance with a specificPropertySourcesinstance, calling back tocustomizePropertySources(PropertySources)during construction to allow subclasses to contribute or manipulatePropertySourceinstances as appropriate. -
方法概要
修饰符和类型方法说明booleanacceptsProfiles(Profiles profiles) Determine whether the givenProfilespredicate matches the active profiles — or in the case of no explicit active profiles, whether the givenProfilespredicate matches the default profiles.voidaddActiveProfile(String profile) Add a profile to the current set of active profiles.voidaddRequiredProperties(String... requiredProperties) Specify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties().booleancontainsProperty(String key) Return whether the given property key is available for resolution, i.e. if the value for the given key is notnull.protected ConfigurablePropertyResolvercreatePropertyResolver(PropertySources propertySources) Factory method used to create theConfigurablePropertyResolverinstance used by the Environment.protected voidcustomizePropertySources(PropertySources propertySources) Customize the set ofPropertySourceobjects to be searched by thisEnvironmentduring calls togetProperty(String)and related methods.Return the set of active profiles as explicitly set throughsetActiveProfiles(java.lang.String...)or if the current set of active profiles is empty, check for the presence ofdoGetActiveProfilesProperty()and assign its value to the set of active profiles.protected StringReturn the property value for the active profiles.Return the set of default profiles explicitly set viasetDefaultProfiles(String...)or if the current set of default profiles consists only of reserved default profiles, then check for the presence ofdoGetActiveProfilesProperty()and assign its value (if any) to the set of default profiles.protected StringReturn the property value for the default profiles.String[]Return the set of profiles explicitly made active for this environment.Return theConfigurableConversionServiceused when performing type conversions on properties.String[]Return the set of profiles to be active by default when no active profiles have been set explicitly.getProperty(String key) Return the property value associated with the given key, ornullif the key cannot be resolved.<T> TgetProperty(String key, Class<T> targetType) Return the property value associated with the given key, ornullif the key cannot be resolved.<T> TgetProperty(String key, Class<T> targetType, T defaultValue) Return the property value associated with the given key, ordefaultValueif the key cannot be resolved.getProperty(String key, String defaultValue) Return the property value associated with the given key, ordefaultValueif the key cannot be resolved.protected final ConfigurablePropertyResolverReturn theConfigurablePropertyResolverbeing used by theEnvironment.Return thePropertySourcesfor thisEnvironmentin mutable form, allowing for manipulation of the set ofPropertySourceobjects that should be searched when resolving properties against thisEnvironmentobject.Return the property value associated with the given key (nevernull).<T> TgetRequiredProperty(String key, Class<T> targetType) Return the property value associated with the given key, converted to the given targetType (nevernull).Return the set of reserved default profile names.Return the value ofSystem.getenv().Return the value ofSystem.getProperties().protected booleanisProfileActive(String profile) Return whether the given profile is active, or if active profiles are empty whether the profile should be active by default.iterator()Returns an iterator of property-names.voidmerge(ConfigurableEnvironment parent) Append the given parent environment's active profiles, default profiles and property sources to this (child) environment's respective collections of each.resolvePlaceholders(String text) Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String).Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String).voidsetActiveProfiles(String... profiles) Specify the set of profiles active for thisEnvironment.voidsetConversionService(ConfigurableConversionService conversionService) Set theConfigurableConversionServiceto be used when performing type conversions on properties.voidsetDefaultProfiles(String... profiles) Specify the set of profiles to be made active by default if no other profiles are explicitly made active throughsetActiveProfiles(java.lang.String...).voidsetEscapeCharacter(Character escapeCharacter) Specify the escape character to use to ignore placeholder prefix or value separator, ornullif no escaping should take place.voidsetIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) Set whether to throw an exception when encountering an unresolvable placeholder nested within the value of a given property.voidsetPlaceholderPrefix(String placeholderPrefix) Set the prefix that placeholders replaced by this resolver must begin with.voidsetPlaceholderSuffix(String placeholderSuffix) Set the suffix that placeholders replaced by this resolver must end with.voidsetRequiredProperties(String... requiredProperties) Specify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties().voidsetValueSeparator(String valueSeparator) Specify the separating character between the placeholders replaced by this resolver and their associated default value, ornullif no such special character should be processed as a value separator.protected booleanDetermine whether to suppressSystem.getenv()/System.getenv(String)access for the purposes ofgetSystemEnvironment().toString()protected voidvalidateProfile(String profile) Validate the given profile, called internally prior to adding to the set of active or default profiles.voidValidate that each of the properties specified byConfigurablePropertyResolver.setRequiredProperties(java.lang.String...)is present and resolves to a non-nullvalue.从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 cn.taketoday.core.env.Environment
matchesProfiles从接口继承的方法 java.lang.Iterable
forEach, spliterator从接口继承的方法 cn.taketoday.core.env.PropertyResolver
getFlag, getFlag
-
字段详细资料
-
log
-
activeProfiles
-
defaultProfiles
-
propertySources
-
propertyResolver
-
-
构造器详细资料
-
AbstractEnvironment
public AbstractEnvironment()Create a newEnvironmentinstance, calling back tocustomizePropertySources(PropertySources)during construction to allow subclasses to contribute or manipulatePropertySourceinstances as appropriate. -
AbstractEnvironment
Create a newEnvironmentinstance with a specificPropertySourcesinstance, calling back tocustomizePropertySources(PropertySources)during construction to allow subclasses to contribute or manipulatePropertySourceinstances as appropriate.
-
-
方法详细资料
-
createPropertyResolver
Factory method used to create theConfigurablePropertyResolverinstance used by the Environment.- 另请参阅:
-
getPropertyResolver
Return theConfigurablePropertyResolverbeing used by theEnvironment. -
customizePropertySources
Customize the set ofPropertySourceobjects to be searched by thisEnvironmentduring calls togetProperty(String)and related methods.Subclasses that override this method are encouraged to add property sources using
PropertySources.addLast(PropertySource)such that further subclasses may callsuper.customizePropertySources()with predictable results. For example:
In this arrangement, properties will be resolved against sources A, B, C, D in that order. That is to say that property source "A" has precedence over property source "D". If thepublic class Level1Environment extends AbstractEnvironment { @Override protected void customizePropertySources(PropertySources propertySources) { super.customizePropertySources(propertySources); // no-op from base class propertySources.addLast(new PropertySourceA(...)); propertySources.addLast(new PropertySourceB(...)); } } public class Level2Environment extends Level1Environment { @Override protected void customizePropertySources(PropertySources propertySources) { super.customizePropertySources(propertySources); // add all from superclass propertySources.addLast(new PropertySourceC(...)); propertySources.addLast(new PropertySourceD(...)); } }Level2Environmentsubclass wished to give property sources C and D higher precedence than A and B, it could simply callsuper.customizePropertySourcesafter, rather than before adding its own:
The search order is now C, D, A, B as desired.public class Level2Environment extends Level1Environment { @Override protected void customizePropertySources(PropertySources propertySources) { propertySources.addLast(new PropertySourceC(...)); propertySources.addLast(new PropertySourceD(...)); super.customizePropertySources(propertySources); // add all from superclass } }Beyond these recommendations, subclasses may use any of the
add*,remove, orreplacemethods exposed byPropertySourcesin order to create the exact arrangement of property sources desired.The base implementation registers no property sources.
Note that clients of any
ConfigurableEnvironmentmay further customize property sources via thegetPropertySources()accessor, typically within ancn.taketoday.context.ApplicationContextInitializer. For example:ConfigurableEnvironment env = new StandardEnvironment(); env.getPropertySources().addLast(new PropertySourceX(...));
A warning about instance variable access
Instance variables declared in subclasses and having default initial values should not be accessed from within this method. Due to Java object creation lifecycle constraints, any initial value will not yet be assigned when this callback is invoked by theAbstractEnvironment()constructor, which may lead to aNullPointerExceptionor other problems. If you need to access default values of instance variables, leave this method as a no-op and perform property source manipulation and instance variable access directly within the subclass constructor. Note that assigning values to instance variables is not problematic; it is only attempting to read default values that must be avoided. -
getReservedDefaultProfiles
Return the set of reserved default profile names. This implementation returns "default". Subclasses may override in order to customize the set of reserved names. -
getActiveProfiles
从接口复制的说明:EnvironmentReturn the set of profiles explicitly made active for this environment. Profiles are used for creating logical groupings of bean definitions to be registered conditionally, for example based on deployment environment. Profiles can be activated by setting "infra.profiles.active" as a system property or by callingConfigurableEnvironment.setActiveProfiles(String...).If no profiles have explicitly been specified as active, then any default profiles will automatically be activated.
-
doGetActiveProfiles
Return the set of active profiles as explicitly set throughsetActiveProfiles(java.lang.String...)or if the current set of active profiles is empty, check for the presence ofdoGetActiveProfilesProperty()and assign its value to the set of active profiles. -
doGetActiveProfilesProperty
Return the property value for the active profiles. -
setActiveProfiles
从接口复制的说明:ConfigurableEnvironmentSpecify the set of profiles active for thisEnvironment. Profiles are evaluated during container bootstrap to determine whether bean definitions should be registered with the container.Any existing active profiles will be replaced with the given arguments; call with zero arguments to clear the current set of active profiles. Use
ConfigurableEnvironment.addActiveProfile(java.lang.String)to add a profile while preserving the existing set.- 指定者:
setActiveProfiles在接口中ConfigurableEnvironment- 另请参阅:
-
ConfigurableEnvironment.addActiveProfile(java.lang.String)ConfigurableEnvironment.setDefaultProfiles(java.lang.String...)cn.taketoday.context.annotation.ProfileEnvironment.KEY_ACTIVE_PROFILES
-
addActiveProfile
从接口复制的说明:ConfigurableEnvironmentAdd a profile to the current set of active profiles. -
getDefaultProfiles
从接口复制的说明:EnvironmentReturn the set of profiles to be active by default when no active profiles have been set explicitly. -
doGetDefaultProfiles
Return the set of default profiles explicitly set viasetDefaultProfiles(String...)or if the current set of default profiles consists only of reserved default profiles, then check for the presence ofdoGetActiveProfilesProperty()and assign its value (if any) to the set of default profiles. -
doGetDefaultProfilesProperty
Return the property value for the default profiles. -
setDefaultProfiles
Specify the set of profiles to be made active by default if no other profiles are explicitly made active throughsetActiveProfiles(java.lang.String...).Calling this method removes overrides any reserved default profiles that may have been added during construction of the environment.
-
acceptsProfiles
从接口复制的说明:EnvironmentDetermine whether the givenProfilespredicate matches the active profiles — or in the case of no explicit active profiles, whether the givenProfilespredicate matches the default profiles.If you wish provide profile expressions directly as strings, use
Environment.matchesProfiles(String...)instead. -
isProfileActive
Return whether the given profile is active, or if active profiles are empty whether the profile should be active by default. -
validateProfile
Validate the given profile, called internally prior to adding to the set of active or default profiles.Subclasses may override to impose further restrictions on profile syntax.
- 抛出:
IllegalArgumentException- if the profile is null, empty, whitespace-only or begins with the profile NOT operator (!).- 另请参阅:
-
getPropertySources
从接口复制的说明:ConfigurableEnvironmentReturn thePropertySourcesfor thisEnvironmentin mutable form, allowing for manipulation of the set ofPropertySourceobjects that should be searched when resolving properties against thisEnvironmentobject. The variousPropertySourcesmethods such asaddFirst,addLast,addBeforeandaddAfterallow for fine-grained control over property source ordering. This is useful, for example, in ensuring that certain user-defined property sources have search precedence over default property sources such as the set of system properties or the set of system environment variables. -
getSystemProperties
从接口复制的说明:ConfigurableEnvironmentReturn the value ofSystem.getProperties().Note that most
Environmentimplementations will include this system properties map as a defaultPropertySourceto be searched. Therefore, it is recommended that this method not be used directly unless bypassing other property sources is expressly intended.- 指定者:
getSystemProperties在接口中ConfigurableEnvironment
-
getSystemEnvironment
从接口复制的说明:ConfigurableEnvironmentReturn the value ofSystem.getenv().Note that most
Environmentimplementations will include this system environment map as a defaultPropertySourceto be searched. Therefore, it is recommended that this method not be used directly unless bypassing other property sources is expressly intended.- 指定者:
getSystemEnvironment在接口中ConfigurableEnvironment
-
suppressGetenvAccess
protected boolean suppressGetenvAccess()Determine whether to suppressSystem.getenv()/System.getenv(String)access for the purposes ofgetSystemEnvironment().If this method returns
true, an empty dummy Map will be used instead of the regular system environment Map, never even trying to callgetenvand therefore avoiding security manager warnings (if any).The default implementation checks for the "infra.getenv.ignore" system property, returning
trueif its value equals "true" in any case. -
merge
从接口复制的说明:ConfigurableEnvironmentAppend the given parent environment's active profiles, default profiles and property sources to this (child) environment's respective collections of each.For any identically-named
PropertySourceinstance existing in both parent and child, the child instance is to be preserved and the parent instance discarded. This has the effect of allowing overriding of property sources by the child as well as avoiding redundant searches through common property source types, e.g. system environment and system properties.Active and default profile names are also filtered for duplicates, to avoid confusion and redundant storage.
The parent environment remains unmodified in any case. Note that any changes to the parent environment occurring after the call to
mergewill not be reflected in the child. Therefore, care should be taken to configure parent property sources and profile information prior to callingmerge.- 指定者:
merge在接口中ConfigurableEnvironment- 参数:
parent- the environment to merge with- 另请参阅:
-
cn.taketoday.context.support.AbstractApplicationContext#setParent
-
getConversionService
从接口复制的说明:ConfigurablePropertyResolverReturn theConfigurableConversionServiceused when performing type conversions on properties.The configurable nature of the returned conversion service allows for the convenient addition and removal of individual
Converterinstances:ConfigurableConversionService cs = env.getConversionService(); cs.addConverter(new FooConverter()); -
setConversionService
从接口复制的说明:ConfigurablePropertyResolverSet theConfigurableConversionServiceto be used when performing type conversions on properties.Note: as an alternative to fully replacing the
ConversionService, consider adding or removing individualConverterinstances by drilling intoConfigurablePropertyResolver.getConversionService()and calling methods such as#addConverter. -
setPlaceholderPrefix
从接口复制的说明:ConfigurablePropertyResolverSet the prefix that placeholders replaced by this resolver must begin with. -
setPlaceholderSuffix
从接口复制的说明:ConfigurablePropertyResolverSet the suffix that placeholders replaced by this resolver must end with. -
setValueSeparator
从接口复制的说明:ConfigurablePropertyResolverSpecify the separating character between the placeholders replaced by this resolver and their associated default value, ornullif no such special character should be processed as a value separator. -
setEscapeCharacter
从接口复制的说明:ConfigurablePropertyResolverSpecify the escape character to use to ignore placeholder prefix or value separator, ornullif no escaping should take place. -
setIgnoreUnresolvableNestedPlaceholders
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders) 从接口复制的说明:ConfigurablePropertyResolverSet whether to throw an exception when encountering an unresolvable placeholder nested within the value of a given property. Afalsevalue indicates strict resolution, i.e. that an exception will be thrown. Atruevalue indicates that unresolvable nested placeholders should be passed through in their unresolved ${...} form.Implementations of
PropertyResolver.getProperty(String)and its variants must inspect the value set here to determine correct behavior when property values contain unresolvable placeholders. -
setRequiredProperties
从接口复制的说明:ConfigurablePropertyResolverSpecify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties(). -
addRequiredProperties
从接口复制的说明:ConfigurablePropertyResolverSpecify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties(). -
validateRequiredProperties
从接口复制的说明:ConfigurablePropertyResolverValidate that each of the properties specified byConfigurablePropertyResolver.setRequiredProperties(java.lang.String...)is present and resolves to a non-nullvalue.- 指定者:
validateRequiredProperties在接口中ConfigurablePropertyResolver- 抛出:
MissingRequiredPropertiesException- if any of the required properties are not resolvable.
-
containsProperty
从接口复制的说明:PropertyResolverReturn whether the given property key is available for resolution, i.e. if the value for the given key is notnull.- 指定者:
containsProperty在接口中PropertyResolver
-
getProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key, ornullif the key cannot be resolved.- 指定者:
getProperty在接口中PropertyResolver- 参数:
key- the property name to resolve- 另请参阅:
-
getProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key, ordefaultValueif the key cannot be resolved.- 指定者:
getProperty在接口中PropertyResolver- 参数:
key- the property name to resolvedefaultValue- the default value to return if no value is found- 另请参阅:
-
getProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key, ornullif the key cannot be resolved.- 指定者:
getProperty在接口中PropertyResolver- 参数:
key- the property name to resolvetargetType- the expected type of the property value- 另请参阅:
-
getProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key, ordefaultValueif the key cannot be resolved.- 指定者:
getProperty在接口中PropertyResolver- 参数:
key- the property name to resolvetargetType- the expected type of the property valuedefaultValue- the default value to return if no value is found- 另请参阅:
-
getRequiredProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key (nevernull).- 指定者:
getRequiredProperty在接口中PropertyResolver- 抛出:
IllegalStateException- if the key cannot be resolved- 另请参阅:
-
getRequiredProperty
从接口复制的说明:PropertyResolverReturn the property value associated with the given key, converted to the given targetType (nevernull).- 指定者:
getRequiredProperty在接口中PropertyResolver- 抛出:
IllegalStateException- if the given key cannot be resolved
-
resolvePlaceholders
从接口复制的说明:PropertyResolverResolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value are ignored and passed through unchanged.- 指定者:
resolvePlaceholders在接口中PropertyResolver- 参数:
text- the String to resolve- 返回:
- the resolved String (never
null) - 另请参阅:
-
resolveRequiredPlaceholders
从接口复制的说明:PropertyResolverResolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value will cause an IllegalArgumentException to be thrown.- 指定者:
resolveRequiredPlaceholders在接口中PropertyResolver- 返回:
- the resolved String (never
null) - 抛出:
IllegalArgumentException- if given text isnullor if any placeholders are unresolvable
-
iterator
从接口复制的说明:IterablePropertyResolverReturns an iterator of property-names.- 指定者:
iterator在接口中Iterable<String>- 指定者:
iterator在接口中IterablePropertyResolver- 返回:
- a property-names Iterator.
-
toString
-