Spring Boot

作者: Andy虎老狮 | 来源:发表于2019-03-31 15:35 被阅读0次

SpringBoot简介

什么是SpringBoot?

SpringBoot是Spring项目中的一个子工程,Boot在计算机界的含义是"引导".

SpringBoot官网:https://spring.io/projects

官方简介:

Takes an opinionated view of buildingproduction-ready Spring applications. Spring Boot favors convention overconfiguration and is designed to get you up and running as quickly as possible.

翻译以下:

用一些固定的方式来构建生产级别的spring应用。Spring Boot推崇约定大于配置的方式以便于你能够尽可能快速的启动并运行程序。

浓缩出来就是两点:

1.约定大于配置

2.快速开发程序

    Spring Boot被称为搭建程序的“脚手架”,用于快速地构建庞大的spring项目,并尽可能地减少xml配置,让开发人员专注于业务而非配置。

SpringBoot的价值

java项目有两个"痛点":

(1)配置太复杂。

(2)依赖管理复杂。项目要引入很多库,版本冲突经常发生。

        Spring Boot 简化了基于Spring的应用开发,只需要“run”就能创建Spring应用。

SpringBoot的特点

更多细节可以到[官网]查看。

快速入门

(1)创建Maven工程

(2)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>com.ah</groupId>

    <artifactId>springbootdemo</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <properties>

        <!-- maven默认jdk是1.5,改为1.8 -->

        <java.version>1.8</java.version>

    </properties>

    <parent>

        <!-- 父工程坐标 -->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.0.0.RELEASE</version>

    </parent>

    <dependencies>

        <dependency>

            <!-- web启动器 -->

            <!-- 无需指定版本信息,因为由SpringBoot控制版本 -->

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>com.fasterxml</groupId>

            <artifactId>classmate</artifactId>

            <version>1.0.0</version>

        </dependency>

    </dependencies>

</project>

```

    SpringBoot提供了一个名为spring-boot-starter-parent的工程,里面已经对各种常用依赖(并非全部)的版本进行了管理。我们的项目需要以这个项目为父工程,这样就不用操心依赖的版本问题了。

(3)启动类

Spring Boot项目通过main函数启动,需要创建一个启动类:

```

packagecom.ah.helloworld;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

//Spring Boot项目通过main函数启动

@SpringBootApplication

publicclassApplication {

    publicstaticvoidmain(String[]args) {

        SpringApplication.run(Application.class,args);

    }

}

```

启动此类,可以进入http://localhost:8080看一下效果。

(4)编写controller

```

packagecom.ah.helloworld;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.RestController;

@RestController// = @ResponseBody + @Controller

publicclassHelloController {

    @GetMapping("hello")// 浏览器访问http://localhost:8080/hello

    publicString hello() {

        return"hello,spring boot!";

    }

}

```

测试:浏览器访问http://localhost:8080/hello

相关文章

网友评论

    本文标题:Spring Boot

    本文链接:https://www.haomeiwen.com/subject/zsfubqtx.html