1.介绍:
Spring Boot是一个基于Spring框架的开源项目,旨在简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
Spring Boot提供了丰富的Spring模块化支持,可以帮助开发者更轻松快捷地构建出企业级应用。 它通过自动配置功能,降低了复杂性,同时支持基于JVM的多种开源框架,可以缩短开发时间,使开发更加简单和高效。
2.系统要求:
- JDK 环境 必须 是 1.8 或者 jdk11 版本及以上。
- 后面要使用到 Maven 管理工具 3.5+ 及以上版本,建议是:3.6 不要用最新。
- 内置了Tomcat9.x/Servlet4.x。
- 开发工具建议使用 IDEA,也可以 MyEclipse,为了实现一站式服务。
3.maven安装SpringBoot:
SpringBoot依赖org.springframework.boot这个groupId和从spring-boot-starter-parent这个artifactId继承。- <?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>org.test</groupId>
- <artifactId>test</artifactId>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <maven.compiler.source>10</maven.compiler.source>
- <maven.compiler.target>10</maven.compiler.target>
- </properties>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.4.2</version>
- </parent>
- <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>
- </plugin>
- </plugins>
- </build>
- </project>
复制代码 4.Hello World
SpringBoot通过spring-boot-starter-web来添加classpath,默认在src/main/java目录中创建以下文件。其中@RestController标识当前控制器接受到web请求处理,@RequestMapping定义http请求规则在“/”内执行。
@SpringBootApplication包括@EnableAutoConfiguration(开启自动配置beans),@ComponentScan(扫描本地应用包的代码),@Configuration(注册扩展beans和第三方包配置类)- import org.springframework.boot.*;
- import org.springframework.boot.autoconfigure.*;
- import org.springframework.web.bind.annotation.*;
- @RestController
- @SpringBootAppliation
- public class Example {
- @RequestMapping("/")
- String home() {
- return "Hello World!";
- }
- public static void main(String[] args) {
- SpringApplication.run(Example.class, args);
- }
- }
复制代码 5.run demo
在spring-boot-starter-parent中默认通过,mvn spring-boot:run命令执行程序,启动web容器。
6.creating an Executable Jar
spring-boot-maven-plugin插件默认打包可执行jar文件,mvn package执行打包。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |