RWS / dxa-web-application-java

SDL Digital Experience Accelerator Java Spring MVC web application
25 stars 37 forks source link

Not possible to use Spring to override the default RichTextDataConverter #94

Open willprice76 opened 6 years ago

willprice76 commented 6 years ago

In order to work around issue #93 we attempted to define our own CustomRichTextDataConverter with the @Primary and @Component annotations. However, due to the way in which the DXA 2.0 loads the data converters into the GenericSemanticModelDataConverter (using @Autowired on the setConverters method) our custom converter is always overridden by the default.

The setConverters method should reverse sort the set before applying the converters, ensuring that lower priority converters are set first and then overridden by any higher priority converters later.

willprice76 commented 6 years ago

Dirty hack while waiting for this fix is to add an onApplicationEvent for context refresh in your custom RichTextDataConvertor:

@Component
@Primary
public class MyRichTextDataConverter extends RichTextDataConverter implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent event) {
        GenericSemanticModelDataConverter dataConverter = getContext().getBean(GenericSemanticModelDataConverter.class);
        Set<SemanticModelConverter<?>> converters = new HashSet<>();
        converters.add(this);
        dataConverter.setConverters(converters);
    }
}