Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
说明:在Spring MVC和Spring Boot中都能正常使用。
首先,我实现了一个自定义的注解,@Trimmed去除字符串String的前后空格。
如果是在Spring MVC的XML配置中,可以这样写:
<bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="formatters">
<list>
<bean class="com.benben.timetable.common.formatter.TrimmedAnnotationFormatterFactory"/>
</list>
</property>
</bean>
但是,如果是基于注解类型去配置以上的配置时,我们通常会有以下的几种形式去配置,应该是第一反应会这样去封装:
@Configuration
public class SpringConfig{ @Bean
public FormattingConversionServiceFactoryBean conversionService() {
FormattingConversionServiceFactoryBean conversionService = new FormattingConversionServiceFactoryBean();
Set<TrimmedAnnotationFormatterFactory> fromatters = new HashSet<TrimmedAnnotationFormatterFactory>();
fromatters.add(new TrimmedAnnotationFormatterFactory());
conversionService.setFormatters(fromatters);
return conversionService;
} @Bean
public FormattingConversionServiceFactoryBean conversionService() {
FormattingConversionServiceFactoryBean conversionService = new FormattingConversionServiceFactoryBean();
Set<TrimmedAnnotationFormatterFactory> formatters = new HashSet<TrimmedAnnotationFormatterFactory>();
formatters.add(new TrimmedAnnotationFormatterFactory());
conversionService.setFormatters(formatters);
return conversionService;
} @Bean
public ConversionService conversionService() {
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService( false );
conversionService.addFormatterForFieldAnnotation(new TrimmedAnnotationFormatterFactory());
return conversionService;
} }
但是很遗憾,以上的方式会正常注入,就是不起作用,完全无效。
经过排查和研究,如果是继承WebMvcConfigurerAdapter来重写addFormatters,然后把自定义的Factory加入进去,那么这样是成功的,也能正常处理,写法如下:
@Configuration
public class SpringConfig extends WebMvcConfigurerAdapter{ @Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldAnnotation(new TrimmedAnnotationFormatterFactory());
super.addFormatters(registry);
} }
就是不明白为什么会这样,虽然是能用,但是未能找到合理的解释。
参考:
https://github.com/spring-projects/spring-boot/issues/6222
Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)的更多相关文章
- spring的纯注解的IOC配置
package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- spring-第十七篇之spring AOP基于注解的零配置方式
1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...
- 10 Spring框架--基于注解的IOC配置
1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...
- Spring boot 的 properties 属性值配置 application.properties 与 自定义properties
配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...
- spring的基于注解的IOC配置
1.配置文件配置 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http: ...
- Spring AOP基于注解的“零配置”方式实现
为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...
- 阶段3 2.Spring_06.Spring的新注解_1 spring的新注解-Configuration和ComponentScan
解决测试类重复代码的问题,xml还是存在的问题,没法脱离xml文件 要想在QueryRunner上加注解,是加不了的 创建工程 复制依赖项到pom.xml 复制注解的工程里面的com文件夹 配置文件b ...
- 如何开启spring框架以注解形式的配置
步骤 导包(新版本需要导入spring-aop-4.3.17.RELEASE.jar) 为配置文件applicationContext.xml引入新的命名空间(约束) 开启使用注解 <?xml ...
随机推荐
- C#知识点-GDI绘图
一.开发环境 编译器:VS2013 .Net版本:4.5 二.开发过程 1.画一条直线 private void btnDrawLine_Click(object sender, EventArgs ...
- PS如何批量整理图片大下
https://jingyan.baidu.com/article/cdddd41cc7849853cb00e193.html
- LN : leetcode 338 Counting Bits
lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...
- 安装linux mint 18.3 后要做的
使用u盘安装的linux mint 18.3,安装过程基本顺利 发现在安装过程中使用中文语言的话会使得下载附加软件的速度快很多 安装完成之后要做的事情有: 1.字体 默认的楷体字比较难看,在软件管理器 ...
- Selenium示例集锦--常见元素识别方法、下拉框、文本域及富文本框、鼠标操作、一组元素定位、弹窗、多窗口处理、JS、frame、文件上传和下载
元素定位及其他操作 0.常见的识别元素的方法是什么? driver.find_element_by_id() driver.find_element_by_name() driver.find_ele ...
- ubantu MongoDB安装
转 https://blog.csdn.net/flyfish111222/article/details/51886787 本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的 ...
- 由DB2分页想到的,关于JDBC ResultSet 处理大数据量
最近在处理DB2 ,查询中,发现如下问题.如果一个查询 count(*),有几十万行,分页如何实现 select row_number() over (order by fid desc ) as r ...
- 新开的坑-python学习笔记(1)——连接符与转义
1/print输出格式虽然知道怎么用却还要打破砂锅的问问题... 问题答案是 需要读很多基础文档教程 --------例如 "+" . "," 作为连接符的作 ...
- TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
二值掩膜输出依据种类预测分支(Faster R-CNN部分)预测结果:当前RoI的物体种类为i第i个二值掩膜输出就是该RoI的损失Lmask 对于预测的二值掩膜输出,我们对每个像素点应用sigmoid ...
- Win7访问不了WINXP共享文件
用win xp的机器可以访问,但用win 7的机器无法访问共享文件 提示:您没有权限访问.请与网络管理员联系请求访问权限 网上找了相应的资料 做了如下动作 1. 打开网上邻居→本地连接→属性里,“看是 ...