先准备个简单的系统,配置和代码如下
# 服务器配置
server:
# 服务器端口
port: 8001
# Spring Boot 配置
spring:
# MVC 配置
mvc:
# Servlet 配置
servlet:
# Servlet 的访问路径
path: /sbd
# 应用程序配置
application:
# 应用程序名称
name: SpringBootDeployment
# 配置数据源
datasource:
hikari:
transaction-isolation: 2
# 驱动类
driver-class-name: com.mysql.jdbc.Driver
# 数据库连接
url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false
# 用户名
username: root
# 密码
password: Ms123!@#
package com.sbd;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Spring Boot 应用程序的主类。
* 使用@SpringBootApplication 注解标记这个类是一个 Spring Boot 的启动类。
* scanBasePackages 属性指定了 Spring Boot 自动扫描组件的包路径。
* @RestController 注解表示这个类是一个 REST 风格的控制器,用于处理 HTTP 请求。
*/
@SpringBootApplication(scanBasePackages = {
"com.sbd"})
@RestController
public class SpringBootDeploymentApplication {
/**
* 处理 GET 请求的测试方法。
* 该方法使用 @GetMapping 注解来指定处理所有的 GET 请求。
* 方法返回一个字符串 "Hello World!",这个字符串将作为 HTTP 响应的内容返回给客户端。
* @param request HttpServletRequest 对象,代表当前的 HTTP 请求。虽然在这个方法中没有使用,但它可以用于访问请求的相关信息。
* @return 字符串 "Hello World!",作为 HTTP 响应的内容。
*/
@GetMapping("/test")
public String test(HttpServletRequest request){
return "Hello World!";
}
/**
* 应用程序的入口点。
* 这个方法使用 SpringApplication.run() 来启动 Spring Boot 应用程序。
* @param args 命令行参数,这些参数在启动应用程序时可以提供。
*/
public static void main(String[] args) {
SpringApplication.run(SpringBootDeploymentApplication.class, args);
}
}
打包部署和运行SpringBoot项目
打包SpringBoot项目
假设MVN的系统环境已经配置好了,在命令行执行命令mvn clean package
即可打包
PS D:\Programs\SpringBootDeployment\SpringBootDeployment> mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------< com.sbd:sdd >-----------------------------
[INFO] Building SpringBootDeployment 0.0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ war ]---------------------------------
[WARNING] The artifact mysql:mysql-connector-java:jar:8.0.33 has been relocated to com.mysql:mysql-connector-j:jar:8.0.33: MySQL Connector/J artifacts moved to reverse-DNS compliant Maven 2+ coordinates.
[INFO]
[INFO] --- clean:3.3.2:clean (default-clean) @ sdd ---
[INFO] Deleting D:\Programs\SpringBootDeployment\SpringBootDeployment\target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ sdd ---
[INFO] Copying 1 resource from src\main\resources to target\classes
[INFO] Copying 0 resource from src\main\resources to target\classes
[INFO]
[INFO] --- compiler:3.13.0:compile (default-compile) @ sdd ---
[INFO] Recompiling the module because of changed source code.
[INFO] Compiling 2 source files with javac [debug parameters release 17] to target\classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ sdd ---
[INFO] skip non existing resourceDirectory D:\Programs\SpringBootDeployment\SpringBootDeployment\src\test\resources
[INFO]
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.sbd.SpringBootDeploymentApplicationTests
15:08:00.704 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils -- Could not detect default configuration classes for test class [com.sbd.SpringBootDeploymentApplicationTests]: SpringBootDeploymentApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
15:08:00.785 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper -- Found @SpringBootConfiguration com.sbd.SpringBootDeploymentApplication for test class com.sbd.SpringBootDeploymentApplicationTests
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.3.1)
2024-06-25T15:08:01.083+08:00 INFO 15232 --- [SpringBootDeployment] [ main] c.s.SpringBootDeploymentApplicationTests : Starting SpringBootDeploymentApplicationTests using Java 17.0.1 with PID 15232 (started by EDY in D:\Programs\SpringBootDeployment\SpringBootDeployment)
2024-06-25T15:08:01.084+08:00 INFO 15232 --- [SpringBootDeployment] [ main] c.s.SpringBootDeploymentApplicationTests : No active profile set, falling back to 1 default profile: "default"
2024-06-25T15:08:01.843+08:00 ERROR 15232 --- [SpringBootDeployment] [ main] o.a.catalina.core.AprLifecycleListener : An incompatible version [1.2.14] of the Apache Tomcat Native library is installed, while Tomcat requires version [1.2.34]
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2024-06-25T15:08:02.821+08:00 WARN 15232 --- [SpringBootDeployment] [ main] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: f87a8981-a777-4a00-9f08-5b1cdd3ba15e
This generated password is for development use only. Your security configuration must be updated before running your application in production.
2024-06-25T15:08:02.834+08:00 INFO 15232 --- [SpringBootDeployment] [ main] r$InitializeUserDetailsManagerConfigurer : Global AuthenticationManager configured with UserDetailsService bean with name inMemoryUserDetailsManager
2024-06-25T15:08:02.903+08:00 INFO 15232 --- [SpringBootDeployment] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint beneath base path '/actuator'
2024-06-25T15:08:03.041+08:00 INFO 15232 --- [SpringBootDeployment] [ main] c.s.SpringBootDeploymentApplicationTests : Started SpringBoot