Java Spring 3.5 如何将 HttpSessionEventPublisher 添加到我的启动配置中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24955534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.5 how to add HttpSessionEventPublisher to my boot configuration
提问by Alexandr
I want to listen to session life cycle events. I read about adding
我想收听会话生命周期事件。我读过关于添加
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
to web.xml. But I don't have it. I am using class that extends SpringBootServletInitializer. How I can add this listener?
到 web.xml。但我没有。我正在使用扩展 SpringBootServletInitializer 的类。如何添加此侦听器?
回答by Swaraj
Adding Listener in Class which is extending SpringBootSelvletInitializer can be done as below.
在扩展 SpringBootSelvletInitializer 的类中添加监听器可以如下完成。
@Configuration
public class Application extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
SpringApplicationBuilder app=application.sources(Application.class, ComponentConfiguration.class);
app.listeners(bootstrapContext.commandLineListener());
return app;
}
As the builder class is having listener method which use to add all the listener which is given to be registered. Github link for SpringApplicationBuilder is http://goo.gl/EGj6jE
由于构建器类具有用于添加所有要注册的侦听器的侦听器方法。SpringApplicationBuilder 的 Github 链接是http://goo.gl/EGj6jE
I think this will solve your issue.
我认为这将解决您的问题。
Swaraj
斯瓦拉吉
回答by axtavt
You can use ServletListenerRegistrationBean:
您可以使用ServletListenerRegistrationBean:
@Bean
public ServletListenerRegistrationBean<HttpSessionEventPublisher> httpSessionEventPublisher() {
return new ServletListenerRegistrationBean<HttpSessionEventPublisher>(new HttpSessionEventPublisher());
}
回答by Serge Ballesta
From SpringBootServletInitializerjavadoc : A handy opinionated WebApplicationInitializer for applications that only have one Spring servlet, and no more than a single filter (which itself is only enabled when Spring Security is detected). If your application is more complicated consider using one of the other WebApplicationInitializers
来自SpringBootServletInitializerjavadoc :一个方便的、自以为是的 WebApplicationInitializer,适用于只有一个 Spring servlet 且不超过一个过滤器(它本身仅在检测到 Spring Security 时才启用)的应用程序。如果您的应用程序更复杂,请考虑使用其他 WebApplicationInitializers 之一
So if you want to generate a war and you want to include a session listener, you should use directly a WebApplicationInitializer. Here is an example from the javadoc :
因此,如果您想生成War并希望包含会话侦听器,则应直接使用WebApplicationInitializer. 这是 javadoc 中的一个示例:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
As you have full control on the ServletContext before it is fully initialized, it is easy to add your listener :
由于您在完全初始化之前对 ServletContext 拥有完全控制权,因此很容易添加您的侦听器:
container.addListener(YourListener.class);
回答by Shrikant Havale
Just to provide reference to official documentation, http://docs.spring.io/spring-session/docs/current-SNAPSHOT/reference/html5/#httpsession-rest
仅供参考官方文档, http://docs.spring.io/spring-session/docs/current-SNAPSHOT/reference/html5/#httpsession-rest
Here if you refer to, HttpSessionListenertopic, you will find the clear example of doing it both Javaand XMLconfiguration way.
在这里,如果您参考HttpSessionListener主题,您将找到同时执行此操作Java和XML配置方式的清晰示例。
if your configuration support Redis
如果您的配置支持 Redis
@Configuration
@EnableRedisHttpSession
public class RedisHttpSessionConfig {
@Bean
public HttpSessionEventPublisher httpSessionEventPublisher() {
return new HttpSessionEventPublisher();
}
// ...
}
In XML configuration, this might look like
在 XML 配置中,这可能看起来像
<bean class="org.springframework.security.web.session.HttpSessionEventPublisher"/>

