springboot 项目初始化执行,只在项目启动时执行一次

一,ApplicationRunner 接口

@Component
public class Runner implements ApplicationRunner {
    @Autowired
    private ZkUserService zkUserService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("===============");
        
    }
}

二,CommandLineRunner 接口

@Component
public class Runner implements CommandLineRunner {
    @Autowired
    private ZkUserService zkUserService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("===============");
    }
}

你可能感兴趣的:(笔记)