Spring中的@PropertySource注解源码详细解析

 更新时间:2024年01月25日 11:35:09   作者:mumubili  
这篇文章主要介绍了Spring中的@PropertySource注解源码详细解析,@PropertySource注解,标注在配置类@Configuration上面,下面主要分析一下@PropertySource注解的处理过程,也就是怎么把配置信息从.properies文件放到environment中的,需要的朋友可以参考下

前言

通常,我们在开发java spring项目时,会包含多套环境(profile),并且分别提供了不同环境下的属性文件(.properties),在引用属性文件时,都会用到@PropertySource注解,标注在配置类@Configuration上面,下面主要分析一下@PropertySource注解的处理过程,也就是怎么把配置信息从.properies文件放到environment中的;

1. @PropertySource处理入口

@PropertySource使用时都会和@Configuration放在一起,对@PropertySource的处理也是放在@Configuration解析处理过程中的(对@Configuration的处理过程后面再单独进行分析),参见源码如下:

/**
	 * Apply processing and build a complete {@link ConfigurationClass} by reading the
	 * annotations, members and methods from the source class. This method can be called
	 * multiple times as relevant sources are discovered.
	 * @param configClass the configuration class being build
	 * @param sourceClass a source class
	 * @return the superclass, or {@code null} if none found or previously processed
	 */
	protected final SourceClass doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass)
			throws IOException {
		// Recursively process any member (nested) classes first
		processMemberClasses(configClass, sourceClass);
		// Process any @PropertySource annotations
		for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), PropertySources.class,
				org.springframework.context.annotation.PropertySource.class)) {
			if (this.environment instanceof ConfigurableEnvironment) {
				processPropertySource(propertySource);
			}
			else {
				logger.warn("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
						"]. Reason: Environment must implement ConfigurableEnvironment");
			}
		}
    //......其余部分省略
}

上面for循环处理的过程,实质上主要包含了两步:

  • 解析@PropertySource注解,包括单独声明的@PropertySource注解,以及在容器注解@PropertySources value属性中指定的注解;
  • 将解析的@PropertySource注解放到environment中;

下面分别对这两个过程进行分析;

2. @PropertySource注解解析

解析过程主要封装到了AnnotationConfigUtils.attributesForRepeatable(sourceClass.getMetadata(), PropertySources.class, org.springframework.context.annotation.PropertySource.class)当中,根据进去源码如下:

static Set<AnnotationAttributes> attributesForRepeatable(
			AnnotationMetadata metadata, String containerClassName, String annotationClassName) {
		Set<AnnotationAttributes> result = new LinkedHashSet<AnnotationAttributes>();
		// Direct annotation present?
		addAttributesIfNotNull(result, metadata.getAnnotationAttributes(annotationClassName, false));
		// Container annotation present?
		Map<String, Object> container = metadata.getAnnotationAttributes(containerClassName, false);
		if (container != null && container.containsKey("value")) {
			for (Map<String, Object> containedAttributes : (Map<String, Object>[]) container.get("value")) {
				addAttributesIfNotNull(result, containedAttributes);
			}
		}
		// Return merged result
		return Collections.unmodifiableSet(result);
	}
private static void addAttributesIfNotNull(Set<AnnotationAttributes> result, Map<String, Object> attributes) {
		if (attributes != null) {
			result.add(AnnotationAttributes.fromMap(attributes));
		}
	}

这里,首先获取配置类上@PropertySource注解,解析成AnnotationAttributes map对象,放到result中;

然后解析容器注解@PropertySources value属性值,并将解析的@PropertySource列表放到result中;

这样@PropertySource注解和@PropertySources容器注解解析完毕;

3. 构造ResourcePropertySource对象

这里主要分析一下processPropertySource(propertySource)的过程,源码如下:

/**
	 * Process the given <code>@PropertySource</code> annotation metadata.
	 * @param propertySource metadata for the <code>@PropertySource</code> annotation found
	 * @throws IOException if loading a property source failed
	 */
	private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
		String name = propertySource.getString("name");
		if (!StringUtils.hasLength(name)) {
			name = null;
		}
		String encoding = propertySource.getString("encoding");
		if (!StringUtils.hasLength(encoding)) {
			encoding = null;
		}
		String[] locations = propertySource.getStringArray("value");
		Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
		boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
		Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
		PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
				DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));
		for (String location : locations) {
			try {
				String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
				Resource resource = this.resourceLoader.getResource(resolvedLocation);
				addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
			}
			catch (IllegalArgumentException ex) {
				// Placeholders not resolvable
				if (ignoreResourceNotFound) {
					if (logger.isInfoEnabled()) {
						logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
					}
				}
				else {
					throw ex;
				}
			}
			catch (IOException ex) {
				// Resource not found when trying to open it
				if (ignoreResourceNotFound &&
						(ex instanceof FileNotFoundException || ex instanceof UnknownHostException)) {
					if (logger.isInfoEnabled()) {
						logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
					}
				}
				else {
					throw ex;
				}
			}
		}
	}

