Spring Java Config with Multiple Dispatchers

后端 未结 3 1373
挽巷
挽巷 2020-12-14 04:50

I\'ve some experience Spring now and also have some pure java config web-apps in use. However, these are usually based on a quiet simple setup:

  • application con
3条回答
  •  既然无缘
    2020-12-14 05:15

    I think you can work it out if you use generic WebApplicationInitializer interface rather than using abstract implementation provided by spring - AbstractAnnotationConfigDispatcherServletInitializer.

    That way, you could create two separate initializers, so you would get different ServletContext on startUp() method and register different AppConfig & dispatcher servlets for each of them.

    One of such implementing class may look like this:

    public class FirstAppInitializer implements WebApplicationInitializer {
    
        public void onStartup(ServletContext container) throws ServletException {
    
            AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
            ctx.register(AppConfig.class);
            ctx.setServletContext(container);
    
            ServletRegistration.Dynamic servlet = container.addServlet(
                    "dispatcher", new DispatcherServlet(ctx));
    
            servlet.setLoadOnStartup(1);
            servlet.addMapping("/control");
    
        }
    
    }
    

提交回复
热议问题