springboot学习(2)无web.xml启动springmvc实现

传统的web应用都需要配置web.xml,web容器读取web.xml获取服务配置和servlet、filter、listener等配置,实例化添加进容器提供服务。学习《springboot编程思想》自动装配章节中,一个简单注解配置就可以实现无web.xml启动web应用,而且也不是springboot启动方式。

一、实现方式:

  1. 继承AbstractAnnotationConfigDispatcherServletInitializer,替代web.xml
public class SpringWebMVCServlatInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class[] getRootConfigClasses() {
        return new Class[0];
    }

    // 读取配置类,从而扫描controller
    protected Class[] getServletConfigClasses() {
        return of(SpringWebMVCConfiguration.class);
    }

    // 配置DispatcherServlet路径
    protected String[] getServletMappings() {
        return of("/*");
    }

    private static  T[] of(T... values) {
        return values;
    }
}
  1. SpringWebMVCConfiguration.class
// @EnableWebMvc打开springmvc配置,@ComponentScan扫描controller包,加载controller
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "controller")
public class SpringWebMVCConfiguration {
}

2.测试controller

@Controller
public class HelloController {

    @PostConstruct
    public void init() {
        System.out.println("init HelloController......");
    }

    @RequestMapping
    @ResponseBody
    public String hello() {
        return "hello world";
    }
}

4.配置文件,配置jar方式启动
需要注意:添加的tomcat maven插件是将应用和tomcat打包到一个jar,配置main class启动tomcat进而加载应用,可以解压打包的jar查看详情。


        5.2.3.RELEASE
        LATEST
    

    
        
            org.springframework
            spring-web
            ${spring_version}
        
        
            org.springframework
            spring-webmvc
            ${spring_version}
        
        
            javax.servlet
            javax.servlet-api
            ${servlet-version}
            provided
        
        
            org.apache.tomcat.maven
            tomcat7-maven-plugin
            2.1
            runtime
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
                org.apache.maven.plugins
                
                    maven-war-plugin
                
                
                    false
                
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.1
                
                    
                        tomcat-run
                        
                            
                            exec-war-only
                        
                        package
                        
                            
                            /
                        
                    
                
            
        
    

参考作者示例:https://github.com/mercyblitz/thinking-in-spring-boot-samples/tree/master/spring-framework-samples

5.启动并测试:

java -jar ./target/springboot_auto-1.0-SNAPSHOT-war-exec.jar

访问: http://localhost:8080

二、原理分析:

  1. AbstractAnnotationConfigDispatcherServletInitializer类
    AbstractAnnotationConfigDispatcherServletInitializer继承关系图如下:


    springboot学习(2)无web.xml启动springmvc实现_第1张图片
    AbstractAnnotationConfigDispatcherServletInitializer.png

    实现了接口WebApplicationInitializer,springmvc应用启动时会调用其onStartup方法,该方法创建WebApplicationContext上下文类,并根据配置创建servletcontext。

2.那么onStartup什么时候调用的呢?
WebApplicationInitializer.onStartup 是通过SpringServletContainerInitializer来调用的,SpringServletContainerInitializer源码如下:

@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    public SpringServletContainerInitializer() {
    }

    public void onStartup(@Nullable Set> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
        List initializers = new LinkedList();
        Iterator var4;
        if (webAppInitializerClasses != null) {
            var4 = webAppInitializerClasses.iterator();

            while(var4.hasNext()) {
                Class waiClass = (Class)var4.next();
                if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
                    try {
                        initializers.add((WebApplicationInitializer)ReflectionUtils.accessibleConstructor(waiClass, new Class[0]).newInstance());
                    } catch (Throwable var7) {
                        throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
                    }
                }
            }
        }

        if (initializers.isEmpty()) {
            servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
        } else {
            servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
            AnnotationAwareOrderComparator.sort(initializers);
            var4 = initializers.iterator();

            while(var4.hasNext()) {
                WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
                initializer.onStartup(servletContext);
            }

        }
    }
}

SpringServletContainerInitializer 的onStartup 方法会处理@HandlesTypes注解的类,通过反射实例化继承该类的子类,并调用其onStartup 方法。

  1. SpringServletContainerInitializer又是哪里调用的呢?
    SpringServletContainerInitializer 实现了 ServletContainerInitializer 接口,实现了ServletContainerInitializer接口的类在META-INF/services路径下添加配置便可以被容器加载,如spring-web包下:


    springboot学习(2)无web.xml启动springmvc实现_第2张图片
    image.png

    内容如下:

org.springframework.web.SpringServletContainerInitializer

这个机制称作SPI,是Servlet 3.0引进的,由容器读取META-INF/services实现。
可以看出该配置是容器提供的代码配置支持,并且可以与web.xml配置方式可以同时生效。

你可能感兴趣的:(springboot学习(2)无web.xml启动springmvc实现)