1

I have a Spring Boot Web MVC application, that sits at the root of the application. I am trying to add a third party servlet (CKFinder Java connector) to this application.

I have achieved this with the following:

    @Bean
    public ServletRegistrationBean<CKFinderServlet> registerCkFinder() {
        ServletRegistrationBean<CKFinderServlet> bean =
                new ServletRegistrationBean<>(new CKFinderServlet(), "/ckfinder/*");
        bean.setLoadOnStartup(-1);
        bean.setInitParameters(
                Map.of("scan-path", "<package name>"));
        bean.setMultipartConfig(new MultipartConfigElement("/tmp/upload"));
        return bean;
    }

The main website continues to work correctly, but when I try and hit the CKFinder servlet, I get a series of errors that show initialisation issues while errors while trying to evaluate @Value(...) parameters while auto-wiring my interceptors and controllers (I don't fully understand why it is constructing the controllers).

Having done some investigation, I get the feeling its to do with application contexts!? I do want to share some context information (such as the security information, logged in user etc), but I otherwise don't really need any of the other Spring MVC goodness while executing the servlet.

My question is threefold:

  1. Am I right in thinking its to do with contexts?
  2. How do I register the servlet such that it when called the @Value() and other Spring stuff evaluates correctly? OR
  3. How do I register the servlet with a new context such that it doesn't inherit, and try to initialise, all the Spring MVC components?

I am also considering putting a controller wrapper around the servlet, but that seems overkill for something that should "just work".

  • A wild guess, could you comment out `registerCKFinder` and use the annotation `@ComponentScan` as shown in 3.1 [here](https://www.baeldung.com/spring-component-scanning)? – D-FENS Dec 04 '21 at 13:27
  • I mean something like this in your app: `@ComponentScan(basePackages = "com.cksource.ckfinder") @Configuration public class MyApp` – D-FENS Dec 04 '21 at 13:30
  • Seems not ... sadly (and I'm not an expert), it looks like `CKFinderServlet` class is not appropriately annotated. Making that change did cause it to pick up the config class and other components, but not the servlet itself. – Chris Clayson Dec 04 '21 at 16:59
  • 1
    Think I've solved the problem, the package I was specified in this line: `bean.setInitParameters(Map.of("scan-path", ""));` Was too high-level. It seems the servlet causes does an explicit scan for components in the scan path, so it was picking up all my Spring components. – Chris Clayson Dec 04 '21 at 17:48

0 Answers0