springboot源码分析6-springboot之PropertySource类初探

来源:互联网 发布:上海同济大学网络教育 编辑:程序博客网 时间:2024/05/31 06:21

摘要:本小节重点梳理一下PropertySource类的相关结构以及职责,本文的学习前提是学习了springboot源码分析5-springboot之命令行参数以及原理一文。

springboot源码分析5-springboot之命令行参数以及原理一文中,我们看到了实例化Source类的时候,会去先实例化其父类SimpleCommandLinePropertySource。SimpleCommandLinePropertySource类的构造函数中直接解析了命令行参数以及值,然后返回封装好的CommandLineArgs实例对象。SimpleCommandLinePropertySource类的构造函数中会继续调用其父类CommandLinePropertySource,CommandLinePropertySource继续调用其父类EnumerablePropertySource,EnumerablePropertySource继续调用其父类PropertySource。PropertySource类是一个抽象基类。

1.1. PropertySource

PropertySource类是一个抽象类,代码如下:

1 public abstract class PropertySource<T> {

2 protected final String name;

3 protected final T source;

4 }

getSource()方法:这个方法会返回得到属性源的源头。比如MapPropertySource的源头就是一个MapPropertiesPropertySource的源头就是一个Properties

name:我们可以理解为一个map中的key

PropertySource类的继承结构图如下图所示:

 

l RandomValuePropertySourcesourcerandom

l ServletConfigPropertySourcesourceServletConfig

l ServletContextPropertySourcesourceServletContext

l JndiPropertySourcesourceJndiLocatorDelegate

l StubPropertySourcesourceObject

l MapPropertySourcesourceMap<String, Object>

本文内容暂时讲解道这里,后面的文章中讲解环境变量的时候,再来一个个的看这些PropertySource子类。

未知参数最终被添加到了nonOptionArgs集合。

讲解到这里基本上命令行参数的设置以及解析原理都搞明白了。


欢迎关注我的微信公众号,第一时间获得博客更新提醒,以及更多成体系的Java相关原创技术干货。 
扫一扫下方二维码或者长按识别二维码,即可关注。
 


阅读全文
0 0
原创粉丝点击