Chiriri's blog Chiriri's blog
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)

Iekr

苦逼后端开发
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)
  • JavaSE

  • JavaEE

    • MyBatis
    • Jackson
    • Jedis
    • Maven
    • POI
    • Spring
    • Spring MVC
    • Maven 高级
    • Dubbo
    • Zookeeper
    • Spring Security
    • Spring Boot
      • 起步依赖原来分析
      • SpringBoot配置
        • YAML
        • 数据格式
        • 读取配置文件内容
        • profile
        • 配置文件切换
        • properties多文件配置
        • yml单文件配置
        • profile激活方式
        • 内部配置加载顺序
        • 外部配置加载顺序
      • SpringBoot整合其他框架
        • Junit
        • Redis
        • MyBatis
    • Spring Boot 高级
    • RabbitMQ
    • RabbitMQ 高级
    • Spring Cloud
    • Docker
    • ElasticSearch
    • ElasticSearch 高级
  • Linux

  • MySQL

  • NoSQL

  • Python

  • Python模块

  • 机器学习

  • 设计模式

  • 传智健康

  • 畅购商城

  • 博客项目

  • JVM

  • JUC

  • Golang

  • Kubernetes

  • 硅谷课堂

  • C

  • 源码

  • 神领物流

  • RocketMQ

  • 短链平台

  • 后端
  • JavaEE
Iekr
2021-09-24
目录

Spring Boot

# Spring Boot

  1. 自动配置 Spring Boot 的自动配置是一个运行时的过程,考虑了众多因素,Spring 配置应该用哪个,不该用哪个。该过程是 SpringBoot 自动完成的
  2. 起步依赖 起步依赖本质上是一个 Maven 项目对象模型,定义了对其他库的传递依赖,简单来说,就是将具备某种功能的坐标打包到一起,并提供一些默认的功能
  3. 辅助功能 提供了一些大型项目中常见的非功能性特征,如嵌入式服务器 安全 指标 健康检测 外部配置等

Spring Boot 并不是对 Spring 功能上的增强 而是提供了一种快速使用 Spring 的方式

# 起步依赖原来分析

在 Spring-boot-starter-parent 中定义了各种技术的版本信息,组合了一套最优搭配的技术版本

在各种 starter 中,定义了完成该功能需要的坐标合集,其中大部分版本信息来自于父工程

我们的工程继承 parent, 引入 starter 后,通过依赖传递,就可以简单方便获取需要的 jar 包,并不会存在版本冲突等问题

# SpringBoot 配置

SpringBoot 是基于约定的,所有很多配置都有默认值,但如果想替换默认值,必须使用 application.properties 或者 application.yml 或 application.yaml/yam 进行配置

  • properties 以键值对方式

    server.port=8080
    
    1
  • xml

    <server>
        <port>8080</port>
    </server>
    
    1
    2
    3
  • yml/yaml

    server:
    	port: 8080
    
    1
    2

如果项目中存在多个 application 配置文件 会根据文件类型按顺序加载 先加载的无法被覆盖

properties > yml > yaml

# YAML

YAML 文件是以数据为核心,比传统的 xml 方式更加简洁

https://toyaml.com/index.html 在线转换

  • 大小写敏感
  • 数据值前边必须有空格,作为分隔符
  • 使用缩进表示层级关系
  • 缩进时不允许使用 Tab 键,只允许使用空格 (各个系统 Tab 对应的 空格数目可能不同,导致层次混乱)
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • #表示注释,从这个字符一直到行尾,都会被解析忽略

# 数据格式

  • 对象 (map): 键值对的集合

    person:
    	name: zhangsan
    #行内写法
    person: {name: zhangsan}
    
    1
    2
    3
    4
  • 数组:一组按次序排列的值

    address:
    	- beijing
    	- shanghai
    #行内写法
    address: [beijing,shanghai]
    
    1
    2
    3
    4
    5
  • 纯量:单个的 不可再分的值

    msg1: 'hello \n world' # 单引忽略转义字符
    msg2: "hello \n world" # 双引识别转义字符
    
    1
    2

    字符串不用加单引号或者双引号,双引号是用来转义

  • 参数引用

    name: lisi
    person:
    	name: ${name}  #引用上边定义的name值
    
    1
    2
    3

