在springmvc上我们会编写很多相关的配置

  • 编写springmvc.xml

    • 配置映射器
    • 配置 处理适配器

      ...
  • web.xml
    • 配置前端控制器 (DispatcherServlet)

官网: https://spring.io/

弱小和无知并不是生存的障碍,傲慢才是。

=================================================================

=================================================================

  • 注解方式

public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
public void onStartup(ServletContext servletCxt) { // Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh(); // Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}

=================================================================

  • 配置方式

<web-app>

    <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-context.xml</param-value>
</context-param> <servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping> </web-app>

=================================================================

* 创建maven项目,然后导入web支持

=================================================================

  • 导入springmvc相关依赖
<dependencies>
<!--springmvc框架-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--servlet3.0-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- 嵌入式tomcat -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.20</version>
</dependency>
<!--解决JSP异常-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.20</version>
</dependency>
</dependencies>

=================================================================

=================================================================

* 创建一个跟官网上一样的类

package com.min.mvc;

import com.min.config.AppConfig;
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.ServletRegistration; public class MyWebApplicationInitializer implements WebApplicationInitializer { public void onStartup(ServletContext servletCxt) { // 通过 注解的方式 创建 IOC容器
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
// 注册一个配置类
ac.register(AppConfig.class);
// 刷新 IOC 容器配置
ac.refresh(); // 创建和注册servlet
DispatcherServlet servlet = new DispatcherServlet(ac);
// 注册 servlet
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
// 启动加载
registration.setLoadOnStartup(1);
// DistpatcherServlet 映射关系
registration.addMapping("*.do");
}
}

* 创建配置类

package com.min.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* 配置类
* @Configuration : 通过他注册一个配置类
* @ComponentScan : 通过他开启一个扫描
*/
@Configuration
@ComponentScan(value = "com.min.controller")
public class AppConfig {
}

=================================================================

  • 编写一个controller

package com.min.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class HelloController { @RequestMapping("/hello.do")
@ResponseBody
public String hello(){
return "Hello World!";
}
}

=================================================================

* 访问hello.do



经过一番波折发现通过官网上的代码也能跑通。。我丢

=================================================================

更有趣的是这哥们跟我们之前写的Servlet3.0的似曾相识哈哈,那我们一探究竟吧。

=================================================================

  • 会发现这个类也是实现ServletContainerInitializer 接口,@HandlesTypes注解以及重写onStartup方法


package org.springframework.web; import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils; @HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
public SpringServletContainerInitializer() {
} public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
List<WebApplicationInitializer> 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);
} }
}
}

=================================================================

  • WebApplicationInitializer接口

package org.springframework.web;

import javax.servlet.ServletContext;
import javax.servlet.ServletException; public interface WebApplicationInitializer {
void onStartup(ServletContext var1) throws ServletException;
}

所以经过以上的查看和测试,发现其中的原理就是这样子的!!!

底层源码

  • 迭代
  • 通过反射实例化对象
  • 调用这些实现类的 startup 方法

