zoukankan      html  css  js  c++  java
  • 模拟Springboot二:内置tomcat

    既然要将tomcat内置到项目中,并且能够成功的启动项目就要知道 tomcat  做了哪些事情 ,那么就必须先搞明白 

    一个 普通的web项目是如何被我们本地配置的tomcat启动并运行的

     (1)、 先告诉tomcat 要运行哪些项目 (也就是在使用eclipse、idea启动项目前对tomcat的配置工作、或linux上将编译后的war包拷贝到webapp下)

           从而在 后面在启动tomcat时,tomcat就会加载编译后的.class项目 

     

      (2)、 tomcat在加载编译后的项目时也会加载 web.xml或上篇博客的WebApplication文件 ,而在这个文件中

            做的事情就是 加载spring进而加载springMVC 

            加载SpringMVC: 将springMVC的DispatcherServlet注册到ServletContext容器 

     那么只需要在项目内部集成tomcat时,运行main方法启动tomcat之前将编译后的项目和tomcat相关联,然后tomcat就能自动加载WebApplication类了

       1、添加tomcat maven 依赖 (通过代码来创建tomcat实例)

            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>8.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-el</artifactId>
                <version>8.5.5</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <version>8.5.5</version>
            </dependency>

    2、创建SpringMain(作用:运行main启动tomcat)、StartTomcat(作用:创建Tom实例,然后让tomcat加载编译后的项

        

      (1)StartTomcat做的事情  创建线程类, springboot启动tomcat的源码也是这样的,这里只是写了必要的步骤    

    public class StartTomcat implements Runnable {
        @Override
        public void run() {
            //创建tomcat实例
            Tomcat tom = new Tomcat();
            //设置端口
            tom.setPort(8081);
            try{
                //获取项目编译后的claess 路径
                String path = StartTomcat.class.getResource("/").getPath();
    
                //获取webapp 文件
                String filePath = new File("src/main/webapp").getAbsolutePath();
    
                //然后将webapp下的项目添加至tomcat的context容器(context对应一个运行的项目)
                Context context =tom.addWebapp("/项目名",filePath); //参数1:一般是项目名 对应请求url中的项目名
           //webResourceRoot 用于加载 项目的class文件 
           WebResourceRoot webResource = new StandardRoot(context); webResource.addPreResources(new DirResourceSet(webResource,"/WEB-INF/classes",path,"/")); tom.start(); }catch (Exception e) { e.printStackTrace(); } //阻塞 ,等待前端连接 tom.getServer().await(); } }

            A、 创建tomcat实例

         B、 获取项目编译后的claess 路径

         C、获取webapp 目录下的项目

         D、然后将webapp下的项目添加至tomcat的context容器

         E、启动tomcat

         F、将tomcat的Server实例await 监听请求的到来

      (2) SpringMain     

    public class SpringMain {
    
        public static void main(String[] args) {
            //启动
            new StartTomcat().run();
        }
    }

           这里也可以自定义一个注解 ,启动标注了此注解了的main方法时 ,做new StartTomcat().run(); 的事情就可以了

    (3)、运行main方法  ,就能启动tom服务了

      

    下一篇 : springboot是如何访问 resources目录下的static、template等 静态资源的 (因为没了ssm项目中的webapp目录了)

        

      

      

     

  • 相关阅读:
    leetcode笔记--7 Find the Difference
    数据挖掘:概念与技术--笔记1--度量数据的相似性与相异性
    leetcode笔记--6 Add Digits
    leetcode 笔记5 single number
    数据挖掘导论笔记2 数据集的类型
    **leetcode笔记--4 Sum of Two Integers
    vs2015-Cordova开发安卓应用环境搭建
    c#一些常用的方法集合
    c#根据ip获取城市地址
    asp.net mvc 无刷新加载
  • 原文地址:https://www.cnblogs.com/bbdong/p/10214211.html
Copyright © 2011-2022 走看看