Spring Boot 简介及快速搭建

1.简介

SpringBoot它基于Spring4.0设计,是由 Pivotal 公司提供的框架。

2014 年 4 月发布 Spring Boot 1.0 基于Spring4.0

2018 年 3 月 Spring Boot 2.0发布 基于Spring 5.0。

优点

--快速构建一个独立的 Spring 应用程序 ;

--嵌入的 Tomcat 、 Jetty 或者 Undertow,无须部署 WAR 文件;

--提供starter POMs来简化Maven配置和减少版本冲突所带来的问题;

--对Spring和第三方库提供默认配置,也可修改默认值,简化框架配置;

--提供生产就绪型功能,如指标、健康检查和外部配置;

--无需配置XML,无代码生成,开箱即用;

环境要求

Spring Boot 2.3.5.RELEASE需要Java 8,并且与Java 15(包括)兼容。 还需要Spring Framework 5.2.10.RELEASE或更高版本。

构建工具的支持

Build Tool

Version

Maven

3.3+

Gradle

6 (6.3 or later). 5.6.x is also supported but in a deprecated form

开发工具

eclipse

Idea 2018+(别忘了配置Maven、jdk)

2.快速开始 SpringBoot Hello World

2.1创建一个maven工程;

(jar)

2.2项目中引入依赖

   <!--继承springboot的父项目-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
​
   <dependencies>
       <!--引入springboot的web支持-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>
   </dependencies>

2.3建包并创建控制器

//在项目中创建指定的包结构
@Controller
@RequestMapping("/hello")
public class HelloController {
   @RequestMapping("/world")
   @ResponseBody
   public String hello(){
       System.out.println("======hello world=======");
       return "hello world";
  }
}
     

2.4编写启动类

//在项目中如下的包结构中创建启动类 Application
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

2.5运行main启动项目

出现以上日志说明启动成功

2.6访问项目

注意: springboot的项目默认没有项目名

访问路径: http://localhost:8080/hello/world

2.7修改端口

如果出现Web server failed to start. Port 8080 was already in use.说明端口正在使用,我们需要修改默认端口

项目中src/main/resources/application.properties

server.port=8088

访问路径: http://localhost:8088/hello/world

2.8部署服务器

<!‐‐ 这个插件,可以将应用打包成一个可执行的jar包;‐‐>
<build>
<plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring‐boot‐maven‐plugin</artifactId>
    </plugin>
</plugins>
</build>

将这个应用打成jar包,直接使用java -jar的命令进行执行;

3.代码说明

  • spring-boot-starter-parent

<!--这是Spring Boot的父级依赖,这样当前的项目就是Spring Boot项目了。
它用来提供相关的Maven默认依赖。使用它之后,常用的包依赖可以省去version标签。-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<!--
spring-boot-starter-parent的父maven
它管理了Spring Boot应用里面的所有依赖版本;    
-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-dependencies</artifactId>
  <version>2.3.5.RELEASE</version>
</parent>
  • spring-boot-starter-web

spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter

相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

  • Application 启动类

/**
 *  @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
    自动装配就是从这里开始的
 */
@SpringBootApplication
public class Application {
 
    public static void main(String[] args) {
 
        // Spring应用启动起来
        SpringApplication.run(Application.class,args);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值