Spring Boot

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配置文件

```

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    4.0.0

    com.ah

    springbootdemo

    0.0.1-SNAPSHOT

   

       

        1.8

   

   

       

        org.springframework.boot

        spring-boot-starter-parent

        2.0.0.RELEASE

   

   

       

           

           

            org.springframework.boot

            spring-boot-starter-web

       

       

            com.fasterxml

            classmate

            1.0.0

       

   

```

    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)