Spring MVC 代码配置启动原理

一.简介

Servlet3.0+ 提供了代码实现无web.xml配置,spring MVC 实现WebApplicationInitializer 接口即等同于web.xml配置。

二.流程

1.新建maven 项目,项目结构如下
Spring MVC 代码配置启动原理_第1张图片

2.pom.xml 文件相关依赖配置如下



    4.0.0

    com.vincent
    1-1
    1.0-SNAPSHOT
    war

    
        1.8
        UTF-8
        UTF-8
        2.2
        1.2
        3.1.0
        4.1.3.RELEASE
    

    
        
            javax
            javaee-web-api
            6.0
            provided
        
        
            org.springframework
            spring-webmvc
            ${spring-framework.version}
        
        
            javax.servlet
            jstl
            ${jstl.version}
        
        
            javax.servlet
            javax.servlet-api
            ${servlet.version}
            provided
        
        
            javax.servlet.jsp
            jsp-api
            ${jsp.version}
            provided
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    ${java.version}
                    ${java.version}
                
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.3
                
                    false
                
            
        
    


3.MyMvcConfig.java

package com.vincent.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan("com.vincent")
public class MyMvcConfig {

    @Bean
    InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/classes/views/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

配置内部资源访问路径映射关系,由于项目打包后会放到tomcat下,所有资源路径中有 /WEB-INF/ 目录。

4.WebInitializer.java

import com.vincent.config.MyMvcConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class WebInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println(servletContext);

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MyMvcConfig.class);
        ctx.setServletContext(servletContext);

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
        servlet.addMapping("/");
        servlet.setLoadOnStartup(1);
    }
}

配置Spring MVC 的 DispatcherServlet

5.HelloController.java

package com.vincent.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
public class HelloController {
    @RequestMapping("/index")
    public String hello(){
        return "index";
    }
}

6.index.jsp

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>




    
    Insert title here


    
        Welcome to Spring MVC world
    

三.测试

1.使用 mvn package 命令打包项目

Spring MVC 代码配置启动原理_第2张图片

2.把 1-1-1.0-SNAPSHOT.war 改名为test.war(为了方便输入ServletContext 的根路径

3.把test.war 复制到tomcat 安装目录的webapps 目录下并启动tomcat
Spring MVC 代码配置启动原理_第3张图片

war 包放到tomcat 发布目录后启动tomcat 将会解压该war 包在目录中。

4.浏览器访问 http://localhost:8080/test/index

Spring MVC 代码配置启动原理_第4张图片

四.总结

1.Spring MVC 代码配置的启动原理基于 Servlet3.0+ 提供的javax.servlet.ServletContainerInitializer相关接口实现

2.org.springframework.web.WebApplicationInitializer 部分说明如下Spring MVC 代码配置启动原理_第5张图片

3.至此就可以使用Spring 提供的相关注解简化开发

你可能感兴趣的:(spring,boot)