作下記錄
從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