如何为应用程序上下文初始化事件添加钩子?
对于常规 Servlet,我想您可以声明一个 上下文侦听器,但是对于Spring MVC Spring 会让这变得更容易吗?
此外,如果我定义了一个上下文侦听器,然后需要访问 servlet.xml
或 applicationContext.xml
中定义的 bean,我将如何访问它们?
For a regular Servlet, I guess you could declare a context listener, but for Spring MVC would Spring make this any easier?
Furthermore, if I define a context listener and then would need to access the beans defined in my servlet.xml
or applicationContext.xml
, how would I get access to them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Spring 有一些您可以处理的标准事件。< /strong>
为此,您必须创建并注册一个实现
ApplicationListener
接口的 bean,如下所示:然后在
servlet.xml 或
applicationContext.xml
文件:Spring 会在应用程序上下文初始化时通知它。
在 Spring 3 中(如果您使用的是此版本),
ApplicationListener
类是通用的,您可以声明您感兴趣的事件类型,并且事件将被相应地过滤。你可以像这样简化你的 bean 代码:Spring has some standard events which you can handle.
To do that, you must create and register a bean that implements the
ApplicationListener
interface, something like this:You then register this bean within your
servlet.xml
orapplicationContext.xml
file:and Spring will notify it when the application context is initialized.
In Spring 3 (if you are using this version), the
ApplicationListener
class is generic and you can declare the event type that you are interested in, and the event will be filtered accordingly. You can simplify a bit your bean code like this:从 Spring 4.2 开始,您可以使用
@EventListener
(文档)Since Spring 4.2 you can use
@EventListener
(documentation)创建注释
创建类
通过 @Component 注释或在 xml 中注册此类
,并在上下文初始化后想要运行的任何方法上使用注释,例如:
Create your annotation
Create class
Register this class by @Component annotation or in xml
and use annotation where you wan on any method that you want to run after context initialized, like:
请按照以下步骤在应用程序上下文加载后进行一些处理,即应用程序准备好提供服务。
创建下面的注释即
@Retention(RetentionPolicy.RUNTIME)
@Target(值= {ElementType.METHOD, ElementType.TYPE})
public @interface AfterApplicationReady {}
2.创建下面的类,它是一个在应用程序就绪状态下调用的监听器。
最后,当您在日志声明应用程序启动之前启动 Spring 应用程序时,您的侦听器将被调用。
Please follow below step to do some processing after Application Context get loaded i.e application is ready to serve.
Create below annotation i.e
@Retention(RetentionPolicy.RUNTIME)
@Target(value= {ElementType.METHOD, ElementType.TYPE})
public @interface AfterApplicationReady {}
2.Create Below Class which is a listener which get call on application ready state.
Finally when you start your Spring application just before log stating application started your listener will be called.
您的 bean 还可以实现
SmartInitializingSingleton.afterSingletonsInstantiated()
。这是 Spring 文档中列出的可能性之一(当前状态:v 6.0.12):https:// docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-lifecycle-initializingbean
Your bean can also implement
SmartInitializingSingleton.afterSingletonsInstantiated()
. This is one of the possibilities, listed in the Spring documentation (current state: v 6.0.12):https://docs.spring.io/spring-framework/reference/core/beans/factory-nature.html#beans-factory-lifecycle-initializingbean
我有一个单页应用程序,在输入 URL 时它正在创建一个 HashMap(由我的网页使用),其中包含来自多个数据库的数据。
我做了以下事情来在服务器启动时加载所有内容 -
1- 创建 ContextListenerClass
2- 在 web.xml 中添加以下条目
3- 在我的控制器类中更新代码以首先检查 servletContext 中的 Map
当我启动 Tomcat 时发生了很大的变化在 startTime 期间加载 dataMap 并将所有内容放入 servletContext 中,然后控制器类使用该内容从已填充的 servletContext 中获取结果。
I had a single page application on entering URL it was creating a HashMap (used by my webpage) which contained data from multiple databases.
I did following things to load everything during server start time-
1- Created ContextListenerClass
2- Added below entry in web.xml
3- In my Controller Class updated code to first check for Map in servletContext
With this much change when I start my tomcat it loads dataMap during startTime and puts everything in servletContext which is then used by Controller Class to get results from already populated servletContext .