SpringBoot入门

打印 上一主题 下一主题

主题 913|帖子 913|积分 2739

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继承。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5.     <modelVersion>4.0.0</modelVersion>
  6.     <groupId>org.test</groupId>
  7.     <artifactId>test</artifactId>
  8.     <version>1.0-SNAPSHOT</version>
  9.     <properties>
  10.         <maven.compiler.source>10</maven.compiler.source>
  11.         <maven.compiler.target>10</maven.compiler.target>
  12.     </properties>
  13.     <parent>
  14.         <groupId>org.springframework.boot</groupId>
  15.         <artifactId>spring-boot-starter-parent</artifactId>
  16.         <version>2.4.2</version>
  17.     </parent>
  18.     <dependencies>
  19.         <dependency>
  20.             <groupId>org.springframework.boot</groupId>
  21.             <artifactId>spring-boot-starter-web</artifactId>
  22.         </dependency>
  23.     </dependencies>
  24.     <build>
  25.         <plugins>
  26.             <plugin>
  27.                 <groupId>org.springframework.boot</groupId>
  28.                 <artifactId>spring-boot-maven-plugin</artifactId>
  29.             </plugin>
  30.         </plugins>
  31.     </build>
  32. </project>
复制代码
4.Hello World
SpringBoot通过spring-boot-starter-web来添加classpath,默认在src/main/java目录中创建以下文件。其中@RestController标识当前控制器接受到web请求处理,@RequestMapping定义http请求规则在“/”内执行。
@SpringBootApplication包括@EnableAutoConfiguration(开启自动配置beans),@ComponentScan(扫描本地应用包的代码),@Configuration(注册扩展beans和第三方包配置类)
  1. import org.springframework.boot.*;
  2. import org.springframework.boot.autoconfigure.*;
  3. import org.springframework.web.bind.annotation.*;
  4. @RestController
  5. @SpringBootAppliation
  6. public class Example {
  7.     @RequestMapping("/")
  8.     String home() {
  9.         return "Hello World!";
  10.     }
  11.     public static void main(String[] args) {
  12.         SpringApplication.run(Example.class, args);
  13.     }
  14. }
复制代码
 5.run demo
在spring-boot-starter-parent中默认通过,mvn spring-boot:run命令执行程序,启动web容器。
6.creating an Executable Jar
spring-boot-maven-plugin插件默认打包可执行jar文件,mvn package执行打包。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

熊熊出没

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表