简体   繁体   中英

DispatcherServlet Config in Annotation Based web.xml

Please refer this Source before reading question(WebAppInitializer.class). Is it mandatory to separately configure root config classes and DispatcherConfig class.Why there is two objects created for AnnotationConfigWebApplicationContext. Can't we use single object to configure all the classes. My Spring Version is 4.3.12 Release and I am referring to Spring MVC (not Spring Boot).

The 2 different contexts are created for a reason and they serves different purpose.

  • Root Application Context contains business services, repositories.
  • DispatcherServlet Application Context contains controllers,Views and resolvers.

So, 2 instances of AnnotationConfigWebApplicationContext are created to register 2 contexts.

It must also be seen, how 2 contexts are registered with the container.

//root Context
AnnotationConfigWebApplicationContext rootContext = ...
container.addListener(new ContextLoaderListener(rootContext));

// Register and map the dispatcher servlet
AnnotationConfigWebApplicationContext dispatcherServlet = ...
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM