springboot快速入门

前言

Springboot是时下非常热门的java开发框架,它采用约定大于配置的开发理念,使我们几乎以零配置的方式快速构建一个java项目,非常优雅!

什么是Spring Boot?

Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。简而言之,Spring Boot通过提供默认配置的方式整合了所有的框架,让我们可以更加简单、快速、方便地构建应用程序。

为什么要用Spring Boot?

Spring Boot包含以下几个特性:

  1. 默认提供了大部分框架的使用方式,方便进行快速集成

  2. Spring Boot应用可以独立运行,符合微服务的开发理念

  3. Spring Boot内置WEB容器,无需部署WAR包即可运行

  4. 提供了各种生产就绪型功能,如指标,健康检查和外部配置

  5. Spring Boot通过网站提供了项目模板,方便项目的初始化

通过以上这些非常优秀的特性,Spring Boot可以帮助我们非常简单、快速的构建起我们的项目,并能够非常方便进行后续开发、测试和部署。


话不多说,学习一门新技术从“HelloWorld”开始是再合适不过的了!

一、开发第一个 Spring Boot 应用

下面会介绍maven和springboot脚手架2种方式开始我们的springboot之旅

环境准备

  • JDK 环境必须是 1.8 及以上

  • IDEA 2021.1.3

  • Maven 3.6.3

方式一:Maven工程


(1)创建maven项目

可以看见这是一个非常存粹的maven项目,和springboot没有任何关系!


(2)引入spring-boot-starter-parent依赖

 
           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">
     4.0.0
 
     org.example
     maven-helloworld
     1.0-SNAPSHOT
     
     
         org.springframework.boot
         spring-boot-starter-parent
         2.5.6
         
     

 
 


spring-boot-starter-parent是springboot项目用于管理jar包版本的,也就是我们经常说的“版本仲裁”,记住它仅仅是管理版本的,并不会导入依赖,记住这点非常重要!我以前就搞混淆了,害我迷惑很长一段时间!!!

