SpringBoot学习笔记四

1. SpringBoot监听器

在这里插入图片描述上述的四种监听器按照使用的方式可以分为两种:

1.1 无需配置

  • CommandLineRunner
  • ApplicationRunner:还需要写一个有参的构造函数
@Component
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner...run");
        System.out.println(Arrays.asList(args));
    }
}
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("MyApplicationRunner...run");
        System.out.println(Arrays.asList(args.getSourceArgs()));
    }
}

项目启动时,会自动执行上述的内容。

1.1.1 CommandLineRunner使用

日常开发中有可能需要实现项目启动后执行的功能,比如特殊数据处理,权限控制、缓存预热等
按照使用可以分为单个实现类和多个实现类,

  • 单个实现类如上面,无需指定执行顺序;
  • 多个实现类如果需要指定执行顺序,需要使用@Order注解来表明执行顺序
@Component
@Order(3)
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner...run");
        System.out.println(Arrays.asList(args));
    }
}

@Component
@Order(2)
public class MyCommandLineRunner1 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner1...run1");
        System.out.println(Arrays.asList(args));
    }
}
@Component
@Order(1)
public class MyCommandLineRunner2 implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner2...run2");
        System.out.println(Arrays.asList(args));
    }
}

执行结果:
在这里插入图片描述注解@Order的执行级别是按照1最优先执行,后面依次执行。

1.1.2 ApplicationRunner的使用

spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法。

run方法的参数: ApplicationArguments可以获取到当前项目执行的命令参数。(比如把这个项目打成jar执行的时候,带的参数可以通过ApplicationArguments获取到)
若有多个代码段需要执行,可用@Order注解设置执行的顺序。

1.1.3 CommandLineRunner与ApplicationRunner的区别

  • CommandLineRunner的方法参数是原始的参数,未做任何处理;
  • ApplicationRunner的参数为ApplicationArguments对象,是对原始参数的进一步封装。

1.2 需要创建META-INF文件,并在其中创建spring.factories,配置相关的信息

  • ApplicationContextInitialize
  • SpringApplicationRunListener

1.2.1 ApplicationContextInitialize

目前不去细研究,有兴趣可以看这篇博客:Springboot扩展点之ApplicationContextInitializer

1.2.2 SpringApplicationRunListener

详细内容看这篇博客:SpringApplicationRunListeners 监听器执

相关推荐

  1. PyTorch学习笔记

    2024-04-10 12:26:01       21 阅读
  2. android学习笔记()

    2024-04-10 12:26:01       10 阅读
  3. SpringBoot学习笔记

    2024-04-10 12:26:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 12:26:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 12:26:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 12:26:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 12:26:01       20 阅读

热门阅读

  1. Vue文档

    Vue文档

    2024-04-10 12:26:01      15 阅读
  2. LeetCode 15.三数之和

    2024-04-10 12:26:01       13 阅读
  3. 【Livox激光MID-360】调试记录

    2024-04-10 12:26:01       17 阅读
  4. 深入剖析OR与UNION的区别及应用场景

    2024-04-10 12:26:01       14 阅读
  5. python实现pdf的页面替换

    2024-04-10 12:26:01       11 阅读
  6. 【SQL】mysql数据库常见函数功能、适用场景

    2024-04-10 12:26:01       13 阅读
  7. swagger记录

    2024-04-10 12:26:01       14 阅读