spring占位符

本文详细介绍了Spring中${}和@Value占位符的解析过程,涉及PropertySource、PropertySources、PropertyResolver和PropertiesLoaderSupport等核心组件。PropertySource作为键值对存储,PropertySources是其集合,PropertyResolver用于解析占位符并进行类型转换,而PropertiesLoaderSupport的子类PropertySourcesPlaceholderConfigurer实现了占位符的处理和Environment的接入。
摘要由CSDN通过智能技术生成

spring中使用的${} 以及@Value 注解的占位符,都在是spring容器初始化bean前,通过反射及类型转化把 占位符上的值转化为真实的值。

主要使用了 PropertySource、 PropertySources、PropertyResolver(PropertySourcesPropertyResolver)、PropertiesLoaderSupport(PropertySourcesPlaceholderConfigurer)类。

1、PropertySource

这个是org.springframework.core.env. PropertySource 抽象类,不是org.springframework.context.annotation.PropertySource注解哈。这个基类用来存key/value 键值对的,像Map、Properties类型的。

首先说下,这两个静态变量,因为定义为final 类型的 必须在构造方法中赋值。

这里的name 和 PropertSource 是有对应关系的,我们看下PropertySource 重写的equeals方法:

只要两个PropertSource 类的name值一样,那么就认为这是两个相同的PropertSource 对象。这就像我们使用@ PropertSource 注解在加载文件时,name就相当于被加载.prop文件的全路径名,如果两个文件名一样,那么就认为对应文件的内容是一样的。

      在说这里的PropertSource<T> 抽象类,可以传入一个泛型的对象,这个对象用来处理这个抽象类的抽象方法:

/**
	 * Return the value associated with the given name,
	 * or {@code null} if not found.
	 * @param name the property to find
	 * @see PropertyResolver#getRequiredProperty(String)
	 */
	public abstract Object getProperty(String name);

像 MapPropertySource 类 传入  Map<String, Object> 对象,从而在getProperty方法里通过source.get(key) 的方式实现这个方法。

像PropertySourcesPlaceholderConfigurer类的postProcessBeanFactory方法中 通过传入Environment 类型的对象 ,通过Environment对象的source.getProperty(key)

方式实现这个方法。

 

2、PropertySources

package org.springframework.core.env;

/**
 * Holder containing one or more {@link PropertySource} objects.
 *
 * @author Chris Beams
 * @since 3.1
 */
public interface PropertySources extends Iterable<PropertySource<?>> {

	/**
	 * Return whether a property source with the given name is contained.
	 * @param name the {@linkplain PropertySource#getName() name of the property source} to find
	 */
	boolean contains(String name);

	/**
	 * Return the property source with the given name, {@code null} if not found.
	 * @param name the {@linkplain PropertySource#getName() name of the property source} to find
	 */
	PropertySource<?> get(String name);

}

这接口相当于PropertySource 的集合类,为PropertySource 服务的。</

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值