SpringMVC运行方式

web三层架构 

mvc模式 

异步调用

 

 SpringMVC运行方式

异步调用

我们想要得到相应的界面,必须要将我们想要加载的东西放入Web容器中。

如何得到Web容器呢?

初始化Web容器。

如何初始化Web容器呢?

我们要启动服务器(我们常用的服务器有Tomcat),执行不同的方法(文章开头举了三种方式,我们用异步调用初始化),得到不同的初始化Web容器。而如果我们要通过SpringMVC创建容器,我们要执行ServletContainerInitConfig类。

项目架构  

 ServletContainersInitConfig.java

package com.it.config;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;

public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(SpringMVCConfig.class);
        return ctx;
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};//所有请求都归mvc处理
    }

    protected WebApplicationContext createRootApplicationContext() {
        return null;
    }
}

代码中创建了WebApplicationContext对象放入到Web容器中 

 

 代码中创建了WebApplicationContext对象放入到Web容器中,但是具体放在Web容器中什么位置呢?

由图可知, 创建的WebApplicationContext对象放在ServletContext

回到ServletContainersInitConfig.java

打开配置

 SpringMVCConfig.java

package com.it.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.it.controller")//扫描这个包 这个包下都是用来处理请求的,加载controller控制器bean
public class SpringMVCConfig {
}

@ComponentScan扫描com.it.controller这个包,我们根据项目结构可知com.it.controller这个包下有UserController.java。这个配置文件的作用是扫描UserController加载对应的bean。当你使用了@ComponentScan注解,并指定了要扫描的包或类路径时,Spring会在这些路径下查找所有被@Component注解(或其子注解,如@Controller@Service@Repository@Configuration等)标记的类,并将这些类自动装配到Spring的bean容器中。

 UserController.java

package com.it.controller;

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

@Controller
public class UserController {
    @RequestMapping("/save")//用来处理请求
    @ResponseBody
    public String save(){

        System.out.println("user save.....");
        return "{'info:'springmvc'}";
    }
}

 找到@Controller加载对应的Bean到容器中

 在代码中@RequestMapping("/save")关联了对应save()方法.

 

回到我们的ServletContainersInitConfig.java配置文件 

拦截所有进入Tomcat的容器的请求,全部由SpringMVC来处理

到目前为止,成功启动服务器的初始化过程

总结:

  1. 服务器启动,执行ServletContainersInitConfig类,初始化Web容器
  2. 执行createServletApplicationContext方法,创建了WebApplicationContext对象。
  3. 加载SpringMVCConfig
  4. 执行@ComponentScan加载对应的Bean
  5. 加载UserController,每一个@RequestMapping对应一个具体方法
  6. 执行getServletMappings方法,定义所有的请求都通过SpringMVC

单次请求过程:

  1. 发送请求localhost/save
  2. web容器发现所有请求都经过SpringMVC,将请求交给SpringMVC处理
  3. 解析请求路径/save
  4. 由/save执行匹配对应的方法save()
  5. 执行save()
  6. 检测到有@ResponseBody直接将save()方法的返回值作为相应体返回请求方 

运行结果: 

 

  • 53
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值