我正在使用Java编程配置的Spring Boot。我使用Spring的ConversionService和Spring的Converter界面的几个自定义实现。我想在配置时使用我的ConversionService bean注册所有转换器。值得注意的是,其中一些转换器具有自己的注释配置依赖关系,并且这些转换器没有连接。例如配置类是类似以下内容:弹簧引导JavaConfig与空的自动装配依赖关系
@Configuration
public class MyConfig extends WebMvcConfigurerAdapter
{
@Bean
public ConversionService conversionService(List<Converter> converters)
{
DefaultConversionService conversionService = DefaultConversionService();
for (Converter converter: converters)
{
conversionService.addConverter(converter);
}
return conversionService;
}
}
和部分转换器实现的可能如下:
@Component
public class ConverterImpl implements Converter
{
@Autowired
private DependentClass myDependency;
//The rest of the implementation
}
虽然conversionService是有每一个转换器实现通过配置加入到它类中,没有任何转换器实现中的自动装配字段正在填充。它们是空的。
我当前的解决方案如下:
@Component
public class ConverterImpl implements Converter
{
@Lazy
@Autowired
private DependentClass myDependency;
//The rest of the implementation
}
总之,在该转换器的实现的所有自动装配字段也标注为“懒惰”。这似乎是在第一次访问字段时填充的。这感觉像是一个黑客。我的问题是:有没有更好的方法来实现我想要的?我在Spring文档中丢失了什么?总体方法是否有缺陷?