1)SpringBoot简介

Spring程序的缺点

  • 配置繁琐
  • 依赖设置繁琐

SpringBoot程序的优点

  • 自动配置
  • 起步依赖(简化依赖配置)
  • 辅助功能(内置服务器,)

一、起步依赖 

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 <groupId>com.example</groupId>
    <artifactId>springboot_01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_01</name>
    <description>springboot_01</description>
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 <dependencies>
</project>

starter
SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的

parent
所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的spring-boot-starter-parent(2.5.0)与spring-boot-starter-parent (2.4.6)共计57处坐标版本不同
实际开发
使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供如发生坐标错误,再指定version(要小心版本冲突)
 

SpringBoot在创建项目时,采用jar的打包方式
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目

 

 二、辅助功能 

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web< / artifactId>< !--web起步依赖环境中,排除Tomcat起步依赖-->
  <exclusions>
     <exclusion>
        <groupId>org.springframework.boot< / groupId>
        <artifactId>spring-boot-starter-tomcat< / artifactId>
    </exclusion>
   </exclusions>
</dependency>
    <! --添加etty起步依赖,版本由SpringBoot的starter控制--><dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
</dependencies>

Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty

三、基本配置

1、配置文件格式

修改服务器端口号:

  • application. properties
    server.port=80
  • application. yml

     server:

     port:81

  • application. yaml
    server:
     port: 82
     


 

第一种 

 重启之后

 第二种

第三种依照格式写好即可

 这三种配置文件优先级

 properties>yml>yaml

四、yaml

YAML ( YAML Ain't Markup Language) ,一种数据序列化格式

优点:

  • 容易阅读
  • 容易与脚本语言交互
  • 以数据为核心,重数据轻格式

YAML文件扩展名

.yml(主流)

.yaml

YAML语法规则

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注释

YAML数组数组

数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据
减号与数据间空格分隔


YAML数据读取方式




 

五、多环境开发 

(1)如何配置启用环境

#设置启用的环境
spring:
  profiles: test
(以上是启动程序时使用的环境,此时选择的是test)

(下面是具体的环境,根据需要设置多个环境方便选择)
---(此乃分隔符)
#设置开发的环境
spring:
  profiles: dev
server:
  port: 80
---
#设置生产的环境
spring:
  profiles: pro
server:
  port: 81

---
#设置测试的环境
spring:
  profiles: test
server:
  port: 82



使用properties做多环境开发???

 (2)多环境命令启动参数设置

 

带参数启动

java -jar springboot.jar --spring.profiles.active=test

首先需要clean一下,以免出现错误难找

 

然后需要打包的工程下进行package(之后会出现target)

在文件下找到该打包文件,进行cmd,然后输入上述命令即可

 (3)多环境开发兼容问题

该插件功能,在配置文件中可以使用pom文件的资源

 maven中如何设置多环境???

<profiles>
  <profile>
    <id>dev_env</id>
 <properties>
   <profile.active>dev</profile.active>
</properties>
 <activation>
    <activeByDefault>true</activeByDefault>
</activation>
 </profile>
 <profile>
 <id>pro_env</id>
 <properties>
   <profile.active>pro</profile.active>
 </properties>
  </profile>
  <profile>
   <id>test_env< /id>
<properties>
  <profile.active>test</profile.active>
</properties>
  </profile>
</profiles>

 springboot中使用maven属性

六、配置文件分类 

SpringBoot中4级配置文件
1级: file : config/ application.yml【最高】
2级: file : application.yml
3级: classpath: config/ application. yml

4级: classpath: application.yml【最低】
作用:
1级与2级留做系统打包后设置通用属性

3级与4级用于系统开发阶段设置通用属性
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这个人是谁呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值