RedisOperationsSessionRepository
uml 图
MessageListener是redis消息订阅的监听接口,
SessionMessageListener是过时的,已经被
RedisOperationsSessionRepository所代替,可以从源码中看到源码分析
public class RedisOperationsSessionRepository implements FindByIndexNameSessionRepository<RedisOperationsSessionRepository.RedisSession>, MessageListener { private static final Log logger = LogFactory .getLog(RedisOperationsSessionRepository.class); private static final String SPRING_SECURITY_CONTEXT = "SPRING_SECURITY_CONTEXT"; static PrincipalNameResolver PRINCIPAL_NAME_RESOLVER = new PrincipalNameResolver(); /** * The default prefix for each key and channel in Redis used by Spring Session. * 默认key和channel的前缀 */ static final String DEFAULT_SPRING_SESSION_REDIS_PREFIX = "spring:session:"; /** * The key in the Hash representing * {@link org.springframework.session.ExpiringSession#getCreationTime()}. */ static final String CREATION_TIME_ATTR = "creationTime"; /** * The key in the Hash representing * {@link org.springframework.session.ExpiringSession#getMaxInactiveIntervalInSeconds()} * . */ static final String MAX_INACTIVE_ATTR = "maxInactiveInterval"; /** * The key in the Hash representing * {@link org.springframework.session.ExpiringSession#getLastAccessedTime()}. */ static final String LAST_ACCESSED_ATTR = "lastAccessedTime"; /** * The prefix of the key for used for session attributes. The suffix is the name of * the session attribute. For example, if the session contained an attribute named * attributeName, then there would be an entry in the hash named * sessionAttr:attributeName that mapped to its value. * session属性sessionAttr:attributeName */ static final String SESSION_ATTR_PREFIX = "sessionAttr:"; /** * The prefix for every key used by Spring Session in Redis. */ private String keyPrefix = DEFAULT_SPRING_SESSION_REDIS_PREFIX; //从uml图中可以看出这是redis模板操作的接口 private final RedisOperations<Object, Object> sessionRedisOperations; /** redis session过期策略处理类。RedisSessionExpirationPolicy与RedisOperationsSessionRepository相互引用, RedisOperationsSessionRepository去通知RedisSessionExpirationPolicy做相关的操作,同时RedisSessionExpirationPolicy从RedisOperationsSessionRepository拉取相关的信息。特别像观察者模式中的“拉操作”,可以认为是简版的观察者模式 **/ private final RedisSessionExpirationPolicy expirationPolicy; /** * 事件发布 **/ private ApplicationEventPublisher eventPublisher = new ApplicationEventPublisher() { public void publishEvent(ApplicationEvent event) { } public void publishEvent(Object event) { } }; /** * If non-null, this value is used to override the default value for * {@link RedisSession#setMaxInactiveIntervalInSeconds(int)}. */ private Integer defaultMaxInactiveInterval; //默认redis序列化使用的jdk序列化方式 private RedisSerializer<Object> defaultSerializer = new JdkSerializationRedisSerializer(); private RedisFlushMode redisFlushMode = RedisFlushMode.ON_SAVE; /** * Allows creating an instance and uses a default {@link RedisOperations} for both * managing the session and the expirations. * * @param redisConnectionFactory the {@link RedisConnectionFactory} to use. */ public RedisOperationsSessionRepository( RedisConnectionFactory redisConnectionFactory) { this(createDefaultTemplate(redisConnectionFactory)); } /** * Creates a new instance. For an example, refer to the class level javadoc. * * @param sessionRedisOperations The {@link RedisOperations} to use for managing the * sessions. Cannot be null. */ public RedisOperationsSessionRepository( RedisOperations<Object, Object> sessionRedisOperations) { Assert.notNull(sessionRedisOperations, "sessionRedisOperations cannot be null"); this.sessionRedisOperations = sessionRedisOperations; this.expirationPolicy = new RedisSessionExpirationPolicy(sessionRedisOperations, this); } /** * Sets the {@link ApplicationEventPublisher} that is used to publish * {@link SessionDestroyedEvent}. The default is to not publish a * {@link SessionDestroyedEvent}. * * @param applicationEventPublisher the {@link ApplicationEventPublisher} that is used * to publish {@link SessionDestroyedEvent}. Cannot be null. */ public void setApplicationEventPublisher( ApplicationEventPublisher applicationEventPublisher) { Assert.notNull(applicationEventPublisher,