首页 > 其他分享 >Spring Boot:热部署

Spring Boot:热部署

时间:2022-11-09 19:05:23浏览次数:45  
标签:index 部署 Spring boot springframework spring Boot import org


配置IDEA

勾选​​Build project automatically​​选项。

Spring Boot:热部署_html

搜索​​Registry​​​(双击​​Shift​​​键),找到并勾选​​compiler.automake.allow.when.app.running​​选项。

Spring Boot:热部署_intellij-idea_02


新版本​​IDEA​​​(博主的​​IDEA​​​版本是​​IntelliJ IDEA 2021.3​​​)没有​​compiler.automake.allow.when.app.running​​选项。

Spring Boot:热部署_intellij-idea_03


新版本​​IDEA​​​将此选项迁移到了​​Advanced Settings​​(高级设置)中,勾选下图标记的选项即可。

Spring Boot:热部署_java_04

配置Maven

添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

添加构建应用的插件:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>

测试

​pom.xml​​:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>

<packaging>jar</packaging>

<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>springboot</name>
<description>springboot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>

启动类:

package com.kaven.springboot;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
new SpringApplicationBuilder()
.sources(SpringbootApplication.class)
.bannerMode(Banner.Mode.OFF)
.lazyInitialization(true)
.run(args);
}
}

接口:

package com.kaven.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

@GetMapping("/index")
public String index() {
return "index.html";
}
}

​HTML​​页面。

Spring Boot:热部署_java_05

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index.html</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index2.html</h1>
</body>
</html>

启动应用,访问​​http://localhost:8080/index​​。

Spring Boot:热部署_spring_06

修改Java代码

修改接口(不重新启动应用):

package com.kaven.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class IndexController {

@GetMapping("/index")
public String index() {
return "index2.html";
}
}

Spring Boot:热部署_intellij-idea_07


控制台也有日志输出。

Spring Boot:热部署_spring boot_08

修改静态资源

修改静态资源(不重新启动应用):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>index2.html kaven</h1>
</body>
</html>

Spring Boot:热部署_spring_09

修改配置文件

修改默认配置(往配置文件​​application.properties​​中添加如下配置,不重新启动应用):

server.port=8081

之前的请求就无法访问了,因为应用的服务端口修改成了​​8081​​。

Spring Boot:热部署_java_10


因此,需要访问​​http://localhost:8081/index​​。

Spring Boot:热部署_spring boot_11


控制台也有日志输出。

Spring Boot:热部署_html_12


​Spring Boot​​的热部署就介绍到这里,如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。


标签:index,部署,Spring,boot,springframework,spring,Boot,import,org
From: https://blog.51cto.com/u_15870611/5838289

相关文章

  • Spring Boot:替换Web服务器
    ​​SpringBoot​​​应用包含默认的嵌入式​​Web​​容器。对于​​servlet​​​应用,​​spring-boot-starter-web​​​通过依赖​​spring-boot-starter-tomcat​​​......
  • Spring Boot:HTTP端口
    在​​SpringBoot​​​应用程序中,​​HTTP​​​端口默认为​​8080​​。可以通过​​server.port​​​属性来设置,往配置文件​​application.properties​​中添加以......
  • vcenter vcenter部署 & vSphere跨主机迁移
    目录vcentervcenter部署&vSphere跨主机迁移背景安装VMwarevCenterServer创建WindowsServer虚拟机安装WindowsServer虚拟机WindowsServer虚拟机设置安装VMwarevCen......
  • springBoot实现全局跨域
    importlombok.extern.slf4j.Slf4j;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.s......
  • spring boot 项目,使用 maven 构建工具打包的两种方法
    根据pom.xml文件指定的打包格式,打包成对于格式的包。如下:<packaging>war</packaging>使用IDEA的终端直接操作(或者cmd)。第一种方式:输入​​mvnpackage​​:打包成......
  • Spring Boot:The new driver class is com.mysql.cj.jdbc.Driver
    以前使用的是​​5.1.31​​​的​​mysql​​​驱动,后面使用​​SpringBoot​​​默认的​​mysql​​驱动,启动就报错了:Loadingclass`com.mysql.jdbc.Driver'.Thisis......
  • springmvc 整合 camunda
    参考官网:ProcessEngine配置|docs.camunda.org 一POM注意:1低版本c3p0会报错:Methodcom/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z千万记得要删除Ta......
  • 自定义SpringMVC(仅用学习)
    自定义MVC有两大核心:1.AppListener程序一启动,监听器就会对程序进行监听,此时会去获取config从而找到controller路径(在使用web项目时,web.xml需要先配置config信息)、再通......
  • 使用 Spring Boot 进行开发
    本节更详细地介绍了如何使用SpringBoot。它涵盖了诸如构建系统、自动配置以及如何运行应用程序等主题。我们还介绍了一些SpringBoot最佳实践。尽管SpringBoot没有什......
  • springboot+vue前后端分离国际化
    一,概要前端使用vue-i18n框架来实现国际化功能,国际化数据存储在数据库里,由后端接口提供,使用pinia缓存。后端使用redis缓存,并使用拦截器对响应中的提示信息做国际化。二......