若依使用线程池的两种方式

若依使用线程池的两种方式

1、注解

@Component
@EnableAsync
public class DataPreloader implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(DataPreloader.class);

    @Override
    @Async
    public void run(ApplicationArguments args) {
		log.info("注解方式异步执行");
    }
}

2、属性注入

@Component
public class DataPreloader implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(DataPreloader.class);

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public void run(ApplicationArguments args) {
        threadPoolTaskExecutor.execute( () -> {
		    log.info("属性注入");
		});
    }
}

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