简体   繁体   English

基于批注的web.xml中的DispatcherServlet Config

[英]DispatcherServlet Config in Annotation Based web.xml

Please refer this Source before reading question(WebAppInitializer.class). 阅读问题(WebAppInitializer.class)之前,请参考此 Is it mandatory to separately configure root config classes and DispatcherConfig class.Why there is two objects created for AnnotationConfigWebApplicationContext. 是否必须分别配置根配置类和DispatcherConfig类。为什么要为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). 我的Spring版本是4.3.12发行版,我指的是Spring MVC(不是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. DispatcherServlet应用程序上下文包含控制器,视图和解析器。

So, 2 instances of AnnotationConfigWebApplicationContext are created to register 2 contexts. 因此,创建了2个AnnotationConfigWebApplicationContext实例来注册2个上下文。

It must also be seen, how 2 contexts are registered with the container. 还必须看到如何在容器中注册2个上下文。

//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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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