说明:在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://stackoverflow.com/questions/23062337/configuring-a-conversion-service-with-a-custom-dateformatter

https://github.com/spring-projects/spring-boot/issues/6222

Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)的更多相关文章

  1. spring的纯注解的IOC配置

    package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...

  2. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  3. spring-第十七篇之spring AOP基于注解的零配置方式

    1.基于注解的零配置方式 Aspect允许使用注解定义切面.切入点和增强处理,spring框架可以识别并根据这些注解来生成AOP代理.spring只是用了和AspectJ 5一样的注解,但并没有使用A ...

  4. 10 Spring框架--基于注解的IOC配置

    1.工程环境搭建 2.基于注解的IOC配置 IOC注解的分类 (1)用于创建对象的 他们的作用就和在XML配置文件中编写一个<bean>标签实现的功能是一样的@Component: 作用: ...

  5. Spring boot 的 properties 属性值配置 application.properties 与 自定义properties

    配置属性值application.properties 文件直接配置: com.ieen.super.name="MDD" 自定义properties文件配置:src/main/r ...

  6. spring的基于注解的IOC配置

    1.配置文件配置 <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http: ...

  7. Spring AOP基于注解的“零配置”方式实现

    为了在Spring中启动@AspectJ支持,需要在类加载路径下新增两个AspectJ库:aspectjweaver.jar和aspectjrt.jar.除此之外,Spring AOP还需要依赖一个a ...

  8. 阶段3 2.Spring_06.Spring的新注解_1 spring的新注解-Configuration和ComponentScan

    解决测试类重复代码的问题,xml还是存在的问题,没法脱离xml文件 要想在QueryRunner上加注解,是加不了的 创建工程 复制依赖项到pom.xml 复制注解的工程里面的com文件夹 配置文件b ...

  9. 如何开启spring框架以注解形式的配置

    步骤 导包(新版本需要导入spring-aop-4.3.17.RELEASE.jar) 为配置文件applicationContext.xml引入新的命名空间(约束) 开启使用注解 <?xml ...

随机推荐

  1. 51nod 1029 大数除法

    1029 大数除法 基准时间限制:4 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出2个大整数A,B,计算A / B和A Mod B的结果. Input 第1 ...

  2. linux学习之路5 系统常用命令

    日期时间 查看设置当前时间 date +%Y--%m--%d 格式化显示时间 -s " "(切换到超级用户)修改时间 hwclock(clock)用以显示硬件时钟时间 命令 cal ...

  3. 在计算机视觉与人工智能领域,顶级会议比SCI更重要(内容转)

    很多领域,SCI是王道,尤其在中国,在教师科研职称评审和学生毕业条件中都对SCI极为重视,而会议则充当了补充者的身份.但是在计算机领域,尤其是人工智能与机器学习领域里,往往研究者们更加青睐于会议 我无 ...

  4. .Net实战之反射相关类之间的人体经络关系

    --1.类的部分组成成员 --2.巧记成员之间的关系 [MyTable("T_UserInfo")] public class UserInfo : Person, UserSer ...

  5. document.write清除原有内容情况

    原博客: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  6. js中true和false判断

    布尔类型里只有这6参数个返回false,其它都为true Boolean(false) // false Boolean(undefined) // false Boolean(null) // fa ...

  7. Android 比SwipeRefreshLayout更漂亮和强大的下拉刷新控件:Android-MaterialRefreshLayout

    这是一个下拉刷新的控件,它比SwipeRefreshLayout更加漂亮和强大.它易于使用并且支持API LEVEL >= 8.希望你能够喜欢. Now let me talk about Ma ...

  8. grunt 全局使用

    grunt 不同地方使用时需要将插件下载的当前文件夹,这是因为查找模块时是当前路径,这会造成多个工程使用时会需要下载多次,而这些东西又不应该存在于工程之中,所以应该将所有模块全局安装,然后在工程下面只 ...

  9. dubbo之日志适配及访问日志

    日志适配 自 2.2.1 开始,dubbo 开始内置 log4j.slf4j.jcl.jdk 这些日志框架的适配 1,也可以通过以下方式显示配置日志输出策略: 命令行 java -Ddubbo.app ...

  10. Spring Boot 缓存的基本用法

    目录 一.目的 二.JSR-107 缓存规范 三.Spring 缓存抽象 四.Demo 1.使用 IDEA 创建 Spring Boot 项目 2.创建相应的数据表 3.创建 Java Bean 封装 ...