简体   繁体   English

在 Spring 上下文初始化之后但在接受请求之前执行方法

[英]Execute method after Spring context initialized but before requests are accepted

In a spring boot app (2.3), is it possible to have a method be executed after context is initialized, but before the app starts to take requests?在 Spring Boot 应用程序 (2.3) 中,是否可以在上下文初始化之后但在应用程序开始接受请求之前执行方法? I don't know if becoming ready to accept requests is part of the spring context initialization or if that is a separate step.我不知道准备好接受请求是否是 spring 上下文初始化的一部分,或者这是一个单独的步骤。

Note that ideally if this method throws an exception, the service fails to start, but that is not a requirement.请注意,理想情况下,如果此方法引发异常,服务将无法启动,但这不是必需的。

I tried ContextRefreshedEvent .我试过ContextRefreshedEvent However, the app is accepting requests at that point.但是,该应用程序此时正在接受请求。 (While my ContextRefreshedEvent event listener method is executing, requests can come in and execute at the same time.) (当我的ContextRefreshedEvent事件侦听器方法正在执行时,请求可以进入并同时执行。)

(I can't use @PostContruct , because my method depends on many different beans being initialized.) (我不能使用@PostContruct ,因为我的方法取决于正在初始化的许多不同的 bean。)

The specific use case is that I have a spring cloud config server.具体用例是我有一个 spring 云配置服务器。 I want it to pre-load authentication data into cache prior to accepting requests.我希望它在接受请求之前将身份验证数据预加载到缓存中。 The loading of this data depends on bootstrap/app config files being loaded, ApplicationEnvironmentPreparedEvent listener being called, beans being constructed, etc.此数据的加载取决于正在加载的引导程序/应用程序配置文件、正在调用的ApplicationEnvironmentPreparedEvent侦听器、正在构造的 bean 等。

As Andy Wilkinson pointed out (thank you!), to do this, implement a bean's method with the desired behavior and add the @PostConstruct annotation.正如 Andy Wilkinson 指出的(谢谢!),要做到这一点,请实现具有所需行为的 bean 方法并添加 @PostConstruct 注释。 The method will be called after all beans are initialized (and environment prepared) but before the service starts taking requests.该方法将在所有bean 初始化(并准备好环境)之后但在服务开始接受请求之前调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Testng && Spring 上下文单元测试 -> 执行 sql 前后测试方法 - Testng && Spring context unit test -> execute sql before and after test method 使用Spring,如何在初始化HIbernate之后但在应用程序启动之前执行代码? - With Spring how can I execute code after HIbernate is initialized but before the application starts? Spring应用程序上下文-在FactoryBean初始化之前调用FactoryBean.getObject() - Spring application context - FactoryBean.getObject() called before FactoryBean initialized 在方法之前和之后执行代码? - Execute code before and after method? 初始化bean时如何让Spring执行方法? - How to get Spring to execute a method when the bean is initialized? 在所有spring内容初始化后如何执行代码? - How to execute code after all spring content is initialized? 在spring上下文初始化开始之前执行一段java代码 - Execute piece of java code before spring context initialization starts JUnit-在@BeforeAll之前执行的方法和在@AfterAll之后执行的方法 - JUnit - method to execute before @BeforeAll and method to execute after @AfterAll NotWritablePropertyException-初始化上下文时-Spring - NotWritablePropertyException - When context is initialized - Spring Java 注解在方法前后执行代码 - Java annotation to execute code before and after method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM