0
votes

I know that i can write to two dispatchers servlets with one root context in this way:

@Override public void onStartup(ServletContext servletContext) throws ServletException {

// root context
AnnotationConfigWebApplicationContext rootContext =
        new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class); // configuration class for root context
rootContext.scan("...service", "...dao"); // scan only some packages
servletContext.addListener(new ContextLoaderListener(rootContext));

// dispatcher servlet 1
AnnotationConfigWebApplicationContext webContext1 = 
        new AnnotationConfigWebApplicationContext();
webContext1.setParent(rootContext);
webContext1.register(WebConfig1.class); // configuration class for servlet 1
webContext1.scan("...web1");            // scan some other packages
ServletRegistration.Dynamic dispatcher1 =
servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1));
dispatcher1.setLoadOnStartup(1);
dispatcher1.addMapping("/subcontext1");

// dispatcher servlet 2
...

}

But how i can do this with AbstractAnnotationConfigDispatcherServletInitializer ? If it is impossible - why we can two methods "getRootConfigClasses" and "getServletConfigClasses" ?

1
It will be great if you could describe your intentions. Usually, you would have only one instance of DispatcherServlet which is managed by Spring itself. I wonder why are trying to create 2nd instance and manage everything yourself.rxn1d
@rxn1d I don't have really task with 2 DispatcherServlet's. But if i understand purpose of method getRootConfigClasses() - to create common context for all dispatcher servlets - and they can get beans from One context. "getServletConfigClasses" - this method - create context for single DispatcherServlet and every DispatcherServlet will have his own context. But when i am trying to create one Root context for all dispatchers - i will have exception, because spring try to create rootContext which was created by first AbstractAnnotationConfigDispatcherServletInitializer class.Apathy21
My aim to understand - why we have this method "getRootConfigClasses" if we can't create one root context for all dispatchers ?Apathy21

1 Answers

0
votes

I think i had found decision - if we use two clases with AbstractAnnotationConfigDispatcherServletInitializer - it is meanining if we define in one of them Root classes in method getRootConfigClasses and another will return empty array "{}" of clases - first root classes will be root classes for both DispatcherServlet's