SpringMVC源码深度解析(一):纯注解版SpringMVC

前言

前后端分离以及Springboot大行其道的时代,XML配置文件出现的频率越来越少了。注解慢慢替代了XML,现在就让我们来看看springboot是怎么样集成springmvc纯注解版的吧。

正文

创建好一个springboot项目后,要想使用springmvc很简单,只需要在pom文件里面添加上相应的依赖即可。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在springboot项目中使用springmvc,我们再也不用去配置web.xml文件了,只需要写普通的类和方法,然后加上注解,就能实现处理url请求的功能。下面这个示例就是大家入门springmvc时写的第一个hello world。

package org.study.jimmy.springmvctest.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "Hello";
    }
}

加下来看看纯注解版的springmvc是如何生效的。
servlet容器(如Tomcat)在启动应用的时候,会扫描当前应用中每一个jar包里面,路径为META-INF/services/javax.servlet.ServletContainerInitializer的文件的内容,文件里面是一个全类名,顾名思义,这个类就是servlet容器的初始化器。
在这里插入图片描述
点进去看看,这个类只有一个onStartup方法,方法执行时,会将@HandlesTypes注解里面的类型的所有子类(子接口、实现类等)都传递到方法的第一个参数中,传进来后就可以对类进行操作,比如用反射创建对象,实际上,代码里就是反射创建了对象。第二个参数servletContext代表当前web应用,一个web应用对应一个servletContext。可以用servletContext注册一些web组件(servlet、filter、listener)。
在这里插入图片描述
我们再来看看@HandlesTypes这个注解里面的那个接口WebApplicationInitializer.class,这个接口下面有一个AbstractAnnotationConfigDispatcherServletInitializer类,这是一个抽象类,我们要做的就是集成这个类,实现其中的抽象方法。
在这里插入图片描述
接下来我们实现这个类,分别实现3个方法,这3个方法分别指定了2个配置文件类和servlet映射路径。

package org.study.jimmy.springmvctest.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
}

1、RootConfig,没啥特别,一般用来扫描除了@Controller的其他组件。

package org.study.jimmy.springmvctest.config;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RootConfig {
}

2、WebConfig,一旦这个配置类加上了@EnableWebMvc这个注解,则对于mvc的自动配置全都不要了,所有的配置都在WebConfig配置类里面配置,具体有哪些配置方法,可以看看继承的WebMvcConfigurerAdapter类里面的方法。

package org.study.jimmy.springmvctest.config;

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.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
@ComponentScan("org.study.jimmy.springmvctest.controller")
public class WebConfig extends WebMvcConfigurerAdapter {
}

我们来看看WebMvcConfigurerAdapter类的方法,这个类里面可以添加跟入参解析和返回值处理相关的方法,如下图圈起来的方法。
在这里插入图片描述
总结

以上就是springboot下的springmvc纯注解开发,下一篇我们来看一下DispatcherServlet是如何工作的。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值