前提描述
spring-boot目前还是比较流行的开源架构,每次开始写项目的时候都要重复搭建项目,为了避免新开项目时候的纠结和浪费时间,这里给出一个最简单的项目架构,每次开项目的时候只需要git一下即可。
配置pom.xml文件
1:先管理依赖的版本,直接用spring-boot-starter-parent作为父依赖,使用官方的推荐的依赖版本,可以避免开发过程中一些没必要的依赖冲突,其中的原理是:spring-boot-starter-parent的父依赖是spring-boot-dependencies```,而spring-boot-starter-parent管理了很多依赖的版本。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>
2.引入最基本的依赖:spring-boot-starter,spring-boot-starter-web,其中spring-boot-starter是spring-boot项目的核心依赖,包括了core,context,autoconfigure,logging,api等核心模块;spring-boot-starter-web是web应用的起步依赖,包括了tomcat,web,webmvc等重要模块。
<dependencies>
<!--springboot启动类依赖导入-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.打包
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
创建启动类并运行
创建启动类,标注@SpringBootApplication注解,运行main方法就可以启动项目。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.run();
}
}
资源地址
项目地址:https://gitee.com/ryfz-git/spring-min.git
结语
共同成长,共同进步,欢迎交流。