Which event is the right event to listen to cache the data for my application?

huangapple go评论64阅读模式
英文:

Out of so many events provided by spring - Which event is the right event to listen to cache the data for my application

问题

在我的Spring应用程序中,我想要缓存一些数据。为了完成这个任务,我实现了一个监听器来监听contextRefreshEvents事件。唯一的问题是,Spring何时触发此事件以及触发多少次,因为我不希望我的数据再次被缓存。

当我查阅了一些链接,比如:https://www.baeldung.com/spring-context-events
上面提到Spring将在“初始化或刷新ApplicationContext时”引发此事件。初始化没问题,因为这仅发生在我们启动服务器时,所以这个事件只会触发一次,但我想了解更多关于Application上下文刷新部分的信息。
应用程序上下文将在何时刷新?

英文:

In my spring application i want to cache some data. To accomplish this task i implemented a listener for the event contextRefreshEvents. The only catch is that when this event will be fired by spring and how many times, because i dont want my data to be cached again.

When i went through some of the links like : https://www.baeldung.com/spring-context-events
it is mentioned that the spring will raise this event during "initializing or refreshing the ApplicationContext". Initialization is fine because this happens when we start the server and so this event will be triggered only once, but i wanted more info about the Application context refreshing part.
When the application context will be refreshed?

答案1

得分: 0

刷新一个上下文在Spring中各种应用到应用是不同的。
如果你正在使用Spring Boot,那么刷新上下文是不可能的,因为它是一个不可刷新的上下文。

在其他情况下,如果你正在使用基于XML的上下文加载,那么在运行时如果你要修改一些类并希望将修改后的类加载到正在运行的应用程序中,你可以移除该类的现有上下文,然后添加新的上下文。这是其中的一部分。
如果你认为有很多东西会发生改变,那么可以考虑使用刷新。

还有一件事,如果你使用了刷新,它不会影响现有的@Autowired注入的内容,所以要小心。
在继续之前,请查看他们的文档以了解各种可刷新的上下文。

英文:

Refreshing a context in spring various to application to application.
If you are using spring-boot then its not possible to refresh the context since it a non refreshable context.

In other cases you can if you are using Xmlbasedcontext loading. During the run time if you are altering some classes and wants to load that modified classes as apart of your running app means you can remove the existing context of that class and add the new one. so this is one part.
If you think that so many stuffs are going to change then prop you can use refresh.

And one more thing if you use refresh then it wont affect the existing @Autowired stuffs. so be careful on that.
Checkout their docs for various refreshable context before proceeding.

huangapple
  • 本文由 发表于 2020年8月7日 15:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定