在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. Day13_68_中止线程

    中止线程方法一 * 在线程类中定义一个bollean类型的变量,默认值设置为ture,如果想要中断线程,只需要将该boolean类型的变量设置为false就可以了 * 代码 package com.s ...

  2. Java学习IO流第一天

    今日内容介绍 字节流 字符流 1 字节流 在前面的学习过程中,我们一直都是在操作文件或者文件夹,并没有给文件中写任何数据.现在我们就要开始给文件中写数据,或者读取文件中的数据. 1.1 字节输出流Ou ...

  3. 1420. Build Array Where You Can Find The Maximum Exactly K Comparisons

    Given three integers n, m and k. Consider the following algorithm to find the maximum element of an ...

  4. D - The Frog's Games (二分)

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

  5. 10276 - Hanoi Tower Troubles Again!(思维,模拟)

    People stopped moving discs from peg to peg after they know the number of steps needed to complete t ...

  6. 为什么传统软件厂商都想转型做Saas?

    欢迎关注微信公众号:sap_gui (ERP咨询顾问之家) 早些年,我工作笔记用的最多的是微软的OneNote,这东西好用不说,不仅能够存在云端,也能存放在本地.可惜到了Office2019之后,On ...

  7. 动态地绑定到它的 is 特性,可以实现动态组件

    前面的话 让多个组件使用同一个挂载点,并动态切换,这就是动态组件.本文将详细介绍Vue动态组件 概述 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以实现动 ...

  8. hdu2167 方格取数 状态压缩dp

    题意:      方格取数,八个方向的限制. 思路:      八个方向的不能用最大流了,四个的可以,八个的不能抽象成二分图,所以目测只能用dp来跑,dp[i][j]表示的是第i行j状态的最优,具体看 ...

  9. 常见设备/CMS弱口令

    目录 tomcat Apache axis2 Apache ActiveMQ zabbix RabbitMQ zentao

  10. openstack虚拟机从数据库修改卷虚拟机backend操作

    由于意外故障,volume-type其中一个backend后段出现性能问题,客户云主机出现卡顿. 因此临时从ceph将系统卷导出,并导入至同一个backend的另一个后端,并启动虚拟机. Nova C ...