简介
2024年11月17日大约 2 分钟
如无法下载源码执行该命令:mvn dependency:resolve -Dclassifier=sources
介绍
SpringBoot是基于Spring开发。继承了Spring框架原有的优秀特性,它并不是用来替代Spring的解决方案,二和Spring框架紧密结合进一步简化了Spring应用的整个搭建和开发过程
其设计目的是用来简化Spring应用的出事啊搭建以及开发过程
关于 SpringBoot 有一句很出名的话就是约定大于配置
优点
- 快速构建一个独立的 Spring 应用程序
- 嵌入的 Tomcat、 Jetty 或者 Undertow,无须部署 WAR 文件
- 提供starter POMs来简化Maven配置和减少版本冲突所带来的问题
- 对Spring和第三方库提供默认配置,也可修改默认值,简化框架配置
- 提供生产就绪型功能,如指标、健康检查和外部配置
- 无需配置XML,无代码生成,开箱即用
POM文件
<?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>
<!-- 以下内容是SpringBoot项目都要继承的一个内容,他指定的SpringBoot的版本和依赖 -->
<!-- 也可以不继承但是需要自己定义依赖管理 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 无父项目方式(建议使用上面的方式) -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 以下内容是项目本身的内容 -->
<groupId>top.xmln</groupId>
<artifactId>newSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- 以下内容是项目的描述 -->
<name>newSpringBoot</name>
<description>newSpringBoot</description>
<!-- 以下内容是项目的版本 -->
<properties>
<java.version>17</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-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 以下内容是项目的打包配置 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
启动类
@SpringBootApplication
public class NewSpringBootApplication {
// SpringBoot启动类
public static void main(String[] args) {
SpringApplication.run(NewSpringBootApplication.class, args);
}
}
通过使用SpringApplication
里的内容来启动springBoot程序
自定义SpringApplication
@SpringBootApplication
public class NewSpringBootApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(NewSpringBootApplication.class);
app.setBannerMode(Banner.Mode.OFF); // 关闭Banner(横幅)
// ...
app.run(args); // 启动
}
}
核心注解
@SpringBootApplication
@SpringBootConfiguration // 包含@Configuration注解的功能
@Configuration // JavaConfig的功能,配置类,结合@Bean能够将对象注入到IOC容器中
@EnableAutoConfiguration // 开启自动配置,避免写XML,需要使用的对象,由框架提供
@ComponentScan // 组件扫描器 <context:Component-scan base-pageage="xxx"/>
运行SpringBoot三种方式
- 使用IDEA执行main方法
- 使用Maven插件:mvn spring-boot:run
- java -jar xxx.jar