作下记录
从spring官方文档中能够看到以下配置:web
public class MyWebApplicationInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletCxt) { // Load Spring web application configuration AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext(); //这里的AppConfig.class其实是经过人为添加的具体配置类(好比视图解析器、静态资源处理均可以放在这里使用),针对这个类我进行了相似的实现 ac.register(AppConfig.class); //这里是对spring的上下文进行刷新 ac.refresh(); // Create and register the DispatcherServlet DispatcherServlet servlet = new DispatcherServlet(ac); //这里在servlet上下文中添加了一个名为 app 的拦截器(servlet),由于拦截器的实质就是一个servlet ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet); registration.setLoadOnStartup(1); //全部带有/app/ 前缀的请求都交由该拦截器处理 registration.addMapping("/app/*"); } }
<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>
/** * @author: Andy * @date: 4/2/2018 3:07 PM * @description: 经过实现接口配置的初始化类 * @version: 1.0 */ public class WebApplicationInitializerConfigA implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebConfig.class); ctx.setServletContext(servletContext); ctx.refresh(); ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); registration.addMapping("/"); registration.setLoadOnStartup(1); } } /** * @author: Andy * @date: 3/30/2018 5:17 PM * @description: 开启视图解析器配置 * @version: 1.0 */ @Configuration @EnableWebMvc @ComponentScan(basePackages = {"com.*"}) public class WebConfig{ /** * Method Description: * 〈Jsp 视图解析器〉 * * @param: null * @return: jsp view resolver * @author: Andy * @date: 4/2/2018 9:55 AM */ @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); //请求前缀,当请求发起时,拦截器会到这里去找相应的页面 viewResolver.setPrefix("/WEB-INF/views/"); //请求后缀,控制器返回的内容拼接.jsp寻找页面 viewResolver.setSuffix(".jsp"); viewResolver.setExposeContextBeansAsAttributes(true); return viewResolver; } }
经过这样简单的配置就已经简单的配置了Springmvc,另外,若是你支持servlet3.0,还能够经过继承一个抽象类来完成配置,以下:
/** * @author: Andy * @date: 3/30/2018 5:11 PM * @description: Web应用初始化配置 * @version: 1.0 */ public class WebApplicationInitializerConfig extends AbstractAnnotationConfigDispatcherServletInitializer { //拦截器将要拦截的请求格式 / 为全部请求 @Override protected String[] getServletMappings() { return new String[]{"/"}; } //根配置 @Override protected Class<?>[] getRootConfigClasses() { return new Class[0]; } //具体配置件类(和上述的WebConfig是同样的) @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } }
这个类和WebApplicationInitializerConfigA具备一样的做用。spring