Springboot启动执行特定代码的方式汇总

作者:MrDeng886 时间:2023-08-06 04:42:24 

实现InitializingBean接口或使用@PostConstruct注解

实现InitializingBean如下


public class AnotherExampleBean implements InitializingBean {

@Override
   public void afterPropertiesSet() {
       // 做一些初始化的工作
   }
}

官方对其的解释是这样的:实现这个接口会让这个bean的所有必要的属性都被容器注入后(依赖注入),再去执行afterPropertiesSet()里的方法。

笔者再用一个简单的例子去实际演示一下(注意:使用@PostConstruct和实现接口是等价的,可以二选一

我们在init方法上使用了@PostConstruct注解,并且方法里使用到了Chicken类,而这个Chicken类是通过依赖注入来设置的,所以印证了官方说的话,会在依赖注入完以后才会调用@PostConstruct注解的方法。那为什么不在构造器里往List里面方Chicken类呢,因为容器调用构造器方法的时候,Chicken类还没被注入,所以要写在@PostConstruct注解的方法里。


// 首先声明一个实体类
@Data
public class Chicken {
   private String name ;
}
// 将他注入容器
@Configuration
public class UserConfig {
   @Bean
   public Chicken putUser(){
       Chicken chinken = new Chicken();
       chinken.setName("普通鸡块");
       return chinken;
   }
}
// 在family 类中调用 注入chinken
@Component
public class Family {
   @Resource
   Chicken chicken;

public static List<String> names;

@PostConstruct
   public void init(){
       names.add(chicken.getName());
   }
   public Family() {
       names = new LinkedList<>();
   }
}

Springboot启动执行特定代码的方式汇总

实现ApplicationListener接口

如果一个容器里的bean实现了ApplicationListener接口,那么在任何时候,如果有ApplicationEvent(事件)在ApplicationContext(容器)中被发布,该bean会收到通知,从而可以执行相应策略。

下面是Spring提供的几种常用的ApplicationEvent事件

事件名称解释
ContextRefreshedEvent当容器ApplicationContext容器正在初始化或refreshed时会发布这个事件。这里的初始化意味着所有的bean都被加载,并且有后置处理的bean都被检测到并激活了。
ContextStartedEvent当容器启动调用start()方法是会发布这个事件,这里的开始是所有生命周期的bean都收到了一个开始的信号
ContextStoppedEvent当容器调用stop方法时会发布这个事件

举一个简单的例子,下面的代码我实现ApplicationListener接口并监听ContextRefreshedEvent事件,所以当springboot启动并且初始化完成后,就能执行下面的方法了。


@Component
@Slf4j
public class MenuManage implements ApplicationListener<ContextRefreshedEvent> {

@Override
   public void onApplicationEvent(ContextRefreshedEvent event) {
      //做一些事情
   }
}

实现CommandLineRunner或ApplicationRunner 接口

实现了CommandLineRunner的bean会被springboot监测到,并在项目启动后执行run方法,如果有多个bean实现了CommandLineRunner接口,那么可以使用order注解来指定执行顺序。


@Order(2)
@Component
public class ServerStartedReport implements CommandLineRunner{
   @Override
   public void run(String... args) throws Exception {
       //do something
   }
}

而实现ApplicationRunner接口与实现CommandLineRunner的唯一不同是,后者接收的参数是main方法传进去的原始参数,而ApplicationRunner接收的参数是封装过原始参数的,可以通过参数名字name来获取指定的参数。


@Component
public class MyApplicationRunner implements ApplicationRunner{

@Override
   public void run(ApplicationArguments args) throws Exception {
       System.out.println("ApplicationRunner:"+ Arrays.asList(args.getSourceArgs()));
       System.out.println("getOptionNames:"+args.getOptionNames());
       System.out.println("getOptionValues:"+args.getOptionValues("foo"));
       System.out.println("getOptionValues:"+args.getOptionValues("log"));
   }
}

来源:https://blog.csdn.net/qq_35347200/article/details/121678579

标签:Springboot,启动,执行代码
0
投稿

猜你喜欢

  • Android实现时钟特效

    2022-09-08 01:43:22
  • Android开发使用自定义View将圆角矩形绘制在Canvas上的方法

    2021-06-08 01:03:17
  • C#多线程之线程池(ThreadPool)

    2022-05-02 07:12:15
  • 基于java实现租车管理系统

    2022-02-08 12:48:49
  • SpringMVC用JsonSerialize日期转换方法

    2021-12-06 10:59:59
  • Java实现斗地主案例

    2023-06-01 06:43:59
  • Android使用OkHttp进行重定向拦截处理的方法

    2022-09-12 15:47:32
  • 解析C#中#region与#if的作用

    2021-09-18 16:48:28
  • c# 插入数据效率测试(mongodb)

    2021-12-16 12:46:07
  • 解析Mybatis SqlSessionFactory初始化原理

    2022-07-09 04:24:05
  • java中String、StringBuffer与StringBuilder的区别

    2021-11-12 13:28:24
  • Java 8 开发的 Mybatis 注解代码生成工具

    2023-01-02 19:53:44
  • 通过Java实现在Word中创建可填充表单

    2023-08-05 21:11:40
  • C#使用伪随机数实现加密用户密码的方法

    2023-06-27 05:27:56
  • springboot+mybatis+redis 二级缓存问题实例详解

    2022-08-09 09:06:36
  • java实现简单的猜数字小游戏

    2023-11-11 12:55:40
  • C语言实现俄罗斯方块源代码

    2023-02-28 13:16:45
  • 深入浅出讲解Java集合之Collection接口

    2023-05-27 05:53:38
  • 浅谈Java由于不当的执行顺序导致的死锁

    2022-08-05 22:05:33
  • C#将Excel中的数据转换成DataSet

    2021-10-29 18:20:55
  • asp之家 软件编程 m.aspxhome.com