SpringBoot概述&SpringBoot基础配置&yml的使用&多环境启动

简介: SpringBoot概述&SpringBoot基础配置&yml的使用&多环境启动

一、 SpringBoot概述

1.1 起步依赖

<?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.14</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot_quick_start</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_quick_start</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
  • 根据spring-boot-starter-web可以得到它对应的配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.7.14</version>
  </parent>
  <artifactId>spring-boot-starter-parent</artifactId>
  <packaging>pom</packaging>
</project>
  • 这个配置类又继承了dependencies
  1. 各种properties信息:
<properties>
     <activemq.version>5.16.6</activemq.version>
 ...
</properties>
  • 2.各种依赖管理:
<dependencies>
<dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-amqp</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-blueprint</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      ...
</dependencies>
  • 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(要小心版本冲突)

1.2 辅助功能

  • 内置服务器,根据spring-boot-starter-web依赖,可以内置一个tomcat服务器
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 内置了tomcat服务器
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <version>2.7.14</version>
      <scope>compile</scope>
</dependency>

1.3 SpringBoot程序启动

  • 启动方式
@SpringBootApplication
public class SpringbootQuickStartApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootQuickStartApplication.class, args);
    }
}
  • SpringBoot在创建项目时,采用jar的打包方式
  • SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
  • 将tomcat服务器换为jetty服务器:
  • 在原有的服务器启动类下使用exclusion排除掉Tomcat依赖:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions>
         <exclusion>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
         </exclusion>
     </exclusions>
</dependency>
  • 添加Jetty服务器的依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
  • 启动SpringBoot项目


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

二、 SpringBoot基础配置

2.1 配置文件格式

  • SpringBoot提供了多种属性配置方式(选用不同的配置文件)
  • 配置文件名必须是application开头的,否则可能不生效
  • application.properties
server.port=80
  • application.yml
server:
  • application.yaml
server:
  port: 82

注意:port:后一定要加空格

  • 当这三个配置文件都配置时,默认使用顺序是application.properties优先级最高,其次是application.yml,最低优先级是application.yaml,一般写项目时用的配置文件是application.yml

2.1.1 自动提示功能消失解决方案

在使用以.yml.yaml为后缀名的配置文件时,可能会出现自动提示功能消失的问题

解决步骤如下:





2.2 yaml

  • YAML (YAML Ain’t Markup Language),一种数据序列化格式
  • 优点:
  • 容易阅读
  • 容易与脚本语言交互
  • 以数据为核心,重数据轻格式
  • YAML文件扩展名
  • .yml(主流)
  • .yaml

2.2.1 yaml语法规则

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

2.2.2 yaml数据读取方式

2.2.2.1 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名…}
lesson: SpringBoot
server:
  port: 80
books:
  name:
  subject:
    - Java
    - 操作系统
    - 网络
@RestController
@RequestMapping("/books")
public class BookController {
    @Value("${lesson}")
    private String lesson;
    @Value("${books.subject[0]}")
    private String subject_0;
}
2.2.2.2 封装全部数据到Environment对象
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private Environment environment;
    @Autowired
    private Books books;
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println(lesson);
        System.out.println(subject_0);
        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("books"));
        System.out.println(books);
        return "hello,spring boot!";
    }
}
2.2.2.3 自定义对象封装指定数据
@Component
@ConfigurationProperties(prefix = "books")
public class Books {
    private String name;
    private String[] subject[];
}
2.2.2.4 自定义对象封装指定数据警告解决方案

在使用自定义对象封装指定数据时,可能会遇到警告信息:



  • 在pom.xml文件中添加如下依赖即可:
<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <optional>true</optional>
</dependency>

2.3 多环境启动

2.3.1 yml文件多环境启动方式

# 设置启用的环境
spring:
  profiles:
    active: pro
---
# 开发
spring:
  profiles: dev
server:
  port: 80
---
# 生产
spring:
  profiles: pro
server:
  port: 81
---
# 测试
spring:
  profiles: test
server:
  port: 82

