[英]Spring Converters reuse
I'm trying to use Spring Custom Converters to convert from my Persistence Objects to Json Objects (for UI layer). 我正在尝试使用Spring自定义转换器将我的持久性对象转换为Json对象(用于UI层)。
DBO's DBO的
class Adbo{
Bdbo b = new Bdbo();
}
class Bdbo{}
UI POJOS UI POJOS
class APoj{
BPoj b = new Poj();
}
class BPoj{}
These are my Converters 这些是我的转换器
class APojToADbo implements Converter<ADbo, APoj>{
@Override
public APoj convert(ADbo source) {
//..code to convert
//..also includes conversion from BDbo to BPoj
}
}
class ADboToAPoj implements Converter<APoj, ADbo>{
@Override
public ADbo convert(APoj source) {
//..code to convert
//..also includes conversion from BPoj to BDbo
}
}
class BPojToBDbo implements Converter<BDbo, BPoj>{
@Override
public BPoj convert(BDbo source) {
//..code to convert
}
}
class BDboToBPoj implements Converter<BPoj, BDbo>{
@Override
public BDbo convert(BPoj source) {
//..code to convert
}
}
I register my converters into the ConversionService with this 我通过此将转换器注册到ConversionService
@Bean
public ConversionServiceFactoryBean getConversionService(){
ConversionServiceFactoryBean c = new ConversionServiceFactoryBean();
Set<Converter> converters = new HashSet<>();
converters.add(aPojToADbo);
converters.add(aDboToAPoj);
converters.add(bPojToBDbo);
converters.add(bDboToBPoj);
c.setConverters(converters);
return c;
}
Now I'm trying to refactor so that I can avoid writing this 现在,我正在尝试重构,以便避免编写此代码
//..also includes conversion from BDbo to BPoj
//..also includes conversion from BPoj to BDbo
in 2 places , So I did this in APojToADbo 在2个地方 ,所以我在APojToADbo中做到了
@Autowired
protected ConversionService conversionServ;
and If I do the above, I'm getting the following exception 如果执行上述操作,则出现以下异常
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Because IoC is not completed yet on the ConversionService bean. 因为IoC尚未在ConversionService bean上完成。 Circular Dependency!
循环依赖!
There was this nice article on how to avoid Circular Dependency, but I want to know if Spring provides any cleaner/elegant way to do this inside converters 有此就如何避免循环依赖好文章,但我想知道,如果Spring提供任何清洁剂/优雅的方式来做到这里面转换器
I would suggest you to return the ConversionService and set the afterPropertiesSet 我建议您返回ConversionService并设置afterPropertiesSet
@Bean
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters( getConverters() );
bean.afterPropertiesSet();
ConversionService object = bean.getObject();
return object;
}
I ended up doing this to my converters: 我最终对转换器进行了此操作:
class BaseConverter {
@Autowired
ApplicationContext alpCtx;
public ConversionService getConversaionService(){
alpCtx.getBean("conversionService");
}
}
class APojToADbo extends BaseConverter implements Converter<>{
convert(){
getConversaionService() //return lookup from alpCtx and return from Baseclass
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.