=================================
SpringBoot 标准项目创建步骤
=================================
使用 Spring IDE(Eclipse), 可以新建一个 Spring starter project项目, 是一个项目向导, 在向导提示下可以按需添加 SpringBoot的常用依赖项目, 该向导生成一个非常规范的 pom.xml, 非常规范的目录结构, 值得推荐.

=================================
创建极简SpringBoot tomcatsample程序的步骤
=================================
1. 准备 JDK8
2. Eclipse 中创建 Maven Project, archetype 选择 quickstart那个即可.
groupid 为: com.springbootsample
artifactId 为: tomcatsample
3. 配置 pom.xml 文件, 包括:
a) 增加 spring-boot-starter-parent parent节点
b) 增加 spring-boot-starter-we 依赖项
c) 增加 spring-boot-devtools 依赖项(开发辅助包)
d) 增加 spring-boot-maven-plugin build 插件
4. 手工增加 resources 目录, controller/entity/service等java 目录,
手工添加 application.properties 文件, 设置端口号和url根, 内容如下:
server.port=8888
server.servlet.context-path=/tomcatsample
#server.context-path=/tomcatsample #SpringBoot V1 property, not works in SB2
5. 最终的项目结构如下:

└─src
│ └─main
│ ├─java
│ │ └─com
│ │ └─springbootsample
│ │ └─tomcatsample
│ │ ├─controller <dir>
│ │ ├─entity <dir>
│ │ └─service <dir>
│ │ └─App.java <主程序文件>
│ └─resources
│ └─templates <dir>
│ └─application.properties <配置文件>
├─pom.xml <Maven 主文件>
├─.project <Eclipse 项目文件>
└─.classpath <classpath 文件>
6. App.java 中, 使用 @SpringBootApplication 注解
7. 在 controller 目录下增加一个 IndexController, 用来做 url mapping.

=================================
将 SpringBoot 部署到外置的Tomcat中
=================================
SpringBoot 2需要servlet V3的支持, 所以需要使用Tomcat 7或8, 我使用的是tomcat 8. 
系统开发时一般都是使用SpringBoot 内置tomcat运行的, 很方便. 而在生产环境中, 通常需要将SpringBoot部署在外置Tomcat server上. 网上很多示例都只能运行在内置tomcat中, 这里提供一个兼容内置/外置Tomcat的样例, 代码和配置需要做一些调整.

步骤有:
1. 修改 pom.xml, 这包括:
a) 修改为 war 打包形式
b) 使用 spring-boot-maven-plugin 编译插件, 目标包将是fat war或fat jar, 可以直接运行.
c) 将依赖项 spring-boot-starter-tomcat 标示为 provided.
d) 在 properties tag 下, 增加 start-class tag, 指定SpringBoot程序的真正入口
e) 在 build tag 下, 增加 finalName tag, 缩短war包的文件名, 否则url地址将包含版本信息, 比如url包含: tomcatsample-0.0.1-SNAPSHOT , 非常不友好.
2. 修改主程序入口类
主程序类需要继承SpringBootServletInitializer, 并重写configure()方法.
3. 使用 maven package 打包成 war 包, 并复制到 tomcat安装目录的webapps子目录下.
4. 修改 tomcat 的 conf\server.xml 文件, 搜索 8080 并用新端口替换, 比如换成 8888 .
5. 使用 tomcat 的 bin\startup.bat启动
6. 访问 web url,
比如: http://127.0.0.1:8888/tomcatsample/index

=================================
其他说明
=================================
1. 这种 war 包是一个 fat 的 executable 压缩包, 包含所有的资源文件和依赖jar包.
2. 使用这种 war 包可以简化了程序部署, 只需部署一个文件即可. 启动Tomcat时, tomcat会自动解压war包到webapps目录, 并启动我们的SpringBoot应用. 将来升级程序, 也仅仅需要覆盖war包即可, tomcat会自动使用新的war包.
3. SpringBoot参数解析器非常智能, 不管参数名是按照驼峰写法, 还是按照下划线写法, 还是按照连接符写法, 都能自动和Bean中的属性关联起来. 即参数中 server.servlet.contextPath 和 server.servlet.context_path 和 server.servlet.context-path 写法其实都是等价的.

=================================
SpringBoot程序的几种启动方式:
=================================
1. [内置tomcat]在Eclipse 以 Java Application 的形式
2. [内置tomcat]使用 mvn spring-boot:run 启动
3. [外置tomcat]使用tomcat的 startup 脚本启动
4. [内置tomcat]使用 java jar tomcatsample.war 的形式.
针对第4种运行方式, 可以传入新参数来覆盖SpringBoot默认值或application.properties的设定值, 比如:
a) 最简启动形式
java -jar tomcatsample.war
b) 以 jvm 环境变量的形式传入 server.port 值
java -Dserver.port=5566 -jar tomcatsample.war
c) 以 application.properties的形式传入 server.port 值
java -jar tomcatsample.war --server.port=7788
java -jar tomcatsample.war server.servlet.context-path
相比 -D 环境变量的形式, 更推荐使用--传参, 因为该传参名称和 application.properties 中的配置项写法完全一致, 而-D的环境变量名称有时候和 application.properties 中的配置项名称不一致.

