www.teandq.com
晓安科普

springboot_springboot框架

2024-06-01Aix XinLe

SSM是Spring的产品,主要用来简化开发,但我们现在所介绍的这款框架——SpringBoot,却是用来简化Spring开发的框架。

springboot_springboot框架

 

在之前的文章中我们已经学习了SSM的全部内容以及相关整合SSM是Spring的产品,主要用来简化开发,但我们现在所介绍的这款框架——SpringBoot,却是用来简化Spring开发的框架SpringBoot是由Pivowtal团队提供的全新框架,其设计目的就是用来简化Spring应用的初始搭建以及开发过程,用来简化开发工具的工具,你是否已经满怀期待~

温馨提醒:在学习前请学习SSM内容以及Maven的高阶内容(依赖传递)等内容SpringBoot简介SpringBoot是由Pivotal团队提供的全新框架,其设计目的就是用来简化Spring应用的初始搭建以及开发过程

SpringBoot概述SpringBoot是针对Spring的繁琐过程进行优化而产生的框架Spring程序缺点:配置繁琐依赖设置繁琐SpringBoot程序优点:自动配置起步依赖(简化依赖配置)辅助功能(内置服务器等)

SpringBoot项目开发我们通过一个简单的SpringBoot案例和SSM案例的比较来展现SpringBoot的优势SSM框架构造首先我们回忆一下SSM框架的基本构造图:

我们来总结一些SSM框架必备的一些文档:pom.xml配置文档ServletConfig配置Java类SpringMvcConfig配置Java类Collector服务层Java文档SpringBoot框架构造

相对而言,我们的SpringBoot将SSM的框架内容隐藏起来,达到简化框架的作用我们下面来介绍创建一个SpringBoot框架的具体步骤:IDEA创建新项目,选择SpringBoot框架,JDK选择1.8版本(Default默认在网页下载,需要联网)

选择Maven,Java,jar等相关选项,注意选择Java8(目前SpringBoot只支持Java8的版本)

选择Web中的SpringWeb,确保右侧存在Spring Web选项(上方可选择SpringBoot版本)

创建项目即可

删除无关项目,只保留src和pom.xml即可

我们仅需书写一个Collector相关类即可package com.itheima.controller; import org.springframework.web.bind.annotation

.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.

annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController

@RequestMapping("/books")publicclassBookController{ @GetMapping("/{id}")public String getById(@PathVariable

Integer id){ System.out.println("id ==> "+id); return"hello , spring boot!"; } }

点击启动Application.java文件即可(由系统自动创建)package com.itheima; import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplicationpublicclass

Application{ publicstaticvoidmain(String[] args){ SpringApplication.run(Application.class

, args); } } 以上至此,我们的SpringBoot项目就开发完毕了除此之外,我们的SpringBoot的核心内容实际上存在于pom.xml中,我们会在下述内容中进行介绍

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

modelVersion>4.0.0org.springframework.bootspring-boot-starter-parent

2.5.0com.itheimaspringboot_01_quickstart

0.0.1-SNAPSHOT1.8

>org.springframework.bootspring-boot-starter-web

org.springframework.bootspring-boot-starter-test

testorg.springframework.boot

spring-boot-maven-plugin我们会发现需要我们书写代码的部分仅仅只有Collector这一部分,相比于SSM框架简化了并非一点点

SSM框架与SpringBoot框架比较我们将SSM框架与SpringBoot框架进行简单的对比:类/配置文件SpringSpringBootpom文件中的坐标手工添加勾选添加web3.0配置类手工添加

无Spring/SpringMvc配置类手工添加无控制器手工添加手工添加我们可以明显比较出两者的显著差距!注意:基于IDEA开发的SpringBoot框架需要联网到SpringBoot官网加载程序框架结构

非IDEA进行SpringBoot开发我们在实际工作中,可能使用的开发工具并非只有IDEA那么IDEA中存在有SpringBoot的开发架构,其他不包含SpringBoot开发架构选项的软件就无法开发了吗?

我们可以选择到官网进行jar包下载直接导入开发即可:打开官网(官网地址:Spring Boot)

拉至页面底部,找到快速开发标志,点击进入创建界面

勾选相对应图标,点击创建即可