3. servlet 和 springmvc框架关系的更多相关文章

  1. 简单实现springmvc框架(servlet+自定义注解)

    个人水平比较菜,没有这么高的实力简单实现springmvc框架,我是看了一个老哥的博客,这老哥才是大神! 原文链接:https://www.cnblogs.com/xdp-gacl/p/4101727 ...

  2. 带着新人简单看看servlet到springmvc

    好久都没有写博客了,不是因为自己懒了,而是总感觉自己知道的只是太少了,每次想写博客的时候都不知道怎么下手,不过最近看到一篇博客说的是springmvc,给了我比较大的兴趣,感觉一下子对整个spring ...

  3. SpringMVC 框架介绍以及环境搭建

    目录 前端设计模式介绍 分析前端设计模式 Spring MVC简单介绍 Spring和Spring MVC的关系 配置Spring MVC的环境并简单测试 前端设计模式介绍 前端设计模式其实和前端没啥 ...

  4. 手写SpringMVC 框架

    手写SpringMVC框架 细嗅蔷薇 心有猛虎 背景:Spring 想必大家都听说过,可能现在更多流行的是Spring Boot 和Spring Cloud 框架:但是SpringMVC 作为一款实现 ...

  5. SpringMVC框架 课程笔记

    SpringMVC框架 课程笔记 第0章 SpringMVC框架的核心内容 1.SpringMVC 概述 2.SpringMVC 的 HelloWorld 3.使用 @RequestMapping 映 ...

  6. MyBatis+SpringMVC 框架搭建小结

    前言:最近再写一款视频播放器的后台,踩了很多坑,在此总结. 设计顺序: 前提:搭建配置完好的Spring-MyBatis项目 1.流程分析,数据库设计(看似无用,真正做起来真的需要这个东西帮忙整理下思 ...

  7. SpringMVC框架搭建流程(完整详细版)

    SpringMVC框架搭建流程 开发过程 1)配置DispatcherServlet前端控制器 2)开发处理具体业务逻辑的Handler(@Controller. @RequestMapping) 3 ...

  8. 手写SpringMVC框架(三)-------具体方法的实现

    续接前文 手写SpringMVC框架(二)结构开发设计 本节我们来开始具体方法的代码实现. doLoadConfig()方法的开发 思路:我们需要将contextConfigLocation路径读取过 ...

  9. 手写SpringMVC框架(二)-------结构开发设计

    续接前文, 手写SpringMVC框架(一)项目搭建 本节我们来开始手写SpringMVC框架的第二阶段:结构开发设计. 新建一个空的springmvc.properties, 里面写我们要扫描的包名 ...

随机推荐

  1. Ansible(1)- 简单介绍

    什么是 Ansible 开源部署工具,也是一个自动化运维工具 开发语言:Python Ansible 的特性 模块化部署管理:调用特定的模块,完成特定任务 三个关键模块:Paramiko(python ...

  2. 什么是SQL注入漏洞?

    什么是SQL注入: SQL是操作数据库数据的结构化查询语言,网页的应用数据和后台数据库中的数据进行交互时会采用SQL. SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字 ...

  3. 详解Linux指令与文件的搜寻

    我们在管理Linux服务器时通常会进行搜索文件及目录操作,下面我们就开始了解一下linux目录结构的相关知识. 博主再奉上一套零基础入门Linux视频,带你从入门到精通 https://www.bil ...

  4. 按照自己的思路去研究Spring AOP源码【1】

    目录 一个例子 Spring AOP 原理 从@EnableAspectJAutoProxy注解入手 什么时候会创建代理对象? 方法执行时怎么实现拦截的? 总结 问题 参考 一个例子 // 定义一个切 ...

  5. 聊一聊Jmeter的参数化

    背景 前面一篇讲了 JMeter 的一个最简单的例子,这篇聊一下 JMeter 的参数化. 在开始之前先来一个单元测试的例子,感受一下参数化. 上面是一个用 xUnit 写的单元测试,这个单元测试就是 ...

  6. 一文抽丝剥茧带你掌握复杂Gremlin查询的调试方法

    摘要:Gremlin是图数据库查询使用最普遍的基础查询语言.Gremlin的图灵完备性,使其能够编写非常复杂的查询语句.对于复杂的问题,我们该如何编写一个复杂的查询?以及我们该如何理解已有的复杂查询? ...

  7. 对c语言回调函数的理解

    对于回调函数,可以简单的理解为一种特别的函数调用方法,我们可以对比一下回调函数与普通函数在调用方法上的区别. 1. 普通函数调用 一般为实现方在其函数体执行过程中直接调用. 代码示例: #includ ...

  8. 基于三层交换机的VRRP技术--MSTP、VRRP的综合运用

    MSTP (多生成树) 每个VLAN或者几个VLAN拥有一颗生成树,基于实例的生成树.instance 1.instance 2 每个实例拥有一颗生成树.MSTP可以实现多VLAN 的负载分担,可以实 ...

  9. PowerShell-6.文件操作

    1.显示文本内容 Get-Content "°C:\\Program Files (x86)\\PsUpdate\\b.dat" 2.得到b.dat文件内容,然后把里面的所有'C' ...

  10. 【maven】Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.3:site (default-site)

    问题描述 site一点击就报错,如下 Failed to execute goal org.apache.maven.plugins:maven-site-plugin:3.3:site (defau ...