Spring boot学习

阅读书目:《spring实战》

2021.1.11

  Spring和Spring Boot的必备知识

     核心概念

    容器(container)

      也称为Spring应用的上下文,可以创建和管理应用组件。

    组件(bean)

      为一个应用提供某些功能或服务的对象称为组件。将所需要的bean放入容器中,形成一个完整的应用程序。

    依赖注入(Dependency Injection)

      有些组件功能的实现是依赖于其他组件的,比如商品服务是依赖于库存服务的,所以在使用商品服务bean的时候可以注入库存服务bean,注入过程是依靠容器实现的

      容器和组件装配的手动方法

    1.xml文件

    形式如下,其中constructor-arg指定构造函数的子标签

<bean id="xxxService"
           class="类所在包名"  />
<bean id="xxxService"
           class="类所在包名">
<constructor-arg ref="依赖注入的类名"/>
</bean>

    2.java文件(偏向采用这种方式)

@Configuration   //告知Spring这是一个配置类,会Spring应用上下文提供bean
public class ServiceConfiguration{
        @Bean  //表示这些方法返回的对象会以bean的形式添加到Spring应用上下文
        (需要注入的类的创建方法)
        
       @Bean
       被注入的类的创建方法
        return new 被注入类名(需要注入的类的创建方法)
}               

    2.自动配置,Spring Boot可以做到这一点

  初始化Spring项目

    因为我采用的是IDEA,可以直接使用Spring Initializer初始化Spring应用,具体创建流程不细说,假设我会创建一个DemoApplication

    目录结构

    DemoApplication.java:Spring Boot的主类,它会启动该项目。

    src/main/java :存放源码

      src/test/java:存放测试代码

    src/main/resources :存放非java的资源,这个文件夹下方还分了如下文件夹:

          static:存放为浏览器提供的任何静态资源,如图片、样式表、JavaScript

          templates(idea里初始化项目时可能没有,自己建就好):存放用来渲染内容到浏览器的模板文件,如Thymeleaf

    pom.xml:Maven构建规范。包含了我们选择的依赖。

   

@SpringBootApplication[有这个注解的类代表是SpringBoot的启动类]= (默认属性)@Configuration【配置类】 + @EnableAutoConfiguration【自动配置类】 + @ComponentScan【组件扫描】

2021.2.14

  吃完晚饭后,在b站上看了网课,记录一下今天学的

  @Configuration     【配置】

  用于定义配置类,可以替代xml文件

  @ComponentScan【组件扫描】

  spring底层在解析配置类,会去解析@ComponentScan,读取basePackages,如果没有读取到就会把当前配置类的包当作扫描包。

   1 public ConfigurableApplicationContext run(String... args) {

       //StopWatch是位于org.springframework.util包下的一个工具类,通过它可方便的对程序部分代码进行计时(ms级别),适用于同步单线程代码块。
 2         StopWatch stopWatch = new StopWatch();
       //开始计时
3 stopWatch.start();
      //创建一个ApplicationContext的父级上下文
4 DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
      //在ApplicationContext的基础上增加了一系列配置应用上下文的功能。配置应用上下文和控制应用上下文生命周期的方法在此接口中被封装起来。
5 ConfigurableApplicationContext context = null;
       // 这里涉及到一个headless模式,该模式是系统的一种配置模式。在系统可能缺少显示设备、键盘或鼠标这些外设的情况下可以使用该模式。
6 this.configureHeadlessProperty();
      //获取应用监听器列表
7 SpringApplicationRunListeners listeners = this.getRunListeners(args);
      //执行监听器列表中的启动方法
8 listeners.starting(bootstrapContext, this.mainApplicationClass); 9 10 try {
          // 创建一个DefaultApplicationArguments对象,它持有着args参数,就是main函数传进来的参数
11 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        //准备环境
12 ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments); 13 //配置忽略bean
         this.configureIgnoreBeanInfo(environment);
        //一行主要就是打印那个启动时的spring图案
14 Banner printedBanner = this.printBanner(environment); 15 //创建上下文
         context = this.createApplicationContext();
16 context.setApplicationStartup(this.applicationStartup);
        //容器准备工作
17 this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
       //刷新容器,里面有一个对
registerShutdownHook的判断,这个东西叫做“优雅关闭”,即程序在运行时发生了意外退出,可以写一个程序做一些补救措施
18       this.refreshContext(context);
        //后置刷新
19 this.afterRefresh(context, applicationArguments); 20 stopWatch.stop(); 21 if (this.logStartupInfo) { 22 (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch); 23 } 24 25 listeners.started(context); 26 this.callRunners(context, applicationArguments); 27 } catch (Throwable var10) { 28 this.handleRunFailure(context, var10, listeners); 29 throw new IllegalStateException(var10); 30 } 31 32 try { 33 listeners.running(context); 34 return context; 35 } catch (Throwable var9) { 36 this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null); 37 throw new IllegalStateException(var9); 38 } 39 }

上图参考:https://blog.csdn.net/qq_32828253/article/details/109496848

 为什么要生成uuid?

参考:https://blog.csdn.net/luanxiyuan/article/details/84355456

这个uuid是满足了分布式系统中所有元素有唯一的标识。一般用来做主键

      

  

原文地址:https://www.cnblogs.com/pipihoudewo/p/14263506.html