# 读取配置文件内容

  1. @Value

    @Value("${name}")
        private String name;
    @Value("${test.hello:test}")   //防止忘记配置 可以提供默认值 在变量名后加上:
    private String testHello;  
    
    1
    2
    3
    4
  2. Environment 是一类 可以注入 使用内置的 getProperty 获取指定键的值

     @Autowired
     private Environment environment;
    
    @RequestMapping("/hello2")
        public void hello2() {
            //通过getProperty 方法获取指定键的值
             System.out.println(environment.getProperty("address[0]"));
        }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  3. @ConfigurationProperties 在自定义类映射为指定键的成员属性 需要 gei 和 set 方法

    @Component
    @ConfigurationProperties(prefix = "person")  //prefix为键
    public class Person {
        //需要提供get和set方法
        private String name;
        private int age;
    
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    @Autowired
    private Person person; //注入ConfigurationProperties的类才可以使用
    
    1
    2

    配置文件中根据 @ConfigurationProperties 注解 标识的类 提示对应的成员属性 坐标

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
    
    1
    2
    3
    4

    image-20210924221146454

# profile

profile 功能是来进行动态配置切换 可以帮助我们快速的切换 开发 测试 生产 环境配置

# 配置文件切换

# properties 多文件配置

通过 application 配置

spring.profiles.active=dev  #dev为application-dev  -后面的名称
1

不同的 application 以 - 进行区分 如:application-dev application-test

在 spring.profiles.active= 横杠后的名称 调用指定的环境配置

image-20210924222221150

# yml 单文件配置

以三个横杠区分不同的环境 ---

---
server:
  port: 8081
spring:
  config:
    activate:
      on-profile: dev

---
server:
  port: 8082
spring:
  config:
    activate:
      on-profile: pro

---

server:
  port: 8083
spring:
  config:
    activate:
      on-profile: test  #配置名称为test环境
---

spring:
  profiles:
    active: pro  #使用pro环境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# profile 激活方式

  • 在虚拟机中配置 以 - Dsrping.profiles:active = 对应的环境名称

image-20210924222950280

  • 通过 jar 包运行
java -jar springboot.jar --spring.profiles.active=pro
1

# 内部配置加载顺序

  1. file:./config/ 当前项目下的 /config 目录下
  2. file:./ 当前项目的根目录
  3. classpath:/config/ classpath 的 /config 目录
  4. classpath:/ : classpath 的根目录 resource 为此处

优先使用先加载配置中的属性

# 外部配置加载顺序

  1. 通过 --spring.config.location= 来指定外部配置文件的路径
java -jar springboot.jar --sporing.config.location=配置路径
1
  1. 在 jar 包的同级文件下放置配置文件 会优先于 jar 中的配置文件

    java -jar springboot.jar
    
    1
  2. 在 jar 包的同级文件下创建 config 文件夹放置配置文件 会优先于 jar 中的配置文件

# SpringBoot 整合其他框架

# Junit

//如果test类跟springboot启动类的包路径一致 则不需要指定classes
@SpringBootTest(classes = SpringbootProfilesApplication.class)
class SpringbootProfilesApplicationTests {

    @Autowired
    private UserService userService;

    @Test
    void contextLoads() {
        userService.add();
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13

# Redis

创建 maven 时选择 redis

image-20210924230135765

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
1
2
3
4

test 类

    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    public void testSet(){
        redisTemplate.boundValueOps("name").set("zhangsang");
    }

    @Test
    public void testGet(){
        Object name = redisTemplate.boundValueOps("name").get();
        System.out.println(name);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

application 配置默认是为本机地址和 6379 端口

spring:
    redis:
       host: 127.0.0.1  #redisip
       port: 6379  #端口
1
2
3
4

# MyBatis

image-20210924235106698

创建项目时勾选

   <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
1
2
3
4
5
6
7
8
9

配置类

spring:
	datasource:
    	driver-class-name: com.mysql.cj.jdbc.Driver  #注意驱动地址
    	username: root
    	password: 123456
    	url: jdbc:mysql:///springboot?serverTimezone=UTC  #如果是本地可以忽略ip和端口  必须设置时区否则会报错
1
2
3
4
5
6
  1. 注解版 mapper
@Repository
@Mapper
public interface UserMapper {
    @Select("select * from t_user")
    List<User>  findAll();
}

1
2
3
4
5
6
7

test

  @Autowired
    private UserMapper userMapper;

    @Test
    public void testFindAll(){
        List<User> all = userMapper.findAll();
        System.out.println(all);
    }
1
2
3
4
5
6
7
8
  1. xml 版 mapper

    @Repository
    @Mapper
    public interface UserMapper {
        List<User>  findAll();
    }
    
    
    1
    2
    3
    4
    5
    6

    xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.itheima.springbootprofiles.mapper.UserMapper">
        <select id="findAll" resultType="user">
            select * from t_user
        </select>
    </mapper>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

    配置文件

    spring:
    	datasource:
        	driver-class-name: com.mysql.cj.jdbc.Driver
        	username: root
        	password: 123456
        	url: jdbc:mysql:///springboot?serverTimezone=UTC
        	
    mybatis:
      mapper-locations: classpath:mapper/*Mapper.xml  #mapper的映射文件路径
      type-aliases-package: com.itheima.springbootprofiles.domain  #配置别名
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

    test

    @Autowired
        private UserMapper userMapper;
    
        @Test
        public void testFindAll(){
            List<User> all = userMapper.findAll();
            System.out.println(all);
        }
    
    1
    2
    3
    4
    5
    6
    7
    8
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48
Spring Security
Spring Boot 高级

← Spring Security Spring Boot 高级→

最近更新
01
k8s
06-06
02
进程与线程
03-04
03
计算机操作系统概述
02-26
更多文章>
Theme by Vdoing | Copyright © 2022-2025 Iekr | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式