Environment
Environment
表示应用当前所运行的环境。
Interface representing the environment in which the current application is running. Models two key aspects of the application
environment: profiles and properties. Methods related to property access are exposed via thePropertyResolver
superinterface.A profile is a named, logical group of bean definitions to be registered with the container only if the given profile is active. Beans may be assigned to a profile whether defined in XML or via annotations; see the spring-beans 3.1 schema or the
@Profile
annotation for syntax details. The role of theEnvironment
object with relation to profiles is in determining which profiles (if any) are currently active, and which profiles (if any) should be active by default.Properties play an important role in almost all applications, and may originate from a variety of sources: properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects, Maps, and so on. The role of the environment object with relation to properties is to provide the user with a convenient service interface for configuring property sources and resolving properties from them
从Environment
注释上可以看到,Environment
主要负责处理两个方面:profiles
和properties
profiles
类似Maven中的profiles
,Spring允许配置多个profile
,但只加载激活的profile
,通过profiles
配置,可以实现不同环境使用不同的配置(例如不同环境使用不同的数据库配置)
properties
在整个应用中都会用到,Spring会从各种来源加载:properties文件(例如数据库配置文件),JVM系统配置,系统环境变量,Servlet上下文参数等。通过properties
,可以更加便捷的获取到配置信息
StandardEnvironment
Environment
Environment
定义了Profiles
相关的方法:
1 | public interface Environment extends PropertyResolver { |
ConfigurableEnvironment
如果说Environment
定义了Profiles
查询方法,那么ConfigurableEnvironment
就定义了设置方法:
1 | public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver { |
AbstractEnvironment
AbstractEnvironment
对接口做了默认实现:
1 | public abstract class AbstractEnvironment implements ConfigurableEnvironment { |
StandardEnvironment
1 | public class StandardEnvironment extends AbstractEnvironment { |