创建后会自动下载jar包,直接导入所用软件即可

SpringBoot快速启动我们在实际开发中,常常会做到前后端分离开发那么我们的SpringBoot中所使用的服务器或开发软件等是否还需要交付给前端呢SpringBoot为我们提供了一种全新的服务器开启方法,我们只需要将SpringBoot打包后交付给前端,前端就可直接进行开启

项目打包

打包后在当前页面采用cmd命令行输入以下指令即可直接开启服务器(注意需要在该jar包的文件夹目录下)java-jarSpringBoot文件包名.jar(可tab键补全) 注意点:我们需要将所需的数据库信息交付给前端,因为SpringBoot只负责项目的开启,与数据库无关

该方法是由一种pom.xml中的插件支持的,请确保存在该插件(SpringBoot自动创建) org.springframework.boot spring-boot-maven-plugin

SpringBoot起步依赖在简单介绍SpringBoot的项目开发之后,你是否有疑惑为什么SpringBoot能够省略如此多的信息来直接开发其实这一切都是源于SpringBoot的依赖的直接创建,我们称之为起步依赖:

parent起步依赖继承starter起步依赖继承我们给出部分pom.xml配置文件内部进行分析:

"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 https://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0

org.springframework.boot

spring-boot-starter-parent2.5.0

>com.itheimaspringboot_01_quickstart0.0.1-SNAPSHOT

>1.8

org.springframework.bootspring-boot-starter-web

org.springframework.boot

spring-boot-starter-testtest

>org.springframework.bootspring-boot-maven-plugin

总而言之,SpringBoot创建时自带的一系列起步依赖帮助我们简化了大量SSM的繁琐操作我们再来详细介绍几个词语:

Starter:SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的Parent:所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,并非依赖),以达到减少冲突的目的

实际开发:使用任意坐标时,仅书写GAV中的G和A,不需要书写V如若发生坐标错误,再指定Version(小心版本冲突)SpringBoot程序启动SpringBoot程序启动方法就是开启Application.java文件即可

package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublicclassApplication{ publicstaticvoidmain(String[] args){ SpringApplication.run(Application

.class, args); } } 我们给出两个注意点:SpringBoot在创建项目时,采用jar的打包方式SpringBoot的引导类是项目的入口,运行main方法就可以启动项目SpringBoot切换服务器

我们最后给出一个Maven使用技巧来切换服务器SpringBoot中默认使用Tomcat服务器并安装了对应插件,那么我们如果想切换服务器,只需要排除掉Tomcat插件,并添加新的插件即可

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

modelVersion>4.0.0org.springframework.bootspring-boot-starter-parent

2.5.0com.itheimaspringboot_01_quickstart

0.0.1-SNAPSHOT1.8

>org.springframework.boot

>spring-boot-starter-weborg.springframework.boot

>spring-boot-starter-tomcat

org.springframework.bootspring-boot-starter-jetty

>org.springframework.bootspring-boot-starter-test

testorg.springframework.boot

spring-boot-maven-pluginSpringBoot基础配置

我们在Spring中能够实现的技术,在SpringBoot中同样可以实现接下来我们依次来介绍一些SpringBoot基本配置的方法和多环境开发的问题SpringBoot配置格式SpringBoot为我们提供了三种配置格式来管理SpringBoot的配置(注意:以下配置均存在于resources文件夹中):

application.properties# 修改服务器端口号为80server.port=80application.yml (主流)# 修改服务器端口号为81(注意:存在空格)server:port:

81application.yaml# 修改服务器端口号为82(注意:存在空格)server:port:82当三者均存在时,其优先级为:application.properties>application.yml >application.yaml

以上三种配置格式均在resources文件夹下创建相对应名称以及后缀的文件下书写:

注意:application.properties属于SpringBoot自带,不需要创建application.yml,application.yaml需要自我创建,因而不被标记为配置文件如果我们希望该文件被标记为配置文件并包含有补全功能,我们需要手动设置为配置文件

yaml文件详细介绍我们在这里详细介绍一下yaml文件:YAML,一种数据序列化格式优点:容易阅读容易与脚本语言交互以数据为核心,重数据轻格式YAML文件扩展名:.yml(主流).yamlYAML语法规则:

大小写敏感属性层级关系使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不能使用tab)属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)# 表示注释使用 - 来表示数据开始符号(数组)

YAML语法使用规范示例:server:port:82logging:level:root:infolikes:-music-game-PEYAML的数据读取方法:首先我们先给出我们在yml文件中所列出的属性:

lesson: SpringBoot server:port:80enterprise:name:itcastage:16tel:4006184000subject:-Java-前端-大数据下面我们来介绍yaml数据读取的三种方法:

${属性名},${属性名.属性名},${属性名.属性名[数组下标]}package com.itheima.controller; import com.itheima.domain.Enterprise;

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.

annotation.Value; import org.springframework.core.env.Environment; import org.springframework.web.bind.

annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.

annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController

@RequestMapping("/books")publicclassBookController{ //使用@Value读取单一属性数据@Value("${lesson}")private String lesson;

@Value("${server.port}")private Integer port; @Value("${enterprise.subject[0]}")private String subject_00;

@GetMapping("/{id}")public String getById(@PathVariable Integer id){ System.out.println(lesson); System.

out.println(port); System.out.println(subject_00); return"hello , spring boot!"; } }

Environment对象匹配方法package com.itheima.controller; import com.itheima.domain.Enterprise; import org.springframework.beans.factory.

annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment;

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation

.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.

annotation.RestController; @RestController@RequestMapping("/books")publicclassBookController{ //使用Environment封装全配置数据(自动装配封装Environment,里面会包含yaml中所有属性和属性值)

@Autowiredprivate Environment environment; @GetMapping("/{id}")public String getById(@PathVariable

Integer id){ // 我们采用environment的getProperty方法,根据属性名,获得属性值 System.out.println(environment.getProperty(

"lesson")); System.out.println(environment.getProperty("server.port")); System.out.println(environment.getProperty(

"enterprise.age")); System.out.println(environment.getProperty("enterprise.subject[1]"));

return"hello , spring boot!"; } } 自定义对象封装指定数据// 自定义对象Enterprise实现类(属于Domain)package com.itheima.domain;

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;

import java.util.Arrays; //封装yaml对象格式数据必须先声明当前实体类受Spring管控@Component//使用@ConfigurationProperties注解定义当前实体类读取配置属性信息,通过prefix属性设置读取哪个数据

@ConfigurationProperties(prefix = "enterprise") publicclassEnterprise{ private String name; private

Integer age; private String tel; private String[] subject; @Overridepublic String toString

(){ return"Enterprise{" + "name=" + name + \ + ", age=" + age +

", tel=" + tel + \ + ", subject=" + Arrays.toString(subject) + }; }

public String getName(){ return name; } publicvoidsetName(String name){ this

.name = name; } public Integer getAge(){ return age; } publicvoidsetAge(Integer age)

{ this.age = age; } public String getTel(){ return tel; } publicvoid

setTel(String tel){ this.tel = tel; } public String[] getSubject() { return subject; }

publicvoidsetSubject(String[] subject){ this.subject = subject; } } // 服务层Controllerpackage

com.itheima.controller; import com.itheima.domain.Enterprise; import org.springframework.beans.factory.

annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment;

import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation

.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.

annotation.RestController; @RestController@RequestMapping("/books")publicclassBookController{ // 自动装配实现类即可

@Autowiredprivate Enterprise enterprise; @GetMapping("/{id}")public String getById(@PathVariable

Integer id){ System.out.println(enterprise); return"hello , spring boot!"; } }

org.springframework.bootspring-boot-configuration-processor

trueSpringBoot多环境启动我们在开发过程中可能会采用不同的环境,频繁的转换环境当然不是一个好办法

SpringBoot选择配置多环境来控制环境选择启动我们从两种不同的配置文件方向来讲解多环境:yaml多环境启动:# yaml采用 --- 来表示环境层级更换 # yaml采用 spring:profiles:active: 环境id 设置启用的环境

spring:profiles:active:dev---#开发环境#yaml采用 spring:config:activate:on-profile: 环境id 来定义当前环境id(规范写法)spring:

config:activate:on-profile:dev#以下属于环境配置server:port:80---#生产#yaml采用 spring:profiles: 环境id 来定义当前环境id(旧版写法,同样适用)

spring:profiles:pro#以下属于环境配置 server:port:81---#测试#yaml采用 spring:profiles: 环境id 来定义当前环境id(旧版写法,同样适用)spring:

profiles:test#以下属于环境配置 server:port:82---properties多环境启动:# application.properties文件(环境主文件)#设置启用的环境spring.profiles.active

=pro # application-dev.properties文件(环境配置文件)# 设置相关资源配置server.port=8080# application-pro.properties文件(环境配置文件)

# 设置相关资源配置server.port=8081# application-test.properties文件(环境配置文件)# 设置相关资源配置server.port=8082SpringBoot前端多环境启动

我们前面提及过SpringBoot的快速启动直接将jar包打包后发给前端就可以采用命令行启动服务器但是我们的配置可能会导致更多的细节问题:当我们的yaml出现中文注释时,需要将IDEA的encoding均设置为UTF-8

当我们的前端需要不同的环境配置时,我们不能在后台手动设置默认环境,因而需要采用指令设置前端在调用时,可以采用指令来更改默认环境默认开启服务器java-jar jar包名称.jar更换默认条件开启服务器样板

java-jar jar包名称.jar --配置属性=配置值更换默认环境开启服务器java-jar jar包名称.jar --spring.profiles.active=test更换默认端口号开启服务器

java-jar jar包名称.jar --server.port=88更换条件可以叠加使用java-jar jar包名称.jar --spring.profiles.active=test --server.port=88

SpringBoot多环境兼容问题SpringBoot中存在有很多的环境设置,不仅如此,包括有Maven也存在有多环境配置那么Maven的多环境配置优先级和SpringBoot的多环境配置优先级谁的更高呢?

我们的package操作是由Maven来完成的多环境优先级:Maven > SpringBoot我们通过一个简单的案例来证明:Maven中配置多环境属性

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

modelVersion>4.0.0org.springframework.bootspring-boot-starter-parent

2.5.0com.itheimaspringboot_05_maven_and_boot_profile

0.0.1-SNAPSHOT1.8

>org.springframework.bootspring-boot-starter-web

org.springframework.bootspring-boot-starter-test

testorg.springframework.boot

spring-boot-maven-plugin

>org.apache.maven.pluginsmaven-resources-plugin3.2.0

UTF-8true

免责声明:本站所有信息均搜集自互联网,并不代表本站观点,本站不对其真实合法性负责。如有信息侵犯了您的权益,请告知,本站将立刻处理。联系QQ:1640731186

知识springboot_springboot框架

2024-06-01Aix XinLe172

springboot_springboot框架SSM是Spring的产品,主要用来简化开发,但我们现在所介绍的这款框架——SpringBoot,却是用来简化Spring开发的框架。…

历史道长宝典系列之道家性经_道长宝典2

2024-06-01Aix XinLe121

道长宝典系列之道家性经_道长宝典2《笑傲江湖》中的《葵花宝典》一书,根据冲虚道长对令狐冲的说法,是「前朝皇宫中一位宦官所著」,根据《笑傲江湖》已经出现了元末明初才开始的武当派,而…

历史蜜糖娇妻送上门_蜜糖婚宠小说免费阅读

2024-06-01Aix XinLe12

蜜糖娇妻送上门_蜜糖婚宠小说免费阅读李毅叼着烟卷低着头站在小区门口,果然没多久韩峰的那辆别克商务就到了,跟保安打了招呼,李毅上车,指引着让司机把车开到了楼下。“我想以你的身体状况,…

知识耶伦_耶伦来华消息

2024-06-01Aix XinLe36

耶伦_耶伦来华消息继美国国务卿布林肯之后,美国财政部长耶伦成为近期第二位访问中国的美国内阁高官。经中美双方商定,耶伦于7月6日至9日访华。…

历史洛洛历险记全集第二部_洛洛历险记第2部

2024-06-01Aix XinLe125

洛洛历险记全集第二部_洛洛历险记第2部在蓝弧动画的发展史上,洛洛历险记可以说是蓝弧最关键的一部动画,因为这部动画,蓝弧在动画圈子里也算是小有名气。…