Reputation: 207
For my spring-boot application (2.3.3.RELEASE, Java 11) I uses spring session, here is a standard dependencies for this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
The application works as expected, sessions stores in redis and managed by spring as well.
But I want to delete sessions by id from Redis manually. For this I noticed that spring has a bean for session repository in RedisHttpSessionConfiguration (line 119):
@Bean
public RedisIndexedSessionRepository sessionRepository() {
And I decided to reuse it. Unfortunately, when I try that in my service like:
@Service
@RequiredArgsConstructor
public class SessionService {
private final RedisIndexedSessionRepository sessionRepository;
the following error appears while application starts:
Failed to instantiate [org.springframework.session.data.redis.RedisIndexedSessionRepository]: Circular reference involving containing bean 'org.springframework.boot.autoconfigure.session.RedisSessionConfiguration$SpringBootRedisHttpSessionConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRepository' threw exception; nested exception is java.lang.IllegalStateException: RedisConnectionFactory is required
UPD. I found that if I add RedisIndexedSessionRepository to my service Spring somehow doesn't execute the code responsible for setting redisConnectionFactory in RedisHttpSessionConfiguration (autowired method setRedisConnectionFactory, line 204). But if I remove RedisIndexedSessionRepository from my service the above method are triggered. I also tried to create my own bean RedisConnectionFactory but it doesn't fix this.
Ahh, I found this comment in RedisHttpSessionConfiguration on top:
* Exposes the {@link SessionRepositoryFilter} as a bean named
* {@code springSessionRepositoryFilter}. In order to use this a single
* {@link RedisConnectionFactory} must be exposed as a Bean.
but still don't understand how to use RedisIndexedSessionRepository in service.
Could someone advice how to handle that and how to use this repository in application?
Upvotes: 1
Views: 2556
Reputation: 207
It seems that RedisIndexedSessionRepository cannot be simply autowired and we have do that manually like:
private final RedisIndexedSessionRepository sessionRepository;
public SessionService(RedisTemplate<Object, Object> redisTemplate) {
this.sessionRepository = new RedisIndexedSessionRepository(redisTemplate);
}
Upvotes: 1