简体   繁体   English

@PostConstruct 和 @PreDestroy 注解的实时应用

[英]Real time applcation use of @PostConstruct and @PreDestroy annotation

And also in a spring-boot application, we don't shutdown context anywhere so how web application manages this shutdown context and destroying beans working.而且在 spring-boot 应用程序中,我们不会在任何地方关闭上下文,因此 Web 应用程序如何管理此关闭上下文并破坏 bean 工作。

When actually this web application will trigger the shutdown of context and trigger @PreDestroy annotation.实际上,此 Web 应用程序将触发上下文的关闭并触发 @PreDestroy 注释。

It depends on the scope of the bean .这取决于 bean 的范围。

For the request scoped bean , it is called when finish processing an HTTP request.对于请求范围的 bean,它在处理完 HTTP 请求时被调用。

For the session scoped bean, it is called when the session is destroyed (eg timeout due to inactivity).对于会话范围的 bean,它在会话被销毁时被调用(例如,由于不活动而超时)。

For the prototype bean, it will not be called and it is the developer responsibility to call the destruction logic.对于prototype bean,它不会被调用,调用销毁逻辑是开发人员的责任。 (Mentioned in the document in this section ). (在本节的文档中提到)。

For the singleton bean, it is called when you terminate the application normally (ie.by sending a SIGTERM signal such as kill 12345 where 12345 is your application process ID).对于单例 bean,它会在您正常终止应用程序时调用(即通过发送 SIGTERM 信号,例如kill 12345 ,其中 12345 是您的应用程序进程 ID)。 Please note that it is not called if you "hard kill" the application using kill -9 12345请注意,如果您使用kill -9 12345 “硬杀死”应用程序,则不会调用它

@PostConstruct is very helpful if you wanted to do anything after all your beans are initialized.如果您想在所有 bean 初始化后做任何事情,@PostConstruct 非常有用。 I had the following use case where I had used @PostConstruct我有以下用例,其中我使用了 @PostConstruct

I had 11 implementations of customerProcess class and after each operation, I had to call a different process so we needed to autowire all its implementation instead we used postconstruct to get an enum map to which we supplied the class name and we got the corresponding bean, So Post Construct worked here for me我有 11 个 customerProcess 类的实现,在每次操作之后,我不得不调用一个不同的进程,所以我们需要自动装配它的所有实现,而不是我们使用 postconstruct 来获取我们提供类名的枚举映射,我们得到了相应的 bean,所以 Post Construct 在这里为我工作

 @PostConstruct
  private void init() {
    CustomerProcessTask.getClassList().forEach( aClass -> {
      final CustomerTask customerTypeBean = applicationContext.getBean(aClass);
      factory.put(aClass,customerTypeBean);
    });
  }

@PreDestroy has various use cases where you use it when you need to shutdown open db connection or rollback something before applicationContext.close is called. @PreDestroy 有多种用例,当您需要在调用 applicationContext.close 之前关闭打开的数据库连接或回滚某些内容时,您可以使用它。 But in the case of web application, dispatcherServlet creates applicationContext and it closes the context whenever server stops.但是在 Web 应用程序的情况下,dispatcherServlet 创建 applicationContext 并在服务器停止时关闭上下文。 We don't need to explicitly invoke applicationContext.close().我们不需要显式调用 applicationContext.close()。

@PreDestroy()
public void dataSourceDestroy() throws SQLException {
    rollbackUnCommittedTransaction();
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM