该文章长期更新

热部署

基本概念

热部署(Hot Deployment)是指在不停止当前运行程序的情况下,对代码或资源配置进行更新,且这些更新能够被即时反映到正在运行的程序中的一种技术。

开发环境热部署

  • pom.xml配置文件中添加dev-tools依赖。
  • 使用optional=true表示依赖不会传递,即该项目依赖devtools; 其他项目如果引入此项目生成的JAR包,则不会包含devtools
1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional >
</dependency>

在实际的项目开发调试过程中会频繁地修改后台类文件,导致需要重新编译、重新启动,整个过程非常麻烦,影响开发效率。

  • 而Spring Boot提供了spring-boot-devtools组件,使得无须手动重启Spring Boot应用即可重新编译启动项目, 大大缩短编译启动的时间。
  • devtools会监听classpath下的文件变动,触发Restart类加载器重新加载该类,从而实现类文件和属性文件的热部署。
  • 并不是所有的更改都需要重启应用(如静态资源、视图模板),可以通过设置spring.devtools.restart.exclude属性来指定—些文件或目录的修改不用重启应用。

配置devtools。

  • application.properties中修改以下代码。
1
2
3
4
5
6
# 热部署生效
spring.devtools.restart.enabled=true
# 设置重启目录
spring.devtools.restart.additional-paths=src/main/java
# 设置classpath目录下的WEB-INF 文件夹以下内容
spring.devtools.restart.exclude=static/**

非IDEA编译器配置

  • 如果使用了Eclipse,那么在修改完代’码 并保存之后, 项目将自动编译并触发重
    启,而如果使用了lntelliJ IDEA, 还需要配置项目自动编译。
  • 打开Settings页面, 在妇力的菜单栏依次找到Build,Execution,Deployment的Compile,勾选Build project automatically
  • *按Ctrl+Shift+Alt+快捷键调出Maintenance页面,单击Registry,勾选compiler.automake.allow.when.app.running复选框。
  • 做完这两步配置之后, 若开发者再次在lntelliJ IDEA中修改代码, 则项目会自动重启。

21 版本的idea 没有这个选项要去settings里找到Advansed Settings勾选Allow auto-make那个选项就可以了
汉语版的是:即使开发的应用程序当前正在运行,也允许自动make启动

引导html

用于非前后端分离式JavaWeb项目,脱离Nginx部署(IDEA)

基本概念

开发环境热部署

  1. 创建一个Spring Boot项目。如果你还没有创建,可以使用Spring Initializr(https://start.spring.io/)生成一个基本的Spring Boot项目结构。在Dependencies选项卡中,选择Web依赖项。

  2. 将你的HTML文件放在项目的src/main/resources/static目录下。这个目录是Spring Boot默认的静态资源目录,用于存放HTML、CSS和JavaScript文件。

  3. 在你的Spring Boot项目中,创建一个Controller类来处理HTTP请求。例如,你可以创建一个名为MyController的类,并添加一个方法来处理对HTML文件的请求。这个方法应该返回一个ModelAndView对象,其中包含要渲染的视图名称(即HTML文件的名称)和一个包含视图所需的数据的Map对象。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

@GetMapping("/")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "Hello, Spring Boot!");
return modelAndView;
}
}
  1. src/main/resources/templates目录下创建一个名为index.html的文件,并在其中编写HTML代码。例如:
1
2
3
4
5
6
7
8
9
10
11
12
13
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {
@RequestMapping("/")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "Hello, Spring Boot!");
return modelAndView;
}
}
1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Spring Boot HTML Example</title>
</head>
<body>
<h1>{{ message }}</h1>
</body>
</html>

非IDEA编译器配置

  • 如果使用了Eclipse,那么在修改完代’码 并保存之后, 项目将自动编译并触发重
    启,而如果使用了lntelliJ IDEA, 还需要配置项目自动编译。
  • 打开Settings页面, 在妇力的菜单栏依次找到Build,Execution,Deployment的Compile,勾选Build project automatically
  • *按Ctrl+Shift+Alt+快捷键调出Maintenance页面,单击Registry,勾选compiler.automake.allow.when.app.running复选框。
  • 做完这两步配置之后, 若开发者再次在lntelliJ IDEA中修改代码, 则项目会自动重启。

21 版本的idea 没有这个选项要去settings里找到Advansed Settings勾选Allow auto-make那个选项就可以了
汉语版的是:即使开发的应用程序当前正在运行,也允许自动make启动