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都是可以使用的。
其实我还没想到在平时接触的业务中什么样的业务会使用到这种转换,先备着吧,万一以后可以用到呢!!!
-------------你知道的越多,不知道的越多----------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
New Features FormHttpMessageConverter should support non-String form values [SPR-17645] #22174 spring-jcl routes logging inefficiently against SLF4J with log4j-to-slf4j setup [SPR-17586] #22118 Allow java.time types for setting the Last-Modified header [SPR-17571] #22103 StringHttpMessageConverter should assume charset UTF-8 for application/json [SPR-17568] #22100 NettyDataBufferFactory.join should return original buffer as-is in case of a single element (for compatibility with Netty 4.1.32) [SPR-17560] #22092 Use Netty's optimized UTF-8 encoding if available [SPR-17558] #22090 Equals checks to MediaType.ALL should not be affected the presence of parameters [SPR-17550] #22082 Reactive HTTP header adapters don't print header values in toString [SPR-17546] #22078 Add ability to retrieve associated ClientRequest from WebClientResponseException [SPR-17087] #21624 beetle Bug Fixes HttpHeaders.EMPTY is not immutable [SPR-17633] #22164 UriComponentsBuilder.toUriString() is broken [SPR-17630] #22161 MethodParameter.isOptional() fails with ArrayIndexOutOfBoundsException [SPR-17629] #22160 MockMvcResultMatchers.forwardedUrl argument is not declared as nullable [SPR-17623] #22155 Cannot convert from Collection to RegularEnumSet [SPR-17619] #22151 TomcatHttpHandlerAdapter is not aware of Server[Request|Response]Wrapper [SPR-17611] #22143 ChannelSendOperator does not propagate cancel signal to the server [SPR-17609] #22141 @Value Optional<...> field injection fails in case of registered ConversionService [SPR-17607] #22139 @Profile mishandles "not" operand mixed with "&" [SPR-17606] #22138 SpEL, error parsing big InlineMap [SPR-17605] #22137 Composed RequestPredicates have invalid string representation [SPR-17594] #22126 Regression: IllegalStateException: Ambiguous handler methods is thrown for explicit HEAD mapping [SPR-17593] #22125 Exporting a lazily initialized bean (which implements SelfNaming and is annotated with ManagedResource annotation) gives IllegalStateException [SPR-17592] #22124 MockHttpServletRequest changes Accept-Language header values [SPR-17566] #22098 SpEL variable evaluation fails with NPE against ConcurrentHashMap [SPR-17565] #22097 WebClient logs "Only one connection receive subscriber allowed" when response status is an error [SPR-17564] #22096 Potential resource leak in DataSourceUtils.doGetConnection [SPR-17559] #22091 HibernateTransactionManager (unintentionally) bound to Hibernate 5.2 SharedSessionContractImplementor [SPR-17557] #22089 Spring JavaMailSenderImpl does not show proper message when recipient list is empty [SPR-17540] #22072 'default-lazy-init' attribute is not processed when XSD validation is disabled [SPR-8335] #12983

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值