SpringBoot是什么?
它是建立在Spring框架基础之上的一个项目,提供了快速且简单的方式来开发、配置和运行简单或Web应用。
简单讲,它是Spring框架和内嵌服务器的组合,并且它使用约定大于配置的软件设计范式,大大简化了开发。
创建第一个简单应用
1. 新建一个maven工程,增加springboot的父pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>
2. 增加web的jar包引用
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3. 增加springboot的打包的maven插件,打包成一个可执行的jar包
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.exmaple.MyApp</mainClass>
<outputDirectory>target</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
整体的maven的pom.xml如下:
<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>org.example</groupId>
<artifactId>SpringBootTest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.exmaple.MyApp</mainClass>
<outputDirectory>target</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4. 添加main方法, 主类MyApp上面增加@SpringBootApplication注解
@SpringBootApplication
public class MyApp {
public MyApp() {
}
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
5. 添加controller
@RestController
public class UserController {
public UserController() {
}
@GetMapping({"/hello"})
public String hello() {
return "hello, world";
}
}
整体项目结构如下:

6. 执行maven打包,打包成jar包在target目录下面,通过java -jar SpringBootTest-1.0-SNAPSHOT.jar命令运行jar包。

7. 浏览器运行,看返回结果

8. 总结
是不是很简单几步,一个可执行的Web应用程序就搞好了,这里省去了很多以前MVC需要的配置以及生成的war包后的部署。

1811

被折叠的 条评论
为什么被折叠?



