SpringBoot学习笔记

配置

配置文件分类

SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用application.properties或者application.yml (application.yaml)进行配置。
properties:
server.port=8080

yml:
server:
port: 8080

yaml

YAML全称是YAML Ain’t Markup Language。YAML是一种直观的能够被电脑识别的的数据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导入,比如: C/C+ +, Ruby, Python, Java, Perl,C#,PHP等。YML文件是以数据为核心的,比传统的xml方式更加简洁。
YAML文件的扩展名可以使用.yml或者.yaml。

几种配置文件的对比:
yaml
properties:server.port=8080
server.address=127.0.0.1
xml

8080

127.0.0.1</ address></ server>
yml
server:
port: 8080
address: 127.0.0.1

基本语法:

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

数据格式:
对象(map):键值对的集合。

person :
	name : zhangsan #行内写法
person : {name: zhangsan}

数组:一组按次序排列的值

address:
- beijing- shanghai#行内写法
address: [beijing,shanghai]纯量:单个的、不可再分的值
msg1: 'h1lo ln wor1d'#单引忽略转义字符msg2 : "hello ln wor1d"#双引识别转义字符

YAML:参数引用

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

YAML小结:
1)配置文件类型
properties:和以前一样yml/yaml:注意空格
2) yaml:简洁,以数据为核心
●基本语法
大小写敏感
数据值前边必须有空格,作为分隔符
使用空格缩进表示层级关系,相同缩进表示同一级数据格式
对象
数组:使用“-”表示数组每个元素·纯量
●参数引用
${key}

读取配置内容

  1. @Value
  2. Environment
  3. @ConfigurationProperties

profile

我们在开发Spring Boot应用时,通常同一套程序会被安装到不同环境,比如:开发、测试、生产等。其中数据库地址服务器端口等等配置都不同,如果每次打包时,都要修改配置文件,那么非常麻烦。profile功能就是来进行动态配置切换的

  1. profile配置方式
    多profile文件方式
    yml多文档方式
  2. profile激活方式
    配置文件
    虚拟机参数
    命令行参数

Profile小结

  1. profile是用来完成不同环境下,配置动态切换功能的。
  2. profile配置方式
    多profile文件方式:提供多个配置文件,每个代表一种环境
    • application-dev.properties/yml开发环境
    • application-test.properties/yml测试环境
    • application-pro.properties/yml生产环境yml多文档方式:
      ·在yml中使用—分隔不同配置
  3. profile激活方式
    配置文件:
    在配置文件中配置:spring.prqfiles.active=dev
    虚拟机参数:在VM options指定:-Dspring.profiles.active=dev
    命令行参数: java-jar xox.jar --spring.profiles.active=dev

内部配置加载顺序

Springboot程序启动时,会从以下位置加载配置文件:

  1. file: ./config/:当前项目下的/config目录下
  2. file: ./︰当前项目的根目录
  3. classpath:/config/: classpath的/config目录
  4. classpath: / : classpath的根目缗
    加载顺序为上文的排列顺序,高优先级配置的属性会生效

外部配置加载顺序

通过官网查看外部属性加载顺序:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

SpringBoot整合其他框架

1.SpringBoot整合Junit
在这里插入图片描述
2.SpringBoot整合Redis
在这里插入图片描述
3.SpringBoot整合MyBatis
在这里插入图片描述
yaml文件的编写

