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("===============");
    }
}

你可能感兴趣的:(JAVA开发技术,java)