2.3.2 application.properties文件多环境启动

  • 主启动配置文件application.properties
  • spring.profiles.active=dev
  • 环境分类配置文件application-dev.properties
  • server.port=8080
  • 环境分类配置文件application-pro.properties
  • server.port=8081
  • 环境分类配置文件application-test.properties
  • server.port=8082

2.3.3 多环境命令行启动参数设置

  • 带参数启动SpringBoot
java -jar springboot.jar --spring.profiles.active=test
  • 可通过命令行修改参数
  • 修改端口:
java -jar springboot.jar --spring.profiles.active=test --server.port=88


2.3.3 多环境开发兼容问题(Maven与boot)

  • Maven中设置多环境属性
<!--开发环境-->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
            <prfile.active>dev</prfile.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>pro</id>
            <properties>
                <prfile.active>pro</prfile.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <prfile.active>test</prfile.active>
            </properties>
        </profile>
    </profiles>
  • SpringBoot中引用Maven属性
# 设置启用的环境
spring:
  profiles:
    active: ${prfile.active}
---
# 开发
spring:
  profiles: dev
server:
  port: 80
---
# 生产
spring:
  profiles: pro
server:
  port: 81
---
# 测试
spring:
  profiles: test
server:
  port: 82
  • Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
  • 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
<build>
      <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.4配置文件分类

  • SpringBoot中4级配置文件
  • 1级:file:config/appication.yml(最高)
  • 2级:file:application.yml
  • 3级:classpath:config/application.yml
  • 4级:classpath:application.yml (最低)
  • 作用:
  • 1级与2级留做系统打包后设置通用属性
  • 3级与4级用于系统开发阶段设置统用属性


相关文章
|
1月前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
26 1
|
1天前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的环境保护生活App的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的环境保护生活App的详细设计和实现(源码+lw+部署文档+讲解等)
3 0
|
3天前
|
消息中间件 设计模式 Java
SpringBoot+Schedule 定时任务的配置开关
SpringBoot+Schedule 定时任务的配置开关
11 0
SpringBoot+Schedule 定时任务的配置开关
|
6天前
|
Java Serverless 应用服务中间件
Serverless 应用引擎产品使用合集之Web函数启动的Spring Boot项目可以通过什么方式配置Nginx
阿里云Serverless 应用引擎(SAE)提供了完整的微服务应用生命周期管理能力,包括应用部署、服务治理、开发运维、资源管理等功能,并通过扩展功能支持多环境管理、API Gateway、事件驱动等高级应用场景,帮助企业快速构建、部署、运维和扩展微服务架构,实现Serverless化的应用部署与运维模式。以下是对SAE产品使用合集的概述,包括应用管理、服务治理、开发运维、资源管理等方面。
|
19天前
|
JSON JavaScript Java
SpringBoot读取配置优先级顺序是什么?
Spring Boot的外部配置加载优先级是开发者理解和管理应用程序配置的关键。它支持多种配置源,包括Java属性文件、YAML文件、环境变量、命令行参数等。配置加载顺序从低到高为:默认属性、@PropertySource加载的配置、Config Data(内部配置文件、外部配置文件)、环境变量、系统属性、Servlet容器初始化参数、SPRING_APPLICATION_JSON格式的环境变量或系统属性以及命令行参数。了解这一顺序有助于在不同环境中灵活配置和管理Spring Boot应用,确保其按预期运行。
|
21天前
|
Java 数据库连接 网络安全
springboot使用Pivotal Greenplum JDBC如何进行配置
【5月更文挑战第23天】springboot使用Pivotal Greenplum JDBC如何进行配置
40 6
|
24天前
|
Java Python Spring
小唐开始学 Spring Boot——(2)Spring Boot核心配置与注解
小唐开始学 Spring Boot——(2)Spring Boot核心配置与注解
QGS
|
1月前
|
JSON Java 关系型数据库
手拉手Springboot获取yml配置文件信息
手拉手Springboot获取yml配置文件信息
QGS
45 1
|
1月前
|
XML Java 数据格式
SpringBoot中yml与properties配置文件及bean取值赋值
SpringBoot中yml与properties配置文件及bean取值赋值
181 0