10

We want to be able to override the default richtext processing and link resolving in DXA Java 1.7.

I am a bit lost with how the Spring configuration works, but it looks like there are Spring @Configuration class(es) in the DXA framework which do component scans to pick up default TridionLinkResolver and DefaultRichTextProcessor.

Given these Configuration classes are embedded in the framework and I don't want to change the code and build my own DXA version, how can I change the configuration to pick up my own LinkResolver and RichTextProcessor components?

Further to this, I actually only want to make minor tweaks to the behaviour of the default TridionLinkResolver and DefaultRichTextProcessor. However it seems that these classes contain mostly private methods, which cannot be overridden. Is there some way I can elegantly extend them without having to duplicate the source code (and thus making maintenance and upgrades troublesome)?

8
+50

Some time ago, we also worked on a DXA implementation where we customized the link resolver (I think we were using DXA 1.5 or 1.6, but the approach should be similar).

The Spring framework should normally scan any of your packages for implementations, annotated by @Component. By adding @Primary, you're able to make your implementation take precedence over the existing ones.

Depending on your requirements, you can now either implement your whole custom resolving logic, or first call the parent functionsuper.resolveLink(url, localizationId, resolveToBinary); function from the parent class, and perform any processing logic on the result of that function.

package com.my.custom.webapp.tridion.linking;

//imports go here here


@Component
@Primary
@Slf4j
public class CustomLinkResolver extends TridionLinkResolver {

    @Autowired
    private WebRequestContext webRequestContext;

    /**
     * {@inheritDoc}
     */
    @Override
    public String resolveLink(String url, String localizationId, boolean resolveToBinary) {

        //custom implementation here

     }
}
3

To answer your second question, once a class has private methods, they can't really be reused or overriden. You have two options: a) code duplication, or b) dynamic method execution using reflection. I would probably go for option b) but it has a few issues of its own, e.g. https://stackoverflow.com/questions/880365/any-way-to-invoke-a-private-method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.