首页 技术 正文
技术 2022年11月23日
0 收藏 661 点赞 3,038 浏览 2125 个字

既然要将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.</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>8.5.</version>
</dependency>

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

模拟Springboot二:内置tomcat

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

public class StartTomcat implements Runnable {
@Override
public void run() {
//创建tomcat实例
Tomcat tom = new Tomcat();
//设置端口
tom.setPort();
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目录了)

    

 

  

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,360
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853