加入收藏 | 设为首页 | 会员中心 | 我要投稿 西安站长网 (https://www.029zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

Web框架的前生今世--从Servlet到Spring mvc到Spring boot

发布时间:2019-08-16 05:06:01 所属栏目:建站 来源:架构师笔记
导读:副标题#e# 背景 上世纪90年代,随着Internet和浏览器的飞速发展,基于浏览器的B/S模式随之火爆发展起来。最初,用户使用浏览器向WEB服务器发送的请求都是请求静态的资源,比如html、css等。 但是可以想象:根据用户请求的不同动态的处理并返回资源是理所当

2.2 编码方式

  1. public class MyWebAppInitializer implements WebApplicationInitializer { 
  2.   
  3.  @Override 
  4.  public void onStartup(ServletContext container) { 
  5.  // Create the 'root' Spring application context 
  6.  AnnotationConfigWebApplicationContext rootContext = 
  7.  new AnnotationConfigWebApplicationContext(); 
  8.  rootContext.register(AppConfig.class); 
  9.   
  10.  // Manage the lifecycle of the root application context 
  11.  container.addListener(new ContextLoaderListener(rootContext)); 
  12.   
  13.  // Create the dispatcher servlet's Spring application context 
  14.  AnnotationConfigWebApplicationContext dispatcherContext = 
  15.  new AnnotationConfigWebApplicationContext(); 
  16.  dispatcherContext.register(DispatcherConfig.class); 
  17.   
  18.  // Register and map the dispatcher servlet 
  19.  ServletRegistration.Dynamic dispatcher = 
  20.  container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
  21.  dispatcher.setLoadOnStartup(1); 
  22.  dispatcher.addMapping("/"); 
  23.  } 
  24.   
  25.  } 

内部实现

web框架的前生今世--从servlet到spring mvc到spring boot

3.spring boot

继承了spring mvc的框架,实现SpringBootServletInitializer

  1. package com.mkyong; 
  2. import org.springframework.boot.SpringApplication; 
  3. import org.springframework.boot.autoconfigure.SpringBootApplication; 
  4. import org.springframework.boot.builder.SpringApplicationBuilder; 
  5. import org.springframework.boot.web.support.SpringBootServletInitializer; 
  6. @SpringBootApplication 
  7. public class SpringBootWebApplication extends SpringBootServletInitializer { 
  8.  @Override 
  9.  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
  10.  return application.sources(SpringBootWebApplication.class); 
  11.  } 
  12.  public static void main(String[] args) throws Exception { 
  13.  SpringApplication.run(SpringBootWebApplication.class, args); 
  14.  } 

然后controller

  1. package com.mkyong; 
  2. import java.util.Map; 
  3. import org.springframework.beans.factory.annotation.Value; 
  4. import org.springframework.stereotype.Controller; 
  5. import org.springframework.web.bind.annotation.RequestMapping; 
  6. @Controller 
  7. public class WelcomeController { 
  8.  // inject via application.properties 
  9.  @Value("${welcome.message:test}") 
  10.  private String message = "Hello World"; 
  11.  @RequestMapping("/") 
  12.  public String welcome(Map<String, Object> model) { 
  13.  model.put("message", this.message); 
  14.  return "welcome"; 
  15.  } 

总结:

1.servlet的本质没有变化,从web框架的发展来看,web框架只是简化了开发servlet的工作,但还是遵循servlet规范的发展而发展的。

2.servlet的历史发展,从配置方式向编程方式到自动配置方式发展

3.spring mvc框架的分组:root和child(可以有多个dispatcherservlet),多个child可以共享root,child直接不共享

参考文献:

【1】https://en.wikipedia.org/wiki/Web_container

【2】https://baike.baidu.com/item/servlet/477555?fr=aladdin

【3】https://www.javatpoint.com/servlet-tutorial

【4】https://www.journaldev.com/1854/java-web-application-tutorial-for-beginners#deployment-descriptor

【5】https://blog.csdn.net/qq_22075041/article/details/78692780

【6】http://www.mkyong.com/spring-mvc/gradle-spring-mvc-web-project-example/

【7】http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/

(编辑:西安站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读