2024年2月13日 星期二

[Spring Boot] 根據尚硅谷IT培训学校教程學習12-23

 這是Day3,一樣使用此教材: 

 尚硅谷IT培训学校: https://www.youtube.com/watch?v=GxOeqaAajtI&list=PLmOn9nNkQxJEeIH75s5pdTUnCo9-xOc7c&index=12


Spring Boot是框架(Spring)的框架

Spring Boot像傻瓜相機

能調整底層,才是像單眼一樣可以自己調整

場景自動配置了那些組件,能不能Autowired來使用

修改默認參數




要在右邊Dependencies上面按右鍵,download source

Ctrl  + N : finds a class by name.



找到RedisAutoConfiguration.java

發現屬性綁定於RedisProperties.java

知道前綴是spring.data.redis


https://github.com/fenturechance/spring-boot-practice-240128/commit/7b8b1ff96729ba5553fa9079d33063c54b407adc




RedisInsight


Redis的Docker-compose


version: '3.3'

services:
  redis:
    image: redis:latest
    restart: always
    ports:
      - "6379:6379"
    volumes:
      - ./dаta:/root/redis
      - ./redis.conf:/usr/local/etc/redis/redis.conf
    environment:
      - REDIS_PASSWORD=jerry
      - REDIS_PORT=6379
      - REDIS_DATABASES=16



Spring Boot場景列表: https://docs.spring.io/spring-boot/docs/current/reference/html/using.html#using.build-systems.starters


properties和yaml相比,層次較低

加dependency: 右邊maven,按右鍵analysis dependencies,然後按sync


在properties上寫嵌套Object、List、Map: https://github.com/fenturechance/spring-boot-practice-240128/commit/2eb02e4c565735aad7d08d0e00da10a3b910139f

印出結果: 

personPerson(id=18, name=張三, birthday=Sun Feb 16 12:12:12 CST 2020, like=true, child=Child(name=李四, age=12, birthday=Fri Feb 16 12:12:12 CST 2024, text=[abc, def]), dogs=[Dog(id=null, name=小黑, age=1), Dog(id=null, name=小白, age=2)], cats={c1=Cat(id=null, name=小咪, age=3), c2=Cat(id=null, name=小喵, age=4)})


在yml寫出同上效果: https://github.com/fenturechance/spring-boot-practice-240128/commit/eb6e4b160c5c889dc0ad3c12e5de56da9ee1ec43


jps 列舉出所有java進程

2024-02-28T15:34:02.215+08:00  INFO 15480 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 21.0.2 with PID 15480 (C:\Users\user\IdeaProjects\demo\target\classes started by user in C:\Users\user\IdeaProjects\demo)

2024-02-28T15:34:02.216+08:00  INFO 15480 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"


日誌級別 / 進程ID / 線程名(main方法) / Logger名(類名)


application.properties

logging.pattern.console

%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd'T'HH:mm:ss.SSSXXX}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}


LogFactory: https://github.com/fenturechance/spring-boot-practice-240128/commit/1269850448b39ea74dd7fd3525dcaacf384e518f

2024-02-28T15:43:44.158+08:00  INFO 7484 --- [nio-8080-exec-1] com.example.demo.HelloController         : 這是info


cmd+shift+o (cmd+shift+n in older versions) leads to the file open dialog


Slf4j: https://github.com/fenturechance/spring-boot-practice-240128/commit/1a9825230823a4d92579c1d289f08b63d9f0b55c


2024-02-28T15:51:21.920+08:00  INFO 15780 --- [nio-8080-exec-1] com.example.demo.HelloController         : info


loggin.level.root=debug

這樣debug級別的Log就會打印出來


logging.level.com.example.demo.HelloController=debug

可以指定某個controller的log級別


字符串格式化: https://github.com/fenturechance/spring-boot-practice-240128/commit/f2bfa96fd01f836dee0c0d1d64135ee2e56b4d45

http://localhost:8080/log?a=1&b=2

2024-02-28T16:11:48.634+08:00  INFO 19980 --- [nio-8080-exec-1] com.example.demo.HelloController         : info, a: 1, b: 2


日誌分組

logging.group.abc=com.atguigu.logging.controller, com.atguigu.logging. service, com. aaa, com.bbb

logging.level.abc=debug

預設jdbc、sql等都是分組到sql並且是debug級別


logging.file.name=demo.log

這樣就會多一個demo.log檔案

logging.file.name=D:\\demo.log

(Windows寫法)


logging.file.path=D:\\ 

path和name同時存在: 以name為主

預設名: spring.log


只要超過1MB就切割

logging.logback.rollingpolicy.file-name-pattern=${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz

logging.logback.rollingpolicy.max-file-size=1MB

換日期也會切割


排除預設日誌框架


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>






沒有留言:

張貼留言