=================================
tomcatsample 代码
=================================

App.java

  1. package com.springbootsample.tomcatsample;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.boot.builder.SpringApplicationBuilder;
  6. import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
  7.  
  8. //在内嵌Tomcat开发或部署, 下面写法最简单.
  9. //@SpringBootApplication
  10. //public class App
  11. //{
  12. // public static void main( String[] args )
  13. // {
  14. // SpringApplication.run(App.class, args);
  15. // }
  16. //}
  17.  
  18. // 要在外置Tomcat上部署, 需要继承SpringBootServletInitializer, 并重写configure()方法.
  19. // 需要说明的是, 这种写法对内置Tomcat也同样支持.
  20. @SpringBootApplication
  21. public class App extends SpringBootServletInitializer {
  22.  
  23. @Override
  24. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  25. return application.sources(App.class);
  26. }
  27.  
  28. public static void main(String[] args) throws Exception {
  29. SpringApplication.run(App.class, args);
  30. }
  31.  
  32. }

IndexController.java

  1. package com.springbootsample.tomcatsample.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6.  
  7. @Controller
  8. @RequestMapping("/")
  9. public class IndexController {
  10. @RequestMapping("/index")
  11. @ResponseBody
  12. public String index() {
  13. return "index1";
  14. }
  15. }

pom.xml

  1.  
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.springbootsample</groupId>
  7. <artifactId>tomcatsample</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <!-- 打包形式修改为 war -->
  10. <packaging>war</packaging>
  11.  
  12. <name>tomcatsample</name>
  13. <url>http://maven.apache.org</url>
  14.  
  15. <properties>
  16. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  17. <!--optional, 如果项目代码包含多个main()函数, 需要告知SpringBoot真正的入口class-->
  18. <start-class>com.springbootsample.tomcatsample.App</start-class>
  19. </properties>
  20.  
  21. <!-- 增加依赖 parent 项目spring-boot-starter-parent -->
  22. <parent>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-parent</artifactId>
  25. <version>2.0.2.RELEASE</version>
  26. </parent>
  27.  
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33.  
  34. <!-- marked the embedded tomcat servlet container as provided -->
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-tomcat</artifactId>
  38. <scope>provided</scope>
  39. </dependency>
  40.  
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46.  
  47. <!-- spring-boot-devtools, 在开发过程中支持Auto restart 和 auto reload, 打包为war/jar不会包含该依赖.-->
  48. <dependency>
  49. <groupId>org.springframework.boot</groupId>
  50. <artifactId>spring-boot-devtools</artifactId>
  51. <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
  52. <optional>true</optional>
  53. </dependency>
  54.  
  55. <!--使用freemarker的starter,里面包含spring-boot-starter-web组件-->
  56. <dependency>
  57. <groupId>org.springframework.boot</groupId>
  58. <artifactId>spring-boot-starter-freemarker</artifactId>
  59. </dependency>
  60.  
  61. <!--mybatis依赖-->
  62. <dependency>
  63. <groupId>org.mybatis.spring.boot</groupId>
  64. <artifactId>mybatis-spring-boot-starter</artifactId>
  65. </dependency>
  66.  
  67. <!--druid连接池依赖-->
  68. <dependency>
  69. <groupId>com.alibaba</groupId>
  70. <artifactId>druid-spring-boot-starter</artifactId>
  71. </dependency>
  72. </dependencies>
  73.  
  74. <build>
  75. <!-- 重命名打包 jar 的文件, 不含版本信息 -->
  76. <finalName>tomcatsample</finalName>
  77. <plugins>
  78. <!--optional, 加上spring-boot-maven-plugin, 这样jar/war打包是可以 executable 的 -->
  79. <plugin>
  80. <groupId>org.springframework.boot</groupId>
  81. <artifactId>spring-boot-maven-plugin</artifactId>
  82. </plugin>
  83.  
  84. <!--optional, mybatis-generator-maven 插件基于数据table 生成 java code -->
  85. <plugin>
  86. <groupId>org.mybatis.generator</groupId>
  87. <artifactId>mybatis-generator-maven-plugin</artifactId>
  88. <configuration>
  89. <verbose>true</verbose>
  90. <overwrite>true</overwrite>
  91. <!--配置文件的路径-->
  92. <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
  93. </configuration>
  94. </plugin>
  95. </plugins>
  96.  
  97. <resources>
  98. <!-- 将 java 目录下的下面文件以资源文件的形式打包到jar输出文件中 -->
  99. <resource>
  100. <directory>src/main/java</directory>
  101. <includes>
  102. <include>**/*.properties</include>
  103. <include>**/*.xml</include>
  104. <include>**/*.sql</include>
  105. <include>**/*.ddl</include>
  106. <include>**/*.txt</include>
  107. <include>**/*.md</include>
  108. </includes>
  109. <filtering>true</filtering>
  110. </resource>
  111. </resources>
  112. </build>
  113. </project>