其中@PropertySource的主要属性value(这里放到了locations中)保存了属性文件的存放位置,对每一个location的解析主要分为如下3步:

  • 解析location中包含的占位符
  • 加载Resource对象
  • 构造ResourcePropertySource对象
  • PropertySource加载到environment当中

其中第三步构造ResourcePropertySource主要用到了PropertySourceFactory,这里默认实现是DefaultPropertySourceFactory,内部实现源码如下:

/**
 * The default implementation for {@link PropertySourceFactory},
 * wrapping every resource in a {@link ResourcePropertySource}.
 *
 * @author Juergen Hoeller
 * @since 4.3
 * @see PropertySourceFactory
 * @see ResourcePropertySource
 */
public class DefaultPropertySourceFactory implements PropertySourceFactory {
	@Override
	public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
		return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
	}
}

上面主要通过resource构造了ResourcePropertySource对象,其构造函数如下:

/**
	 * Create a PropertySource based on Properties loaded from the given resource.
	 * The name of the PropertySource will be generated based on the
	 * {@link Resource#getDescription() description} of the given resource.
	 */
	public ResourcePropertySource(EncodedResource resource) throws IOException {
		super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
		this.resourceName = null;
	}

如上,可见先是由resource构造了Peoperties对象,然后构造了PropertiesPropertySource父类.....

如下是ResourcePropertySource的继承结构,最终加载的属性值放入到了PropertySource的成员变量source中;

4. PropertySource配置加载到environment当中

构造完ResourcePropertySource对象之后,下面将该对象放入到environment中,源码如下:

private void addPropertySource(PropertySource<?> propertySource) {
		String name = propertySource.getName();
		MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
		if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
			// We've already added a version, we need to extend it
			PropertySource<?> existing = propertySources.get(name);
			PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
					((ResourcePropertySource) propertySource).withResourceName() : propertySource);
			if (existing instanceof CompositePropertySource) {
				((CompositePropertySource) existing).addFirstPropertySource(newSource);
			}
			else {
				if (existing instanceof ResourcePropertySource) {
					existing = ((ResourcePropertySource) existing).withResourceName();
				}
				CompositePropertySource composite = new CompositePropertySource(name);
				composite.addPropertySource(newSource);
				composite.addPropertySource(existing);
				propertySources.replace(name, composite);
			}
		}
		else {
			if (this.propertySourceNames.isEmpty()) {
				propertySources.addLast(propertySource);
			}
			else {
				String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
				propertySources.addBefore(firstProcessed, propertySource);
			}
		}
		this.propertySourceNames.add(name);
	}

注意,这里对于@PropertySource注解获取的配置属性放入到了environment的后面,实际在application.properties后面,也即application.properties的优先级高于@PropertySource引入的配置,后面单独对这块进行分析;

到此这篇关于Spring中的@PropertySource注解源码详细解析的文章就介绍到这了,更多相关@PropertySource注解源码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 解决IDEA中pom.xml文件变为灰色的问题

    解决IDEA中pom.xml文件变为灰色的问题

    这篇文章主要给大家介绍了如何解决IDEA中pom.xml文件变为灰色的问题,文中通过图文结合给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-12-12
  • Java方法反射实现原理详解

    Java方法反射实现原理详解

    这篇文章主要为大家详细介绍了Java方法反射的实现原理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Java 中不全部使用 Static 方法的理由

    Java 中不全部使用 Static 方法的理由

    这篇文章主要介绍了Java 中不全部使用 Static 方法的理由,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • Java基于redis和mysql实现简单的秒杀(附demo)

    Java基于redis和mysql实现简单的秒杀(附demo)

    这篇文章主要介绍了Java基于redis和mysql实现简单的秒杀(附demo),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • IDEA如何取消空格补全代码

    IDEA如何取消空格补全代码

    这篇文章主要介绍了IDEA如何取消空格补全代码的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-06-06
  • springboot实现指定mybatis中mapper文件扫描路径

    springboot实现指定mybatis中mapper文件扫描路径

    这篇文章主要介绍了springboot实现指定mybatis中mapper文件扫描路径方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • Java数组的扩容代码示例

    Java数组的扩容代码示例

    这篇文章主要介绍了Java数组的扩容,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2017-09-09
  • 一个Java的main方法在JVM中的执行流程示例详解

    一个Java的main方法在JVM中的执行流程示例详解

    main方法是Java程序的入口点,程序从这里开始执行,这篇文章主要介绍了一个Java的main方法在JVM中执行流程的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2025-09-09
  • javamail 发送邮件的实例代码分享

    javamail 发送邮件的实例代码分享

    今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题。为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用
    2013-08-08
  • SWT(JFace)体验之FillLayout布局

    SWT(JFace)体验之FillLayout布局

    FillLayout是非常简单的一种布局方式,它会以同样大小对父组件中的子组件进行布局,这些子组件将以一行或一列的形式排列。
    2009-06-06

最新评论