工作项目中使用了@EnableRedisHttpSession
今天刚好有空,准备了解一下这个注解。
首先看源码:
```java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(RedisHttpSessionConfiguration.class)
@Configuration
public @interface EnableRedisHttpSession {
/**
* The session timeout in seconds. By default, it is set to 1800 seconds (30 minutes).
* This should be a non-negative integer.
* @return the seconds a session can be inactive before expiring
*/
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
/**
* Defines a unique namespace for keys. The value is used to isolate sessions by
* changing the prefix from default {@code spring:session:} to
* {@code <redisNamespace>:}.
* <p>
* For example, if you had an application named "Application A" that needed to keep
* the sessions isolated from "Application B" you could set two different values for
* the applications and they could function within the same Redis instance.
* @return the unique namespace for keys
*/
String redisNamespace() default RedisOperationsSessionRepository.DEFAULT_NAMESPACE;
/**
* Flush mode for the Redis sessions. The default is {@code ON_SAVE} which only
* updates the backing Redis when {@link SessionRepository#save(Session)} is invoked.
* In a web environment this happens just before the HTTP response is committed.
* <p>
* Setting the value to {@code IMMEDIATE} will ensure that the any updates to the
* Session are immediately written to the Redis instance.
* @return the {@link RedisFlushMode} to use
* @since 1.1
*/
RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE;
/**
* The cron expression for expired session cleanup job. By default runs every minute.
* @return the session cleanup cron expression
* @since 2.0.0
*/
String cleanupCron() default RedisHttpSessionConfiguration.DEFAULT_CLEANUP_CRON;
官方给的注释已经很明白了,分别是:
- 会话超时时间
会话超时时间,单位为秒。缺省值为1800秒(30分钟)。
这应该是一个非负整数。 - 命名空间
为键定义一个唯一的命名空间。该值用于会话隔离,将前缀从默认的{@code spring:session:} 更改为 {@code redisNamespace:}。
例如,如果您有一个名为“应用程序A”的应用程序需要保存
*会话隔离“应用程序B”,你可以设置两个不同的值
*这些应用程序和它们可以在同一个Redis实例中运行。 - 刷新模式
刷新模式为Redis会话。默认值是{@code ON_SAVE},这只是当调用{@link SessionRepository#save(Session)}时,更新支持的Redis。在web环境中,这发生在HTTP响应提交之前。
设置值为{@code IMMEDIATE}将确保任何更新
Session被立即写入Redis实例。 - 过期会话自动清理的cron表达式
过期会话清理作业的cron表达式。默认情况下每分钟运行一次。
ok ,来看@Import(RedisHttpSessionConfiguration.class)
@Configuration
@EnableScheduling
public class RedisHttpSessionConfiguration extends SpringHttpSessionConfiguration
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware,
SchedulingConfigurer {
此处继承了父类SpringHttpSessionConfiguration
@Bean
public <S extends Session> SessionRepositoryFilter<? extends Session> springSessionRepositoryFilter(
SessionRepository<S> sessionRepository) {
SessionRepositoryFilter<S> sessionRepositoryFilter = new SessionRepositoryFilter<>(
sessionRepository);
sessionRepositoryFilter.setServletContext(this.servletContext);
sessionRepositoryFilter.setHttpSessionIdResolver(this.httpSessionIdResolver);
return sessionRepositoryFilter;
}
麻了 有个紧急需求 先这样吧,后续有空了接着写