Spring Boot Sping Boot 转换器不适用于配置属性

h6my8fg2  于 7个月前  发布在  Spring
关注(0)|答案(1)|浏览(67)

我试图提供一个转换器,将application.yml中的r,g,B元组转换为Color对象。属性解析失败,并显示:“org.springframework.core.convert.ConverterNotFoundException:No converter found capable of converting from type [java.lang.String] to type [java.awt.Color]”
转换器是这样声明的:

@Component
public static class ColorConverter implements Converter<String, Color>{
    @Override
    public Color convert(String source) {
      ...
    }
}

字符串

我的环境:

Spring Boot 2.7
应用注解;

@SpringBootApplication(scanBasePackages = {"my.packages"})
@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)
@EnableSpringConfigured


应用程序 * 确实 * 有一个WebMvcConfigurer子类

我还试过什么:

1.向应用程序类中添加@ EnableProximationProperties。这没有区别。
1.说明转换器在启动期间注册(我在zeroarg构造函数上放置了一个断点)
1.在WebMvcConfigurer中注册转换器(这没有意义-我需要的转换器是属性解析,而不是mvc调用-但我还是尝试了)。这没有什么区别。

4dbbbstv

4dbbbstv1#

终于找到了正确的谷歌搜索词。
解决方案是用@ conversion properties Binding注解Convert类:

@ConfigurationPropertiesBinding
@Component
public static class ColorConverter implements Converter<String, Color>{
}

字符串
@ EnablePractitionProperties注解没有什么区别-听起来像是Sping Boot 自动包含的(ref:https://www.baeldung.com/spring-enable-config-properties

相关问题