前言
每天都在使用springboot
随便copy过来一个直接使用
那么是否真正的具备工程能力吗?
- idea 2021
- jkd8
- mvn3.6.3
- 阿里云OS
用简单的材料一步一步,回顾下每天用的springboot工程如何来的。
简单web工程
简单web,不连接任何数据库,只需要提供能够提供rest服务即可。
- 创建一个web工程
- 统一返回值
- 统一异常处理
- 日志配置
- 适配多环境profile
- 支持打出jar和tar.gz包
- linux服务器发布
创建springboot-web工程
File->New Project->Maven
于是我们就得到了一个简单的工程
mavn核心的配置文件就是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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wht.test</groupId>
<artifactId>SpringBootWebTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
创建基础的目录结构
其中的一种方式,有些项目可能是模块化,有些是一个业务模块一个工程。
terminal是一个终端,玩过操作系统的都应该明白,基本操作都可以在这里使用。
内嵌cmd命令,以及各种外部有效命令可以直接在这里执行,相当于cmd窗口
查看目录结构:
PS D:\workspace\java\SpringBootWebTest> tree
卷 DATA 的文件夹 PATH 列表
卷序列号为 12E3-5DA9
D:.
├─.idea
├─config
└─src
├─main
│ ├─java
│ │ └─com
│ │ └─wht
│ │ └─test
│ │ ├─controller
│ │ ├─exception
│ │ ├─mapper
│ │ ├─service
│ │ └─util
│ └─resources
└─test
└─java
如下图:
- config 目录放到了工程根目录下,真实业务上经常这么干为了剥离代码,便于操作和安控。
- com.wht.test这个企业包命名习惯
- controller 控制层
- exception 统一异常处理
- mapper 持久层暂时预留
- service 业务层
- util工具类目录
由于这是简单的实例,暂时这些够用了。
引入基础依赖
目前能看到工程基础依赖只有JDK8基础环境,需要引入springboot-web的基础模块
修改pom.xml
- 引入parent:spring-boot-starter-parent 版本很重要2.3.8.RELEASE
- 添加依赖:spring-boot-starter-web
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wht.test</groupId>
<artifactId>SpringBootWebTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
创建启动类
- com.wht.test 下创建启动类TestAppStart
- 运行main方法
package com.wht.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestAppStart {
public static void main(String[] args) {
SpringApplication.run(TestAppStart.class, args);
}
}
运行日志很重要,如下:
每一行日志都很重要,都应该知道什么意思,是了解springboot的基础
No active profile set, falling back to default profiles: default
- 没有找到配置文件默认default,如果多环境定位配置很重要
Tomcat started on port(s): 8080 (http) with context path ''
-
内嵌tomcat
-
端口port 8080 默认端口
-
上下文根默认没有
"C:\Program Files\Java\jdk1.8.0_341\bin\java.exe" -javaagent:D:\setup\idea\lib\idea_rt.jar=64406:D:\setup\idea\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_341\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_341\jre\lib\rt.jar;D:\workspace\java\SpringBootWebTest\target\classes;D:\repository\org\springframework\boot\spring-boot-starter-web\2.3.8.RELEASE\spring-boot-starter-web-2.3.8.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter\2.3.8.RELEASE\spring-boot-starter-2.3.8.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot\2.3.8.RELEASE\spring-boot-2.3.8.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-autoconfigure\2.3.8.RELEASE\spring-boot-autoconfigure-2.3.8.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-logging\2.3.8.RELEASE\spring-boot-starter-logging-2.3.8.RELEASE.jar;D:\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;D:\repository\org\apache\logging\log4j\log4j-to-slf4j\2.13.3\log4j-to-slf4j-2.13.3.jar;D:\repository\org\apache\logging\log4j\log4j-api\2.13.3\log4j-api-2.13.3.jar;D:\repository\org\slf4j\jul-to-slf4j\1.7.30\jul-to-slf4j-1.7.30.jar;D:\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\repository\org\springframework\spring-core\5.2.12.RELEASE\spring-core-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-jcl\5.2.12.RELEASE\spring-jcl-5.2.12.RELEASE.jar;D:\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;D:\repository\org\springframework\boot\spring-boot-starter-json\2.3.8.RELEASE\spring-boot-starter-json-2.3.8.RELEASE.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.11.4\jackson-databind-2.11.4.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.11.4\jackson-annotations-2.11.4.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.11.4\jackson-core-2.11.4.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.11.4\jackson-datatype-jdk8-2.11.4.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.11.4\jackson-datatype-jsr310-2.11.4.jar;D:\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.11.4\jackson-module-parameter-names-2.11.4.jar;D:\repository\org\springframework\boot\spring-boot-starter-tomcat\2.3.8.RELEASE\spring-boot-starter-tomcat-2.3.8.RELEASE.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.41\tomcat-embed-core-9.0.41.jar;D:\repository\org\glassfish\jakarta.el\3.0.3\jakarta.el-3.0.3.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.41\tomcat-embed-websocket-9.0.41.jar;D:\repository\org\springframework\spring-web\5.2.12.RELEASE\spring-web-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-beans\5.2.12.RELEASE\spring-beans-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-webmvc\5.2.12.RELEASE\spring-webmvc-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-aop\5.2.12.RELEASE\spring-aop-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-context\5.2.12.RELEASE\spring-context-5.2.12.RELEASE.jar;D:\repository\org\springframework\spring-expression\5.2.12.RELEASE\spring-expression-5.2.12.RELEASE.jar" com.wht.test.TestAppStart
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.8.RELEASE)
2023-04-24 21:07:42.322 INFO 13704 --- [ main] com.wht.test.TestAppStart : Starting TestAppStart on DESKTOP-TVKIOH9 with PID 13704 (D:\workspace\java\SpringBootWebTest\target\classes started by 13355 in D:\workspace\java\SpringBootWebTest)
2023-04-24 21:07:42.325 INFO 13704 --- [ main] com.wht.test.TestAppStart : No active profile set, falling back to default profiles: default
2023-04-24 21:07:43.280 INFO 13704 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-04-24 21:07:43.291 INFO 13704 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-04-24 21:07:43.292 INFO 13704 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2023-04-24 21:07:43.371 INFO 13704 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-04-24 21:07:43.371 INFO 13704 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 996 ms
2023-04-24 21:07:43.520 INFO 13704 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2023-04-24 21:07:43.663 INFO 13704 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-04-24 21:07:43.670 INFO 13704 --- [ main] com.wht.test.TestAppStart : Started TestAppStart in 1.707 seconds (JVM running for 2.169)
访问
没有暴露任何接口,但是可以看到8080端口已经有了
立即窗口验证下:
PS D:\workspace\java\SpringBootWebTest> netstat -an|findstr 8080
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING
TCP [::]:8080 [::]:0 LISTENING
TCP [::1]:8080 [::1]:64599 FIN_WAIT_2
TCP [::1]:8080 [::1]:64600 FIN_WAIT_2
TCP [::1]:64599 [::1]:8080 CLOSE_WAIT
TCP [::1]:64600 [::1]:8080 CLOSE_WAIT
PS D:\workspace\java\SpringBootWebTest>
通过命令方式也是linux服务器运维基础知识,常常用来查看服务运行问题,网络请求健康并发统计等。
标签:web,启航,工程,spring,boot,jar,repository,RELEASE,org From: https://www.cnblogs.com/hcgk/p/17350968.html