赞
踩
当项目启动了Tomcat后,服务器会去当前wabapp的路径下去读取配置文件,然后解析其中配置的DispathServlet,在DispathServlet中会创建一个Spring容器,去扫描我们的Contorller等各个由Spring管理的bean对象,在DispathServlet的源码中,是需要将Spring容器给到DispathServlet当中,也就相当于在DispathServlet中内嵌一个Spring容器,从而实现对目标资源的调用。
XML配置方式:
- <web-app>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/app-context.xml</param-value>
- </context-param>
-
- <servlet> <servlet-name>app</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
-
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value></param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
-
- </servlet>
- <servlet-mapping>
- <servlet-name>app</servlet-name> <url-pattern>/app/*</url-pattern>
- </servlet-mapping>
- </web-app>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
JavaConfig配置方式:
- public class MyWebApplicationInitializer implements WebApplicationInitializer {
-
- @Override
- public void onStartup(ServletContext servletContext) {
-
- // Load Spring web application configuration
- AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
- context.register(AppConfig.class);
-
- // Create and register the DispatcherServlet
- DispatcherServlet servlet = new DispatcherServlet(context);
- ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
- registration.setLoadOnStartup(1);
- registration.addMapping("/app/*");
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。