小编典典

后台工作者的Spring Boot应用程序

spring-boot

我定义了以下Spring Boot应用程序:

@SpringBootApplication
public class Application implements CommandLineRunner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(WebApplicationType.NONE).run(args);
    }

    public void run(String... args) throws Exception {
        Thread.currentThread().join();
    }
}

我还有一揽子工人(即可以是实现的类Runnable)。他们应该无限期地奔跑。

运行它们的“Spring方式”是什么?(并且自动执行,而无需明确知道它们)

谢谢


阅读 402

收藏
2020-05-30

共1个答案

小编典典

您可以(1)用@Component注释实现Runnable的类,以便Spring可以找到它们。然后,您可以(2)用@Service编写带有@Autowired列表的WorkManager,该列表将由(1)中所有类的列表实例进行初始化。并且可以(3)在WorkManager中编写一个@PostConstruct方法,该方法将遍历该Runnable列表,然后将每个传递给TaskExecutor来运行…

2020-05-30