Springboot学习记录(二)

1. springboot自动装配的理解

1.1 main方法的理解

该方法内自带启动行为

SpringApplication.run(Springboot01Application.class, args);

SpringApplication:Spring Boot的SpringApplication类,该类启动一个Spring应用,也就是初始化spring。

run方法:run方法

  1. 创建SpringApplication对象

  2. 利用创建好的SpringApplication对象调用run方法

先看run源码,该方法默认返回一个ConfigurableApplicationContext对象,run需要传入两个参数,SpringApplication 对象实例和args,SpringApplication 对象是用来执行run方法,那args有什么用呢?

    public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
        // 创建引导上下文
    DefaultBootstrapContext bootstrapContext = createBootstrapContext();
    ConfigurableApplicationContext context = null;
        // 配置应用为headless模式
        // Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标
    configureHeadlessProperty();
        // 获取Spring应用运行监听器,这里其实是初始化了一个广播器,将前面获取的所有的监听器注册到广播器中
        // 后续的流程中会触发各种事件
    SpringApplicationRunListeners listeners = getRunListeners(args);
        // 获取Spring应用运行监听器后,立刻发出第一个事件,标志着应用要开始启动了,进行最早的一些初始化工作
    listeners.starting(bootstrapContext, this.mainApplicationClass);
    try {
                // 封装启动参数
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                // 准备运行环境,对配置文件的解析就在这一步
        ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
                // 创建应用上下文
        context = createApplicationContext();
        context.setApplicationStartup(this.applicationStartup);
                // 准备应用上下文
        prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
                // 刷新应用上下文
        refreshContext(context);
                // 刷新应用上下文后处理
        afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
                // 发出启动结束事件,上下文已刷新,应用程序已启动,但尚未调用CommandLineRunner和ApplicationRunner。
        listeners.started(context);
                // 执行runner的run方法
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, listeners);
        throw new IllegalStateException(ex);
    }
    try {
                // 发出运行中事件,此时应用程序上下文已刷新,并且所有CommandLineRunner和ApplicationRunner都已调用
        listeners.running(context);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, null);
        throw new IllegalStateException(ex);
    }
        // 返回最终构建的容器对象
    return context;
    }

args理解

简单的说就是可以用它来写入 配置; 并且是覆盖项目中的配置(因为他的优先级更高); 例如

java -jar --spring.profiles.active=dev

了解更多看大佬分析【SpringBoot 一】SpringApplication启动类的Args详解_石臻臻的杂货铺的博客-CSDN博客_application启动类

1.2 main方法上的注解理解

  • @SpringBootApplication

    springboot的核心注解目的是开启自动配置,点进注解源码:

    public @interface SpringBootApplication

    返回值为@interface,@interface理解:声明该类为注解。

    再来看看修饰SpringBootApplication的注解

     

    初步了解一下从上到下各个注解的作用:

     
  • @Target({ElementType.TYPE}):作用目标在接口、类、枚举、注解

  • @Retention(RetentionPolicy.RUNTIME):这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。

  • @Documented:生成文档时保留该注解

  • @Inherited:被修饰的子类注解可以继承该注解(这里的该指被@Inherited修饰的注解)

  • @SpringBootConfiguration:表示该类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。注意:它只是@Configuration注解的派生注解;@Configuration注解的功能一致;只不过@SpringBootConfiguration是springboot的注解,而@Configuration是spring的注解。

  • @EnableAutoConfiguration:开启Spring应该上下文的自动配置,尽可能去加载需要的配置Beans。这就是很多默认配置开启的地方,如果需要加上自行配置类生效,也需要该注解。

  • @ComponentScan:作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中,同样也可以进行过滤。一般对有@Component,@Repository,@Service,@Controller,@Bean修饰的类进行装配。

1.3需要更深层次了解可看图自行找源码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值