spring:
  datasource:
    password: 123123
    username: root
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=CST&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
   mapper-locations: classpath:mapper/*Mapper.xml  #mapper的映射文件路径
   type-aliases-package: com.itheima.springbootmybatis1.domain

高级部分

SpringBoot原理分析

SpringBoot自动装配

Condition
Condition是在Spring4.0增加的条件判断功能,通过这个功能是可以实选择性的创建Bean操作。
SpringBoot是如何知道要创建哪个Bean的?比如SpringBoot是如
何知道要创建RedisTemplate的?

步骤1:编写ConditionClass类,需要继承Condition接口,重写matches方法。

public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata) 
        Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        String[] value = (String[]) map.get("value");
        boolean flag=true;
        try {
            for(String classname:value){
                Class<?> aClass = Class.forName(classname);
            }
        } catch (ClassNotFoundException e) {
            flag=false;
        }
        return flag;

    }
}

步骤二:编写ClassOnClas接口

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ClassCondition.class)
public @interface ConditionOnClass {
    String[] value();
}

Condition小结
自定义条件:

  1. 定义条件类:自定义类实现Condition接口,重写matches方法,在matches方法中进行逻辑判断,返回boolean值。matches方法两个参数:
    context: 上下文对象,可以获取属性值,获取类加载器,获取BeanFactory等。
    metadata:元数据对象,用于获取注解属性。
  2. 判断条件:在初始化Bean时,使用econditional (条件类.class)注解
    SpringBoot提供的常用条件注解:
    . conditionalonProperty:判断配置文件中是否有对应属性和值才初始化Bean.
    . conditionalonclass:判断环境中是否有对应字节码文件才初始化Bean
    . conditionalonMissingBean:判断环境中没有对应Bean才初始化Bean

切换内置服务器
https://blog.csdn.net/qq_44913491/article/details/105812738
@Enable*注解
SpringBoot中提供了很多Enable开头的注解,这些注解都是用于动态启用某些功能的,而其底层原理是使用@Import注解导入一些配置类,实现Bean的动态加载。

无法直接调用其他容器中的注入的类,因为@Compnentscan只能扫描其所在的包以及其子包,但是有三种解决办法:
方法一:使用@ComponentScan扫描对应的包

 @ComponentScan("com.itheima.config")

方法二:使用@Import加载类,这些类都会被Spring识别,放入容器

 @Import(UserConfig.class)

方法三:封装Import

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}

@Import
@Enable*底层依赖于@Import注解导入一些类,使用@Import导入的类会被Spring加载到IOC容器中,而@Import中导入提供4种用法:

  1. 导入Bean
  2. 导入配置类
  3. 导入ImportSelector实现类,一般用于加载配置文件中的类
  4. 导入ImportBeanDefinitionRegistrar实现类

自定义起步依赖
在这里插入图片描述
以Jedis为例:
创建spring-boot-autoconfiguration

@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfigration {

    @Bean
    public Jedis jedis(RedisProperties redisProperties){
        return new Jedis(redisProperties.getHost(),redisProperties.getPort());
    }
}

在resources 文件夹下创建META-INF/spring.factories 文件

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.itheima.redis.config.RedisAutoConfigration

在spring-boot-starter中继承spring-boot-autoconfiguration

  <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>redis-spring-boot-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

在主模块中导入自己的创建的文件。

<!--        导入自己创建的redis类-->
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>redis-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

SpringBoot监听机制

Java监听机制
SpringBoot的监听机制,其实是对Java提供的事件监听机制的封装。Java中的事件监听机制定义了以下几个角色:

  1. 事件:Event,继承java.util.EventObject类的对象
  2. 事件源:Source,任意对象Object
  3. 监听器: Listener,实现java.util.EventListener接口的对象

SpringBoot监听机制
SpringBoot在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成—些操作。
ApplicationContextInitializer、SpringApplicationRunListener、CommandLineRunner、ApplicationRunner

SpringBoot 监控

Spring Boot Admin是一个开源社区项目,用于管理和监控SpringBoot应用程序。Spring Boot Admin有两个角色,客户端(Client)和服务端(Server)。
应用程序作为Spring Boot Admin Client向为Spring Boot Admin Server注册
Spring Boot Admin Server的UI界面将Spring Boot Admin Client的ActuatorEndpoint上的一些监控信息。
在这里插入图片描述

SpringBoot 项目部署

SpringBoot项目开发完毕后,支持两种方式部署到服务器:

  1. jar包(官方推荐)
  2. war包
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值