ConversionService转换服务使用

前言

在最近分析和写的SpringBoot源码分析(面试官:你说说Springboot的启动过程吧(5万字分析启动过程))中,给自己留了一个关于ConversionService的使用的作业,这不就来补作业了。

使用出处

这个转换服务我这里的例子很简单,就是将String的数字转为Integer,使用方法来自于源码,这个图在我写的源码分析的文章中的B.2.1处理上下文也有
在这里插入图片描述
这个注释里是从env中拿的ConversionService,那env是啥,就是环境的简写,在分析
A.6.1 获取或创建环境变量节有提到源码中几个环境变量的类的关系:

«interface»
ConfigurableEnvironment
AbstractEnvironment
«interface»
Environment
«interface»
PropertyResolver
«interface»
ConfigurablePropertyResolver
StandardEnvironment
StandardServletEnvironment
«interface»
ConfigurableWebEnvironment
ApplicationServletEnvironment

代码示例

    @Test
    public void testConversionService(){
    
    
        AbstractEnvironment se = new StandardServletEnvironment();
        AbstractEnvironment se1 = new StandardEnvironment();
        StandardServletEnvironment se2 = new StandardServletEnvironment();
        StandardEnvironment se3 = new StandardEnvironment();
        ConfigurableConversionService conversionService = se.getConversionService();
        Integer convert = conversionService.convert("111", Integer.class);
        System.out.println(convert);
    }

上面的代码结合前面的有关环境变量的类图中,我们可以new的主要是StandardServletEnvironmentStandardEnvironment,其中se1到se3都是可以使用的。
其实我还没想到在平时接触的业务中什么样的业务会使用到这种转换,先备着吧,万一以后可以用到呢!!!
-------------你知道的越多,不知道的越多----------------

猜你喜欢

转载自blog.csdn.net/fhf2424045058/article/details/128289898
今日推荐