=================================
参考
=================================
https://www.jianshu.com/p/157eb1ab8524
https://www.mkyong.com/spring-boot/spring-boot-deploy-war-file-to-tomcat/
http://www.codesheep.cn/2018/06/05/SpringBoot%E5%BA%94%E7%94%A8%E9%83%A8%E7%BD%B2%E4%BA%8E%E5%A4%96%E7%BD%AETomcat%E5%AE%B9%E5%99%A8/

SpringBoot系列: 极简Demo程序和Tomcat war包部署的更多相关文章

  1. tomcat war包部署

    平常的开发我们都是通过IDE进行项目的部署,但有时候我们不得不进行手工部署(例如在Server上). 手工部署分为以下几步: 第1步: 用maven打war包 (假如得到的war包名为: appkit ...

  2. gitlab+jenkins+tomcat war包部署(此文有新版本)

    对本文进行格式整理,url: https://www.cnblogs.com/huandada/p/9969234.html 整个项目的框架为: 1.gitlab的安装(Centos7) 新建/etc ...

  3. SpringBoot之打成war包部署到Tomcat

    正常情况下SpringBoot项目是以jar包的形式,正常情况下SpringBoot项目是以jar包的形式,并且SpringBoot是内嵌Tomcat服务器,所以每次重新启动都是用的新的Tomcat服 ...

  4. springboot 学习之路 5(打成war包部署tomcat)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  5. SpringBoot项目war包部署

    服务部署 记录原因 将本地SpringBoot项目通过war包部署到虚拟机中,验证服务器部署. 使用war包是为了方便替换配置文件等. 工具 对象 版本 Spring Boot 2.4.0 VMwar ...

  6. Springboot War包部署下nacos无法注册问题

    目录 1. @EnableDiscoveryClient的使用 2. EnableDiscoveryClientImportSelector类的作用 3.AutoServiceRegistration ...

  7. springboot项目war包部署及出现的问题Failed to bind properties under 'mybatis.configuration.mapped-statements[0].

    1.修改pom文件 修改打包方式 为war: 添加tomcat使用范围,provided的意思即在发布的时候有外部提供,内置的tomcat就不会打包进去 <groupId>com.scho ...

  8. Windows下war包部署到Linux下Tomcat出现的问题

    最近,将Windows下开发的war包部署到Linux下的Tomcat时报了一个错误:tomcat error in opening zip file.按理说,如果正常,当把war包复制到webapp ...

  9. 将Web项目War包部署到Tomcat服务器

    1. 配置Java运行环境 1.1 下载并安装JDK 从官网上下载最新的JDK:http://java.sun.com/javase/downloads/index.jsp ,下载后安装,选择想把JD ...

随机推荐

  1. pip 安装第三方包提示Unknown or unsupported command 'install'

    Unknown or unsupported command 'install' Unknown or unsupported command 'show' Unknown or unsupporte ...

  2. Thinkphp5 captcha扩展包安装,验证码验证以及点击刷新

    首先下载 captcha扩展包,↓ 下载附件,解压到vendor目录下: 然后进入application/config.php添加配置信息: //验证码       'captcha'  =>  ...

  3. IO创建Socket通信中慎用BufferReader中的readLine()

    在编写Socket的Demo的时候,在Server中使用BufferReader获取从客服端发送过来的内容 package cn.lonecloud.socket; import cn.loneclo ...

  4. 时间复杂度和大O表示法

    大O表示法:称一个函数g(n)是O(f(n)),当且仅当存在常数c>0和n0>=1,对一切n>n0均有|g(n)|<=c|f(n)|成立,也称函数g(n)以f(n)为界或者称g ...

  5. notepad问题汇总

    右键无法设置为默认打开方式:https://blog.csdn.net/jl1134069094/article/details/50749075

  6. 汇编 gdb调试

    as -g --32 -o hello.o hello.s ld -m elf_i386 -o hello hello.o gdb hello

  7. io系列之字节流

    java中io流系统庞大,知识点众多,作为小白通过五天的视频书籍学习后,总结了io系列的随笔,以便将来复习查看. 本篇为此系列随笔的第一篇:io系列之字节流. 一.字节流的File读写操作. Inpu ...

  8. python config.ini的应用

    config.ini文件的结构是以下这样的:结构是"[ ]"之下是一个section,一部分一部分的结构.以下有三个section,分别为section0,section1,sec ...

  9. python logging日志模块

    一.logging模块的简介 logging模块是Python内置的标准模块,主要用于输出运行日志,可以设置输出日志的等级.日志保存路径.日志文件回滚等:相比print,具备如下优点: 可以通过设置不 ...

  10. id选择器为变量时

    使用angularjs或者freemarker的同学基本都接触过一个问题:当使用list遍历数组值显示在页面,并要对所显示的数值进行操作时,如何选取数值所在标签? 以下是一个把地址id转为中文地址的函 ...