Springboot复习

本笔记来自b站尚硅谷

文章目录

  • Springboot
  • HelloWorld原理
    • @Value获取值和@ConfigurationProperties获取值比较
  • @PropertySource 和 @ImportResource
  • profile
  • 自动配置原理
  • 整合日志
    • 指定配置
  • 全面接管SpringMVC
  • 启动流程
  • 自定义starter
  • dev-tools
  • yml提示
  • 指标监控

Springboot

Springboot复习_第1张图片


Springboot复习_第2张图片


HelloWorld原理

Springboot复习_第3张图片


Springboot复习_第4张图片

Springboot复习_第5张图片

配置文件中 last-name 和 lastName是一样的

@Value获取值和@ConfigurationProperties获取值比较

Springboot复习_第6张图片

@PropertySource 和 @ImportResource

Springboot复习_第7张图片


Springboot复习_第8张图片

Springboot复习_第9张图片

profile

Springboot复习_第10张图片

Springboot复习_第11张图片

自动配置原理

在这里插入图片描述

自动配置

相关注解@configurationProperties 读配置文件的配置 能配什么属性都是靠xxxProperties类,这些属性又是和配置文件绑定的

根据不同的条件判断,判断这个配置类是否生效

Springboot复习_第12张图片

我们可以通过用debug=true属性﹔来让控制台打印自动配置报告,这样我们就可以很方便的知道哪些自动配置类生效

整合日志

Springboot复习_第13张图片

在这里插入图片描述

Springboot复习_第14张图片

Springboot复习_第15张图片

在这里插入图片描述

使用

Springboot复习_第16张图片

指定配置

Springboot复习_第17张图片

Springboot复习_第18张图片
如果使用的是logback-spring.xml 这个名字, 就没有以上的配置 不然报错~

全面接管SpringMVC

  • 肯定不推荐加@EnableWebMvc

Springboot复习_第19张图片

Springboot复习_第20张图片

启动流程

1.创建springapplication对象

Springboot复习_第21张图片

2 运行run方法

扫描创建加载所有组建的地方 refreshContext(context);

自定义starter

1.先创建一个空项目,后创建一个maven工程

Springboot复习_第22张图片

2.再创建一个springboot项目

Springboot复习_第23张图片

springboot项目结构

Springboot复习_第24张图片

<dependencies>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starterartifactId>
    dependency>

dependencies>

HelloService

package com.tian.starter;

public class HelloService {

    HelloProperties helloProperties;

    public String sayHello(String name){
        return helloProperties.getPrefix()+"-"+name+"-"+helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

HelloProperties

package com.tian.starter;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "tian.hello")
public class HelloProperties {
    private String prefix;
    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}

HelloServiceConfiguration

@Configuration
@ConditionalOnWebApplication //web才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceConfiguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tian.starter.HelloServiceConfiguration

maven项目 引入依赖

<dependencies>
    
    <dependency>
        <groupId>com.tian.startergroupId>
        <artifactId>tian-spring-boot-starter-autoconfigurerartifactId>
        <version>0.0.1-SNAPSHOTversion>
    dependency>
dependencies>

两个项目打包

new 一个springboot项目测试

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>

<dependency>
    <groupId>com.tian.startergroupId>
    <artifactId>tian-spring-boot-starterartifactId>
    <version>1.0-SNAPSHOTversion>
dependency>

controller

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        return  helloService.sayHello("tian");
    }
}

访问测试

Springboot复习_第25张图片

dev-tools

Springboot复习_第26张图片

ctrl+f9 重新编译

yml提示

Springboot复习_第27张图片

@RequestAttribute

Springboot复习_第28张图片

知识点

redisTemplate 底层用的就是Lettuce

指标监控

springboot Actuator

1.导入依赖

Springboot复习_第29张图片

直接访问

localhost:8080/actuator/health

但是不能访问某些端点 比如 localhost:8080/actuator/beans

可以配置暴露所有端点

Springboot复习_第30张图片

最常用的Endpoint

  • Health:监控状况
  • Metrics:运行时指标
  • Loggers:日法记录

你可能感兴趣的:(springboot,spring,boot,java)