在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractAnnotationDispatcherServletInitializer,将会自动的被用为配置DispatcherServlet,以及在DispatcherServlet中的的spring容器(如:spring-servlet.xml)文件。
spring in action的描述是:any class that extends AbstractAnnotationDispatcherServletInitializer will automatically be used to configure DispatcherServlet and the Spring Application context in the application's servlet context.
AbstractAnnotationDispatcherServlet的代码如下:
1 /* 2 * Copyright 2002-2016 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.web.servlet.support; 18 19 import org.springframework.util.ObjectUtils; 20 import org.springframework.web.context.WebApplicationContext; 21 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 22 23 /** 24 * Base class for {@link org.springframework.web.WebApplicationInitializer} 25 * implementations that register a 26 * {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet} 27 * configured with annotated classes, e.g. Spring's 28 * {@link org.springframework.context.annotation.Configuration @Configuration} classes. 29 * 30 * <p>Concrete implementations are required to implement {@link #getRootConfigClasses()} 31 * and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. 32 * Further template and customization methods are provided by 33 * {@link AbstractDispatcherServletInitializer}. 34 * 35 * <p>This is the preferred approach for applications that use Java-based 36 * Spring configuration. 37 * 38 * @author Arjen Poutsma 39 * @author Chris Beams 40 * @since 3.2 41 */ 42 public abstract class AbstractAnnotationConfigDispatcherServletInitializer 43 extends AbstractDispatcherServletInitializer { 44 45 /** 46 * {@inheritDoc} 47 * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, 48 * providing it the annotated classes returned by {@link #getRootConfigClasses()}. 49 * Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}. 50 */ 51 @Override 52 protected WebApplicationContext createRootApplicationContext() { 53 Class<?>[] configClasses = getRootConfigClasses(); 54 if (!ObjectUtils.isEmpty(configClasses)) { 55 AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); 56 rootAppContext.register(configClasses); 57 return rootAppContext; 58 } 59 else { 60 return null; 61 } 62 } 63 64 /** 65 * {@inheritDoc} 66 * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, 67 * providing it the annotated classes returned by {@link #getServletConfigClasses()}. 68 */ 69 @Override 70 protected WebApplicationContext createServletApplicationContext() { 71 AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext(); 72 Class<?>[] configClasses = getServletConfigClasses(); 73 if (!ObjectUtils.isEmpty(configClasses)) { 74 servletAppContext.register(configClasses); 75 } 76 return servletAppContext; 77 } 78 79 /** 80 * Specify {@link org.springframework.context.annotation.Configuration @Configuration} 81 * and/or {@link org.springframework.stereotype.Component @Component} classes to be 82 * provided to the {@linkplain #createRootApplicationContext() root application context}. 83 * @return the configuration classes for the root application context, or {@code null} 84 * if creation and registration of a root context is not desired 85 */ 86 protected abstract Class<?>[] getRootConfigClasses(); 87 88 /** 89 * Specify {@link org.springframework.context.annotation.Configuration @Configuration} 90 * and/or {@link org.springframework.stereotype.Component @Component} classes to be 91 * provided to the {@linkplain #createServletApplicationContext() dispatcher servlet 92 * application context}. 93 * @return the configuration classes for the dispatcher servlet application context or 94 * {@code null} if all configuration is specified through root config classes. 95 */ 96 protected abstract Class<?>[] getServletConfigClasses(); 97 98 }
案例的目录结构如下图所示:
RootConfig的代码如下:
1 package spittr.config; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.FilterType; 6 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 7 8 /** 9 * Created by ${秦林森} on 2017/6/12. 10 */ 11 @Configuration 12 /** 13 * excludeFilters是指明不被@ComponentScan扫描的类型 14 */ 15 @ComponentScan(basePackages = {"spittr"},excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = EnableWebMvc.class)}) 16 17 public class RootConfig { 18 19 }
WebConfig的代码如下:
1 package spittr.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.ComponentScan; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.web.servlet.ViewResolver; 7 import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; 8 import org.springframework.web.servlet.config.annotation.EnableWebMvc; 9 import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 10 import org.springframework.web.servlet.view.BeanNameViewResolver; 11 import org.springframework.web.servlet.view.InternalResourceViewResolver; 12 import spittr.web.HomeController; 13 14 /** 15 * Created by ${秦林森} on 2017/6/12. 16 */ 17 @Configuration 18 @EnableWebMvc 19 @ComponentScan(basePackages = {"spittr.web"}) 20 public class WebConfig extends WebMvcConfigurerAdapter{ 21 @Bean 22 public ViewResolver viewResolver(){ 23 InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 24 //设置前缀 25 viewResolver.setPrefix("/WEB-INF/views/"); 26 //设置后缀 27 viewResolver.setSuffix(".jsp"); 28 viewResolver.setExposeContextBeansAsAttributes(true); 29 return viewResolver; 30 } 31 @Override 32 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 33 configurer.enable();//configure static content handling. 34 } 35 36 }
1 package spittr.config; 2 3 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 5 /** 6 * Created by ${秦林森} on 2017/6/12. 7 */ 8 public class SpittrWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 9 10 @Override 11 protected Class<?>[] getRootConfigClasses() { 12 return new Class<?>[]{RootConfig.class}; 13 } 14 15 @Override 16 protected Class<?>[] getServletConfigClasses() { 17 //specifiy configuration class 18 return new Class<?>[]{WebConfig.class}; 19 } 20 21 @Override 22 protected String[] getServletMappings() { 23 return new String[]{"*.do"};//Map DispatcherServlet to 24 } 25 }
HomeController的代码如下:
1 package spittr.web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.WebApplicationInitializer; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 /** 9 * Created by ${秦林森} on 2017/6/12. 10 */ 11 @Controller 12 @RequestMapping(value = "/people") 13 public class HomeController { 14 @RequestMapping(value = "/home.do",method = RequestMethod.GET) 15 public String home(){ 16 return "home"; 17 } 18 }
home.jsp的代码如下:
1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator 4 Date: 2017/6/12 5 Time: 16:25 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <html> 10 <head> 11 <title>spittr</title> 12 </head> 13 <body> 14 welcome to china ,one of the ancient civilized countries. 15 </body> 16 </html>
注意我这个项目的web应用名设置为该项目名。
启动tomcat 输入:http://localhost:8090/spring20170602/people/home.do
访问结果如下图所示: