springboot项目启动后执行方法,后置执行bean,后加载

1:指定执行顺序

当项目中同时实现了ApplicationRunner和CommondLineRunner接口时,可使用Order注解或实现Ordered接口来指定执行顺序,值越小越先执行。

2:实现CommandLineRunner接口

@Component
@Order(value = 1)
public class VibratorListener implements CommandLineRunner {
    @Value("${ntcp.port}")
    private Integer port;
    @Autowired
    private VibratorService vibratorService;

    @Override
    public void run(String... args) throws Exception {
        MyClass.initServer(port, vibratorService);
    }
}

2:实现ApplicationRunner接口

@Component
@Order(value = 2)
public class VibratorListener implements ApplicationRunner {


    @Value("${ntcp.port}")
    private Integer port;
    @Autowired
    private VibratorService vibratorService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        MyClass.initServer(port, vibratorService);
    }
}

3:实现ApplicationRunner接口

@Component

@Order(value = 2)
public class VibratorListener implements ApplicationRunner {


    @Value("${ntcp.port}")
    private Integer port;
    @Autowired
    private VibratorService vibratorService;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        MyClass.initServer(port, vibratorService);
    }
}

你可能感兴趣的:(spring,boot,java,spring)