HttpServletBean 源码阅读

来源:互联网 发布:淘宝特步 编辑:程序博客网 时间:2024/06/06 17:26
public abstract class HttpServletBean extends HttpServlet implements EnvironmentCapable, EnvironmentAware 

HttpServletBean继承自HttpServlet,实现了EnvironmentCapable, EnvironmentAware 接口。

成员变量

有两个类成员变量:

    @Nullable    private ConfigurableEnvironment environment;    private final Set<String> requiredProperties = new HashSet<>(4);

ConfigurableEnvironment

public interface ConfigurableEnvironment extends Environment, ConfigurablePropertyResolver

Configuration 接口大多数(如果不是全部)由Environment类型来实现。 该接口提供工具用以①设置活跃和默认的profiles和②操纵底层property sources。 允许客户端设置和验证所需的(required)属性,通过超级接口ConfigurablePropertyResolver自定义转换服务。

property sources可能会被删除,重新排序或替换; 并且可以使用从getPropertySources()返回的MutablePropertySources实例添加其他属性来源。

ApplicationContext正在使用Environment时,一定要在contextorg.springframework.context.support.AbstractApplicationContext#refresh() 被调用之前执行。 这可以确保所有property sources在容器引导过程中都可用,包括由org.springframework.context.support.PropertySourcesPlaceholderConfigurer

类方法

setActiveProfiles

setActiveProfiles 由实现抽象类AbstractEnvironment 实现:

@Overridepublic void setActiveProfiles(String... profiles) {    Assert.notNull(profiles, "Profile array must not be null");    synchronized (this.activeProfiles) {        this.activeProfiles.clear();        for (String profile : profiles) {            validateProfile(profile);            this.activeProfiles.add(profile);        }    }}

其中:

rivate final Set<String> activeProfiles = new LinkedHashSet<>();

protected void validateProfile(String profile) {    if (!StringUtils.hasText(profile)) {        throw new IllegalArgumentException("Invalid profile [" + profile + "]: must contain text");    }    if (profile.charAt(0) == '!') {        throw new IllegalArgumentException("Invalid profile [" + profile + "]: must not begin with ! operator");    }}
@Overridepublic void addActiveProfile(String profile) {    if (logger.isDebugEnabled()) {        logger.debug("Activating profile '" + profile + "'");    }    validateProfile(profile);    doGetActiveProfiles();    synchronized (this.activeProfiles) {        this.activeProfiles.add(profile);    }}
原创粉丝点击