3

I recently wanted to take a big step and move everything to Java based configuration. Worked perfect until now. There is just one issue.

There is this WebApplicationInitializer Interface from Spring, which was actually the first web.xml alternative AFAIK:

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");

        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}

Better, there is this wonderfull AbstractAnnotationConfigDispatcherServletInitializer Class, which even more perfect to use if your configuration files (Dispatcher etc.) are all Java based:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { MyWebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

And also an XML version of that, if your dispatcher is still stuck in xml:

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

    @Override
    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }

    @Override
    protected WebApplicationContext createServletApplicationContext() {
        XmlWebApplicationContext cxt = new XmlWebApplicationContext();
        cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
        return cxt;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

Now my question is: I have mainly java based config files, so i am using the 2. case with AbstractAnnotationConfigDispatcherServletInitializer. But i have one XML file (for Spring Security), which i would really like to register, without changing my abstract class. For this case is the interface WebApplicationInitializer (1. case) is the only current solution for this (or can be a solution)? Or is there another way to achieve this without using the 1. case or without creating 2 WebApplicationInitializer Classes (1 with AbstractAnnotationConfigDispatcherServletInitializer and 1 with AbstractDispatcherServletInitializer)?

1
  • I just saw this page SpringBlog where the Spring Security is configured Java based. For 3 Months.. So technically i do not have this problem anymore. But the question can be interesting for other situations where the Java based conf. is not supported yet. Commented Oct 31, 2013 at 23:21

1 Answer 1

3

In example 2, you could have MyWebConfig (the class itself, you know, where you have @Configuration at...) annotated with @ImportResource.

@ImportResource is a way to specify some xml configuration to be imported from your Java config. This is one of the primary ways to mix Java and xml configuration.

From the link:

Like @Import, this annotation provides functionality similar to the element in Spring XML. It is typically used when designing @Configuration classes to be bootstrapped by AnnotationConfigApplicationContext, but where some XML functionality such as namespaces is still necessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.