2

I got spring MVC application. It runs on Tomcat 7. By now i got this part in my web.xml file:

<servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/app-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

Is there any way to init it by annotations? I got a MainSettings.java class where all my beans are initialyzed by @Bean annotation. So how do i initDispatherServlet there?

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
user2160696
  • 699
  • 3
  • 8
  • 21
  • Take a look at this - http://stackoverflow.com/questions/8075790/how-to-register-spring-configuration-annotated-class-instead-of-applicationcont – gkamal May 23 '13 at 06:49

2 Answers2

5

Here is the example with comments. Hope this helps you.

public class ApplicationInitializer implements WebApplicationInitializer {

    //Called first when the application starts loading.
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        System.out.println("Inside application initializer...");

        //Registering the class that incorporates the annotated DispatcherServlet configuration of spring
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(DispatcherConfig.class);

        //Adding the listener for the rootContext
        servletContext.addListener(new ContextLoaderListener(rootContext));

        //Registering the dispatcher servlet mappings.
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}
Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44
  • 3
    You don't want to pass your `rootContext` to the `DispatcherServlet`. Pass it an empty context that has the `rootContext` as the parent. Else the context will be initialized twice. I would also suggest extending the `AbstractAnnotationConfigDispatcherServletInitializer` instead of implementing the `WebApplicationInitializer`. – M. Deinum Oct 09 '14 at 12:43
  • 3
    I can't find DispatcherConfig.class. Where can I get this from? – Shervin Asgari Nov 12 '15 at 12:53
  • @ShervinAsgari Check your Sprint-mvc dependencies. Make sure you have correct version. Above example is based on Spring 3.2 – Japan Trivedi Nov 12 '15 at 16:46
  • @ShervinAsgari It's user-defined. https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html – Cameron Hudson Feb 15 '19 at 03:27
4

Writing this because Japs's answer leads to creation of another context, which doesn't see the contents of the security context.

public class WebInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

    private static final Log LOGGER = LogFactory.getLog(WebInitializer.class);

    @Override
    protected Class<?>[] getRootConfigClasses() {
        /*  this is where you will return you config class
         *  your root config class should @Import other configs 
         *  to make them work without needing them to add there
         */
        return new Class[] { ViewConfig.class };
    }

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

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

}
Community
  • 1
  • 1
bunyaCloven
  • 313
  • 1
  • 4
  • 14