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级用于系统开发阶段设置统用属性


相关文章
|
3月前
|
缓存 Java API
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
本文介绍了在Spring Boot中配置Swagger2的方法。通过创建一个配置类,添加`@Configuration`和`@EnableSwagger2`注解,使用Docket对象定义API文档的详细信息,包括标题、描述、版本和包路径等。配置完成后,访问`localhost:8080/swagger-ui.html`即可查看接口文档。文中还提示了可能因浏览器缓存导致的问题及解决方法。
120 0
微服务——SpringBoot使用归纳——Spring Boot集成 Swagger2 展现在线接口文档——Swagger2 的配置
|
3月前
|
Java 关系型数据库 数据库
微服务——SpringBoot使用归纳——Spring Boot事务配置管理——Spring Boot 事务配置
本文介绍了 Spring Boot 中的事务配置与使用方法。首先需要导入 MySQL 依赖,Spring Boot 会自动注入 `DataSourceTransactionManager`,无需额外配置即可通过 `@Transactional` 注解实现事务管理。接着通过创建一个用户插入功能的示例,展示了如何在 Service 层手动抛出异常以测试事务回滚机制。测试结果表明,数据库中未新增记录,证明事务已成功回滚。此过程简单高效,适合日常开发需求。
162 0
|
3月前
|
Java 测试技术 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
本课主要讲解Spring Boot项目中的属性配置方法。在实际开发中,测试与生产环境的配置往往不同,因此不应将配置信息硬编码在代码中,而应使用配置文件管理,如`application.yml`。例如,在微服务架构下,可通过配置文件设置调用其他服务的地址(如订单服务端口8002),并利用`@Value`注解在代码中读取这些配置值。这种方式使项目更灵活,便于后续修改和维护。
45 0
|
3月前
|
SQL Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— application.yml 中对日志的配置
在 Spring Boot 项目中,`application.yml` 文件用于配置日志。通过 `logging.config` 指定日志配置文件(如 `logback.xml`),实现日志详细设置。`logging.level` 可定义包的日志输出级别,例如将 `com.itcodai.course03.dao` 包设为 `trace` 级别,便于开发时查看 SQL 操作。日志级别从高到低为 ERROR、WARN、INFO、DEBUG,生产环境建议调整为较高级别以减少日志量。本课程采用 yml 格式,因其层次清晰,但需注意格式要求。
212 0
|
8天前
|
Java API 数据库
JPA简介:Spring Boot环境下的实践指南
上述内容仅是JPA在Spring Boot环境下使用的冰山一角,实际的实践中你会发现更深更广的应用。总而言之,只要掌握了JPA的规则,你就可以借助Spring Boot无比丰富的功能,娴熟地驾驶这台高性能的跑车,在属于你的程序世界里驰骋。
56 15
|
4月前
|
Cloud Native Java Nacos
springcloud/springboot集成NACOS 做注册和配置中心以及nacos源码分析
通过本文,我们详细介绍了如何在 Spring Cloud 和 Spring Boot 中集成 Nacos 进行服务注册和配置管理,并对 Nacos 的源码进行了初步分析。Nacos 作为一个强大的服务注册和配置管理平台,为微服务架构提供
958 14
|
3月前
|
Java 数据库连接 数据库
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——MyBatis 介绍和配置
本文介绍了Spring Boot集成MyBatis的方法,重点讲解基于注解的方式。首先简述MyBatis作为持久层框架的特点,接着说明集成时的依赖导入,包括`mybatis-spring-boot-starter`和MySQL连接器。随后详细展示了`properties.yml`配置文件的内容,涵盖数据库连接、驼峰命名规范及Mapper文件路径等关键设置,帮助开发者快速上手Spring Boot与MyBatis的整合开发。
173 0
|
3月前
|
缓存 Java 应用服务中间件
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——依赖导入和Thymeleaf相关配置
在Spring Boot中使用Thymeleaf模板,需引入依赖`spring-boot-starter-thymeleaf`,并在HTML页面标签中声明`xmlns:th=&quot;http://www.thymeleaf.org&quot;`。此外,Thymeleaf默认开启页面缓存,开发时建议关闭缓存以实时查看更新效果,配置方式为`spring.thymeleaf.cache: false`。这可避免因缓存导致页面未及时刷新的问题。
80 0
|
3月前
|
Java 数据库 微服务
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——指定项目配置文件
在实际项目中,开发环境和生产环境的配置往往不同。为简化配置切换,可通过创建 `application-dev.yml` 和 `application-pro.yml` 分别管理开发与生产环境配置,如设置不同端口(8001/8002)。在 `application.yml` 中使用 `spring.profiles.active` 指定加载的配置文件,实现环境快速切换。本节还介绍了通过配置类读取参数的方法,适用于微服务场景,提升代码可维护性。课程源码可从 [Gitee](https://gitee.com/eson15/springboot_study) 下载。
86 0
|
3月前
|
Java 微服务 Spring
微服务——SpringBoot使用归纳——Spring Boot中的项目属性配置——少量配置信息的情形
在微服务架构中,随着业务复杂度增加,项目可能需要调用多个微服务。为避免使用`@Value`注解逐一引入配置的繁琐,可通过定义配置类(如`MicroServiceUrl`)并结合`@ConfigurationProperties`注解实现批量管理。此方法需在配置文件中设置微服务地址(如订单、用户、购物车服务),并通过`@Component`将配置类纳入Spring容器。最后,在Controller中通过`@Resource`注入配置类即可便捷使用,提升代码可维护性。
54 0