SpringMVC 的两种Initiallizer方法
package com.ellin.lc.config; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration.Dynamic; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; public class LoveCalculatorApplicationInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webApplcationContext = new AnnotationConfigWebApplicationContext(); webApplcationContext.register(LoveCalculatorAppConfig.class); //create a dispatcher servlet object DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplcationContext); //register the dispatcher servlet with the servlet context Dynamic myCustomDispatcherServlet = servletContext.addServlet("myDispatcherServlet", dispatcherServlet); myCustomDispatcherServlet.setLoadOnStartup(1); myCustomDispatcherServlet.addMapping("/mywebsite.com/*"); } }
package com.ellin.lc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class LCAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { // TODO Auto-generated method stub return null; } @Override protected Class<?>[] getServletConfigClasses() { Class arr[] = {LoveCalculatorAppConfig.class}; return arr; } @Override protected String[] getServletMappings() { String arr[] = {"/mywebsite.com/*"}; return arr; } }
两种其实都是实现了WebApplicationInitializer接口,一个是自己手动实现,一个是Spring帮我们实现的。
DispatcherServlet负责接收请求并分发到各个controller中,最后收集响应并回复给客户端。


浙公网安备 33010602011771号