SpringBoot之PropertyResolver深度解析

最开始的PropertyResolver用于解析各种属性源的属性解析器,我们看一下它需要实现的方法:

SpringBoot之PropertyResolver深度解析

那么ConfigurablePropertyResolver是啥呢?我们看它的注释:

SpringBoot之PropertyResolver深度解析

相当于在PropertyResolver的基础之上增加了值转换功能,我们看一下它有哪些方法:

SpringBoot之PropertyResolver深度解析

我们再来看它的实现者AbstractPropertyResolver,这个抽象类实现了ConfigurablePropertyResolver接口和PropertyResolver接口的方法。

我们看看它的几个特殊的属性:

SpringBoot之PropertyResolver深度解析

占位符前缀和后缀等,

SpringBoot之PropertyResolver深度解析

属性转换服务,我们看它的getConversionService方法实现:

SpringBoot之PropertyResolver深度解析

我们再来看getProperty方法:

SpringBoot之PropertyResolver深度解析

带有2个参数的getProperty方法是没有被实现的,这也就是说等待子类自己实现,这个方法很重要。

再来看另外一个方法:

SpringBoot之PropertyResolver深度解析

这里的代码如下:

SpringBoot之PropertyResolver深度解析

很明显它是一个帮助类。

真正的替换操作是通过下面的方法进行的:

SpringBoot之PropertyResolver深度解析

我们看这个方法:

SpringBoot之PropertyResolver深度解析

我们来看AbstractPropertyResolver的子类PropertySourcesPropertyResolver,如下:

他有一个属于自己的属性,所以直觉告诉我们,当前类的主角就是它了:

SpringBoot之PropertyResolver深度解析

我们看这个类实现的方法:

SpringBoot之PropertyResolver深度解析

我们再来看PropertySources是何许人也,我们看它的定义:

SpringBoot之PropertyResolver深度解析

从这里可以看到,PropertySources实际上是一个PropertySource类的容器,PropertySource就是一个真正的属性源了。

SpringBoot之PropertyResolver深度解析

它的核心属性T source,这个source就是最为核心的属性了,我们看或者source是啥?

SpringBoot之PropertyResolver深度解析

PropertySource类的核心方法为:

SpringBoot之PropertyResolver深度解析

getProperty方法取决于具体的source,所以是没有实现的。

再一次回到PropertySources中,我们看它的子类EnumerablePropertySource.这个类很简单。

它的核心方法为:

SpringBoot之PropertyResolver深度解析

getPropertyNames方法返回了当前source中所有的包含的属性名。

我们看EnumerablePropertySource的子类CommandLinePropertySource。注意了它的source还是未知的。

我们看它到底有哪些方法:

SpringBoot之PropertyResolver深度解析

有一个东西需要注意以下,就是红线标出来的部分,非选项参数是啥(简单理解就是参数不是以为-或者是--开头)。

这个就是非选项参数默认的属性名。

SpringBoot之PropertyResolver深度解析

我们看当前类最为核心的方法getProperty方法:

SpringBoot之PropertyResolver深度解析

还没说明的一点是CommandLinePropertySource是一个抽象类,哈哈。我们看它的几个核心方法:

SpringBoot之PropertyResolver深度解析

我们来看一下它有哪些子类:

SpringBoot之PropertyResolver深度解析

我们分析一个SimpleCommandLinePropertySource类,我们看它的定义:

SpringBoot之PropertyResolver深度解析

注意这里的模板参数为CommandLineArgs,也就是当前PropertySource的属性源。

我们看CommandLineArgs这个类的定义,命令行参数包括选项参数非选项参数

SpringBoot之PropertyResolver深度解析

我们再来看SimpleCommandLinePropertySource类,它的构造方法如下:

SpringBoot之PropertyResolver深度解析

SimpleCommandLineArgsParser类是一个命令行参数解析器,这个类我们不用分析,我们所要知道的是它的parse方法返回的是一个CommandLineArgs类的对象。