前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring:property-placeholder

spring:property-placeholder

作者头像
MickyInvQ
发布2021-10-19 14:38:49
5900
发布2021-10-19 14:38:49
举报
文章被收录于专栏:InvQ的专栏

文章目录

property-placeholder

占位符的用法,详见https://zetcode.com/spring/propertyplaceholder/

解析

解析的实现类是PropertyPlaceholderBeanDefinitionParser,此类的父类继承体系和property-override的PropertyOverrideBeanDefinitionParser完全一样,所以整体的处理套路也是基本一致。为什么会一致呢,查看此配置拥有的属性就会发现,和property-override很多都是一样的,所以这里只对不一样的而进行说明。

PropertyPlaceholderBeanDefinitionParser.doParse:

代码语言:javascript
复制
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
    super.doParse(element, builder);
    builder.addPropertyValue("ignoreUnresolvablePlaceholders",
            Boolean.valueOf(element.getAttribute("ignore-unresolvable")));
    String systemPropertiesModeName = element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE);
    if (StringUtils.hasLength(systemPropertiesModeName) &&
            !systemPropertiesModeName.equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
        builder.addPropertyValue("systemPropertiesModeName", "SYSTEM_PROPERTIES_MODE_"
            + systemPropertiesModeName);
    }
    if (element.hasAttribute("value-separator")) {
        builder.addPropertyValue("valueSeparator", element.getAttribute("value-separator"));
    }
    if (element.hasAttribute("trim-values")) {
        builder.addPropertyValue("trimValues", element.getAttribute("trim-values"));
    }
    if (element.hasAttribute("null-value")) {
        builder.addPropertyValue("nullValue", element.getAttribute("null-value"));
    }
}

system-properties-mode

Spring会将java的System.getProperties也当做属性的来源,此配置用于设置系统的和本地文件的同名属性的覆盖方式(谁覆盖谁),自己看文档去。

value-separator

用于配置默认的值的分隔符:

代码语言:javascript
复制
<bean id="student" class="base.Student">
    <property name="name" value="${student.name:skywalker}" />
</bean>

如果属性文件里没有student.name,那么就是skywalker。默认就是:。

null-value

遇到哪些值应该当做空处理,比如可以把空串""设为这个,默认不对任何值进行处理。

trim-values

是否移除开头和结尾的空格,按理说应该是布尔值,但是Spring没有提供可以选择的值,经过测试发现设为true或是false都会把空格干掉,不知道什么鬼。

BeanDefinition

这次是PropertySourcesPlaceholderConfigurer,其类图:

在这里插入图片描述
在这里插入图片描述

运行

PropertySourcesPlaceholderConfigurer.postProcessBeanFactory:

代码语言:javascript
复制
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertySources == null) {
        this.propertySources = new MutablePropertySources();
        if (this.environment != null) {
            this.propertySources.addLast(
                new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, 
                    this.environment) {
                    @Override
                    public String getProperty(String key) {
                        return this.source.getProperty(key);
                    }
                }
            );
        }
        PropertySource<?> localPropertySource =
                new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
        if (this.localOverride) {
            this.propertySources.addFirst(localPropertySource);
        }
        else {
            this.propertySources.addLast(localPropertySource);
        }
    }
    processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
    this.appliedPropertySources = this.propertySources;
}

从源码中可以看出,如果其内部的propertySources属性不为空(当然默认是空),那么属性文件和系统属性都会被忽略。它的使用场景应该是这样:

不使用property-placeholder标签,以显式的bean定义代替。

处理

处理的过程就是遍历全部BeanDefinition,替换${},不再详细进行详细说明。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/10/15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • property-placeholder
    • 解析
      • system-properties-mode
      • value-separator
      • null-value
      • trim-values
      • BeanDefinition
    • 运行
      • 处理
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档