关于这一点我们可以用mvn dependency:tree查看项目依赖来验证,发现依赖为空!Good Gay!

 [INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ maven-helloworld ---[INFO] org.example:maven-helloworld:jar:1.0-SNAPSHOT


从上面可以看到,我构建这个工程使用的是springboot 2.5.6版本,它集成的第三方依赖版本属性如下:

 
   5.16.3
   2.7.7
   1.9.91
   2.17.0
   1.9.7
   3.19.0
   4.0.6
   4.0.3
   3.2.0
   1.10.22
   2.9.2
   4.11.3
   1.5.1
   1.15
   2.8.0
   3.12.0
   1.6
   2.9.0
   3.1.7
   11.5.6.0
   1.0.11.RELEASE
   10.14.2.0
   4.1.26
   2.10.9.2
   3.9.7
   7.12.1
   3.0.0
   7.7.3
   2.3.31
   4.0.5
   3.0.4
   2.3.5
   3.0.9
   2.8.8
   1.4.200
   2.2
   4.1.5
   2.2.1
   5.4.32.Final
   6.2.0.Final
   4.0.3
   2.5.2
   2.49.1
   4.1.4
   4.5.13
   5.0.4
   4.4.14
   5.1.2
   12.1.7.Final
   2.21
   2.12.5
   1.2.2
   1.3.5
   2.0.3
   1.1.6
   1.0.2
   1.6.7
   1.1.4
   2.2.3
   4.0.4
   1.2.7
   1.3.3
   2.0.2
   1.1.2
   2.1.6
   2.3.3
   1.4.2
   2.3.3
   3.1.6
   1.2.0
   1.3.2
   1.1.1
   2.3.1
   2.3.1
   2.0.1
   1.1.4
   1.0
   1.6.2
   1.1
   2.2
   1.3
   2.0.1.Final
   1.1
   1.2.0
   4.0.4.java8
   3.4.2.Final
   7.6.1.Final
   2.0.6
   3.6.3
   2.33
   9.0.52
   2.2.0.v201112011158
   1.1.10
   9.4.44.v20210927
   1.15
   1.2.14
   1.6.2
   3.14.15
   2.5.0
   2.4.7
   1.5.0
   1.2
   1.3.1
   4.13.2
   5.7.2
   2.7.1
   1.5.31
   1.5.2
   6.1.5.RELEASE
   4.3.5
   2.14.1
   1.2.6
   1.18.22
   2.7.4
   1.8
   3.3.0
   3.1.0
   3.8.1
   3.1.2
   2.8.2
   3.0.0
   2.22.2
   3.2.0
   2.5.2
   3.2.2
   3.2.0
   3.2.0
   3.2.0
   3.2.4
   3.2.1
   2.22.2
   3.3.2
   1.7.5
   1.9.15
   3.9.0
   4.2.3
   9.2.1.jre8
   8.0.27
   1.9.22
   4.2.7
   4.1.69.Final
   2.0.44.Final
   9.9.1
   9.10.1
   19.3.0.0
   3.14.9
   21.1.0.0
   1.2.2
   42.2.24
   0.10.0
   2.3.2
   4.4.0
   Arabba-SR11
   5.12.0
   1.0.3
   2020.0.12
   4.3.3
   1.1.1
   1.3.8
   1.2.1
   2.2.21
   1.5.3
   3.141.59
   2.49.1
   4.7.6
   4.0.1
   1.7.32
   1.28
   8.8.2
   2.3.11
   4.3.3
   2021.0.6
   5.3.12
   1.3.5
   5.5.5
   2.7.8
   2.3.4.RELEASE
   2.0.5.RELEASE
   1.3.1
   5.5.3
   2021.0.3
   3.1.1
   3.34.0
   1.6.7
   3.0.12.RELEASE
   2.0.1
   3.0.4.RELEASE
   3.0.4.RELEASE
   2.5.3
   9.0.54
   4.0.14
   2.2.12.Final
   2.8.1
   3325375
   0.46
   1.6.3
   1.0.2
   2.8.3
 


(3)引入spring-boot-starter-web启动器

接下来,我们把项目改造成一个web工程!

以前我们创建web工程,需要引入很多依赖,有了springboot后我们只需要引入spring-boot-starter-web启动器

 
           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">
     4.0.0
 
     org.example
     maven-helloworld
     1.0-SNAPSHOT
 
     
         org.springframework.boot
         spring-boot-starter-parent
         2.5.6
         
     

     
     
         
             org.springframework.boot
             spring-boot-starter-web
         

     

 

引入web的starter依赖后,刷新一下maven,再次用mvn dependency:tree 查看依赖树:

 [INFO] org.example:maven-helloworld:jar:1.0-SNAPSHOT
 [INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.5.6:compile
 [INFO]    +- org.springframework.boot:spring-boot-starter:jar:2.5.6:compile
 [INFO]    |  +- org.springframework.boot:spring-boot:jar:2.5.6:compile
 [INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.5.6:compile
 [INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.5.6:compile
 [INFO]    |  |  +- ch.qos.logback:logback-classic:jar:1.2.6:compile
 [INFO]    |  |  |  +- ch.qos.logback:logback-core:jar:1.2.6:compile
 [INFO]    |  |  |  \- org.slf4j:slf4j-api:jar:1.7.32:compile
 [INFO]    |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile
 [INFO]    |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
 [INFO]    |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.32:compile
 [INFO]    |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
 [INFO]    |  +- org.springframework:spring-core:jar:5.3.12:compile
 [INFO]    |  |  \- org.springframework:spring-jcl:jar:5.3.12:compile
 [INFO]    |  \- org.yaml:snakeyaml:jar:1.28:compile
 [INFO]    +- org.springframework.boot:spring-boot-starter-json:jar:2.5.6:compile
 [INFO]    |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.5:compile
 [INFO]    |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.5:compile
 [INFO]    |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.12.5:compile
 [INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.12.5:compile
 [INFO]    |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.12.5:compile
 [INFO]    |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.12.5:compile
 [INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.5.6:compile
 [INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.54:compile
 [INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.54:compile
 [INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.54:compile
 [INFO]    +- org.springframework:spring-web:jar:5.3.12:compile
 [INFO]    |  \- org.springframework:spring-beans:jar:5.3.12:compile
 [INFO]    \- org.springframework:spring-webmvc:jar:5.3.12:compile
 [INFO]       +- org.springframework:spring-aop:jar:5.3.12:compile
 [INFO]       +- org.springframework:spring-context:jar:5.3.12:compile
 [INFO]       \- org.springframework:spring-expression:jar:5.3.12:compile

好家伙,仅仅是引入了一个web的starter,springboot就会默认为我们下载web工程所有依赖的jar,这正是springboot简化开发的第一步,所有开发场景都给你集成一个starter!


(4)新建启动类

 package com.huiwei.hello;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.ComponentScan;
 
 @EnableAutoConfiguration
 @ComponentScan
 //@SpringBootApplication //可以代替上面2个注解
 public class HelloWorldApplication {
     public static void main(String[] args) {
         SpringApplication.run(HelloWorldApplication.class,args);
     }
 }

注意:@SpringBootApplication注解包含了@EnableAutoConfiguration、@ComponentScan、@SpringBootConfiguration这3个注解,所以可以用这一个注解代替3个注解


(5)创建HelloController来进行测试

 package com.huiwei.hello.controller;
 
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 public class HelloController {
     @RequestMapping("hello")
     public String hello(){
         return "Hello,Spring!";
     }
 }


(6)启动项目,访问测试:http://localhost:8080/hello

(7)创建可执行 jar

要创建可执行 jar,我们需要将 spring-boot-maven-plugin 添加到 pom.xml 文件中。在 dependencies 下方插入以下配置:

 
         
             
                 org.springframework.boot
                 spring-boot-maven-plugin
             

         

     


引入依赖后,刷新maven,双击idea右侧的package,进行打包


生成maven-helloworld-1.0-SNAPSHOT.jar


在命令行执行java -jar maven-helloworld-1.0-SNAPSHOT.jar,这样就以jar包的方式启动了一个springboot工程



方式二:使用springboot脚手架创建项目


(1)选择左边的Spring Initializr  


(2)选择web的依赖,点击完成创建springboot脚手架


当我们经过以上2步创建一个springboot工程后,创建好的项目结构如下:


我们可以看见这种方式已经默认帮我们创建了项目的启动类

 package com.example.springboothello;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
 public class SpringbootHelloApplication {
     public static void main(String[] args) {
         SpringApplication.run(SpringbootHelloApplication.class, args);
     }
 }


此外还在resources资源目录下创建了

 static文件夹:用于存放静态资源文件
 templates文件夹:用于存放模板文件
 application.properties:用于修改项目的一些默认配置


我们打开pom文件如下:

 
           xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
     4.0.0
     
         org.springframework.boot
         spring-boot-starter-parent
         2.5.6
         
     

     com.example
     springboot-hello
     0.0.1-SNAPSHOT
     springboot-hello
     Demo project for Spring Boot
     
         1.8
     

     
         
             org.springframework.boot
             spring-boot-starter-web
         

 
         
             org.springframework.boot
             spring-boot-starter-test
             test
         

     

 
     
         
             
                 org.springframework.boot
                 spring-boot-maven-plugin
             

         

     

 


好家伙,已经为我们默认引入了parent依赖、web依赖以及一个test的依赖,还有项目进行打包的依赖,不用我们手动引入了,我们想要什么功能只需要在创建项目时勾选相应的模块即可!


(3)创建HelloController用于测试

 package com.example.springboothello.controller;
 
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 public class HelloController {
     @RequestMapping("hello")
     public String hello(){
         return "Hello,springboot-hello!";
     }
 }


(4)启动访问:http://localhost:8080/hello


二、SpringbootApplication启动类详解

Spring Boot 项目通常有一个名为 XXXApplication 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Java应用的入口方法。

 @SpringBootApplication
 public class SpringbootHelloApplication {
     public static void main(String[] args) {
         SpringApplication.run(SpringbootHelloApplication.class, args);
     }
 }

当我们启动这个类时,会运行SpringApplication的run方法,run方法传入了启动类SpringbootHelloApplication 的class对象,然后对启动类的注解@SpringBootApplication进行解析,我们来看看@SpringBootApplication的内部

 @SpringBootConfiguration
 @EnableAutoConfiguration
 @ComponentScan(
     excludeFilters = {@Filter(
     type = FilterType.CUSTOM,
     classes = {TypeExcludeFilter.class}
 ), @Filter(
     type = FilterType.CUSTOM,
     classes = {AutoConfigurationExcludeFilter.class}
 )}
 )
 public @interface SpringBootApplication {

可以看到,它由@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan三个注解组成

@SpringBootConfiguration是spring原生注解Configuration的springboot版本

@EnableAutoConfiguration让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置,例如,添加了 spring-boot-starter-web 依赖,会自动添加Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。

Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下


三、springboot配置文件

Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下。


Spring Boot 不仅支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具有面向对象的特征。


Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。


修改 properties 配置文件实例:

(1)打开 resources 下的 application.properties


(2)在这里我们可以设置访问的端口,将 Tomcat 默认端口设置为 8082 ,并将默认的访问路径从 “/” 修改为 “/springboot” 时,再访问 http://localhost:8080/hello是什么都没有的


此时要访问 hello 是要使用 http://localhost:8082/springboot/hello



(3)使用 yml 文件作为配置文件


重启项目进行访问:


(4)yml语法

  • 基本语法

k:(空格)v:表示一对键值对(空格必须有);

空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

此外,属性和值也是大小写敏感;

  • 值的写法

字面量:普通的值(数字,字符串,布尔)

k: v:字面直接来写;

 name: 小明
 age: 18

字符串默认不用加上单引号或者双引号;

"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思

 name:   "zhangsan \n lisi":输出;zhangsan 换行  lisi

'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据

 name:   ‘zhangsan \n lisi’:输出;zhangsan \n  lisi


  • 对象、Map(属性和值)(键值对)

k: v:在下一行来写对象的属性和值的关系;注意缩进

 friends:
     lastName: zhangsan
     age: 20

行内写法:

 friends: {lastName: zhangsan,age: 18}


  • 数组(List、Set)

用- 值表示数组中的一个元素

 pets:
  - cat
  - dog
  - pig

行内写法

 pets: [cat,dog,pig]


(5)@value获取配置文件属性

在application.yml中配置person,name为小明,年龄为18

 server:
   port: 8085
 
 person:
   name: 小明
   age: 18

在我们的HelloController中用@value来获取配置属性,

 @RestController
 public class HelloController {
 
     @Value("${person.name}")
     String name;
 
     @Value("${person.age}")
     Integer age;
 
     @RequestMapping("hello")
     public String hello(){
         return "Hello,"+name+",今年"+age+"岁";
     }
 }


重启项目,访问 http://localhost:8085/hello,结果如下:


(6)封装配置信息使用方法

@value尽管可以封装配置信息,但当相关连的配置信息太多时,我们可以对信息进行封装


我们构建一个Person对象,里面包含了字符串、数字、布尔、日期、map、数组、对象等各种类型的属性,正好可以对上面的yml语法进行验证

 String lastName
 Integer age
 Boolean boss
 Date birth
 Map maps
 List lists
 Dog dog


1)application.yml 文件内容如下:

 person:
   lastName: 小明
   age: 18
   boss: false
   birth: 2017/12/12
   maps: {k1: v1,k2: 12}
   lists:
     - lisi
     - zhaoliu
   dog:
     name: 小狗
     age: 12


2)新建 Person.java 和 HelloController.java ,内容如下:


Person.java:

 package com.example.springboothello.bean;
 
 import lombok.Data;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 @Component
 @Data
 @ConfigurationProperties(prefix = "person")
 public class Person {
 
     private String lastName;
     private Integer age;
     private Boolean boss;
     private Date birth;
 
     private Map maps;
     private List lists;
     private Dog dog;
 }


HelloController.java:

 package com.example.springboothello.controller;
 
 import com.example.springboothello.bean.Person;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
 @RestController
 public class HelloController {
 
     @Autowired
     Person person;
 
     @RequestMapping("hello")
     public String hello(){
         return person.toString();
     }
 }


3)访问 http://localhost:8080/hello 结果如下:


解释:我们可以把配置信息封装成一个类,首先在我们的属性前加一个 person前缀,然后新建一个 Person类用来封装这些信息,并用上两个注解:


@Component:表明当前类是一个 Java Bean

@ConfigurationProperties(prefix = “person”):表示获取前缀为person的配置信息


你可能感兴趣的:(springboot快速入门)