初始化容器

来源:互联网 发布:win7安装apache 编辑:程序博客网 时间:2024/06/07 01:25
WebApplicationInitializer是Spring提供用来配置Servlet 3.0+配置的接口,从而实现了替代web.xml的作用。
实现此接口将会自动被SpringServletContainerInitializer(用来启动Servlet3.0容器)获取到。

package com.lebron.springmvc;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.servlet.DispatcherServlet;public class WebInitializer implements WebApplicationInitializer{@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();ctx.register(MyMvcConfig.class);   //加载springMVC配置ctx.setServletContext(servletContext);//注册springMVC的DispatcherServletDynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/");servlet.setLoadOnStartup(1);//servlet.setAsyncSupported(true);  //开启异步方法支持}}


原创粉丝点击