【问题标题】:Does the SpringBoot autmatically create Tomcat intance and what are the necessary bits present in classpath as per the document?Spring Boot 会自动创建 Tomcat 实例吗?根据文档,类路径中存在哪些必要位?
【发布时间】:2023-02-25 17:37:42
【问题描述】:

I was tryin to understand the difference between jar and war file, whether war file can be run on embedded tomcat server and gradually stumbled upon , starting of tomcat server. Which brought me to the following code line :

Tomcat tomcat = new Tomcat();

However i do not see this line anywhere in my personal projects, enterprise projects. I was imagining if Springboot automatically creates bean of tomcat server. And eventually led me to following documentation- embedded_servers_tomcat

Below is the screenshot of the lines :

Can someone explain what are these necessary bits and if possible please post the links for following if your have relevant resources for :

  1. Can war files be run using if so how ?
  2. Why do i not see the line Tomcat tomcat = new Tomcat(); in my java projects ?
  3. Why do we need war files , when we have the option to run jar files ?
  4. When we deploy application on linux servers , if we have multiple applciations , so multiple instances of tomcat servers is required (one standalone tomcat and multiple instances (Tomcat x1 = new Tomcat())) or multiple tomcat servers are required ?

I have 2.5 years of experience as a java-angular web application developer and I try learning daily and improving and understanding daily on how web applications work. Any help to answer my queries will be appreciated. Please don't downvote if you find my question naiive. We all begin somewhere , right !

I tried reading the documentation and watching videos on YouTube and blogs from javaatpoint etc. nothing gave me a clear or concise idea about the same.

  • Thanks for downvoting , i hope it made you look cool.

标签: java spring-boot eclipse maven tomcat


【解决方案1】:

先执行和测试,再提问。

非常简单的测试:

第一轮:Spring Boot JAR

请先用浏览器打开https://start.spring.io/

  • (1) 项目 - maven
  • (2) 语言 - java
  • (3) Spring Boot - 3.0.3,
  • (4) 包装,jar
  • (5) Java 17
  • (6) Dependencies,点击Dependencies,选择Spring Web
  • (7) 点击生成 --> 下载 demo.zip
  • (8)解压demo.zip
  • (9) 添加简单的休息控制器

演示/src/main/java/com/example/demo/HelloRestController.java

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloRestController {
    @RequestMapping("/hello")
    public String hello() {
        return "HELLO WORLD!";
    }
}
  • (10)编译打包
mvn clean package
  • (11) 运行Spring Boot jar
cd target
java -jar demo-0.0.1-SNAPSHOT.jar
  • (12) 打开浏览器http://localhost:8080/hello

第二轮:Spring Boot WAR

请先用浏览器打开https://start.spring.io/

  • (1) 项目 - maven
  • (2) 语言 - java
  • (3) Spring Boot - 3.0.3,
  • (4)包装,战争
  • (5) Java 17
  • (6) Dependencies,点击Dependencies,选择Spring Web
  • (7) 点击生成 --> 下载 demo.zip
  • (8)解压demo.zip
  • (9) 添加简单的休息控制器

演示/src/main/java/com/example/demo/HelloRestController.java

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloRestController {
    @RequestMapping("/hello")
    public String hello() {
        return "HELLO WORLD!";
    }
}
  • (10)编译打包
mvn clean package
  • (11)运行Spring Boot war
cd target
java -jar demo-0.0.1-SNAPSHOT.war
  • (12) 打开浏览器http://localhost:8080/hello

第 3 轮 - Spring Boot WAR - 但在 Java Web 容器下运行 - 像 Tomcat Standalone,JBoss EAP,......

  • 将 Round 2 target/demo-0.0.1-SNAPSHOT.war 重命名为 demo.war
  • 将 demo.war 复制到您的独立 tomcat 服务器 /webapps/
  • 启动你的 tomcat 服务器
  • 打开浏览器http://localhost:8080/demo/hello

Spring Boot 会自动创建 Tomcat 实例吗?

仅当您添加以下依赖项时:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2016-02-01
    • 2021-08-13
    • 1970-01-01
    相关资源
    最近更新 更多