SpringBoot启动时初始化参数

发布时间 2023-05-24 17:24:03作者: 土豆泥呀

1、通过@PostConstruct注解进行初始化

@Service
public class InitParam {
    @PostConstruct
    public void init(){
        System.out.println("==========@PostConstruct初始化");
    }
}

2、通过实现InitializingBean接口,重写afterPropertiesSet方法进行初始化

@Service
public class InitParam implements InitializingBean{
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("==========实现InitializingBean接口初始化");
    }
}

3、通过监听器方式,实现ApplicationListener,重写onApplicationEvent方法

@Service
public class InitParam implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("==========实现ApplicationListener初监听器始化");
    }
}