-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: add multiple conditionals to prevent Bean clashes #91
Spring.session.store-type was removed to follow the Spring Boot 3 way. The RedisConfiguration is dependent on Redis being on the classpath, so this property is redundant anyway.
- Loading branch information
Showing
7 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...n/kotlin/io/cloudflight/platform/spring/caching/autoconfigure/SessionAutoConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.cloudflight.platform.spring.caching.autoconfigure | ||
|
||
import io.cloudflight.platform.spring.caching.serializer.SafeRedisSessionSerializer | ||
import org.springframework.boot.autoconfigure.AutoConfiguration | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean | ||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Import | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.core.RedisOperations | ||
import org.springframework.data.redis.serializer.RedisSerializer | ||
import org.springframework.session.SessionRepository | ||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession | ||
|
||
@AutoConfiguration(after = [CachingAutoConfiguration::class], before = [RedisAutoConfiguration::class]) | ||
@Import(SessionAutoConfiguration.RedisSessionConfig::class) | ||
class SessionAutoConfiguration { | ||
|
||
@Configuration | ||
@ConditionalOnClass(RedisOperations::class) | ||
@ConditionalOnBean(RedisConnectionFactory::class) | ||
@ConditionalOnMissingBean(SessionRepository::class) | ||
@EnableRedisHttpSession | ||
class RedisSessionConfig | ||
|
||
@Bean | ||
@ConditionalOnClass(RedisOperations::class) | ||
@ConditionalOnBean(SessionRepository::class) | ||
fun springSessionDefaultRedisSerializer(): RedisSerializer<*> { | ||
return SafeRedisSessionSerializer(RedisSerializer.java()) | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
io.cloudflight.platform.spring.caching.autoconfigure.CachingAutoConfiguration | ||
io.cloudflight.platform.spring.caching.autoconfigure.CachingAutoConfiguration | ||
io.cloudflight.platform.spring.caching.autoconfigure.SessionAutoConfiguration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...ng/src/test/kotlin/io/cloudflight/platform/spring/caching/SessionAutoConfigurationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package io.cloudflight.platform.spring.caching | ||
|
||
import io.cloudflight.platform.spring.caching.autoconfigure.SessionAutoConfiguration | ||
import io.cloudflight.platform.spring.caching.serializer.SafeRedisSessionSerializer | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito.mock | ||
import org.springframework.boot.autoconfigure.AutoConfigurations | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner | ||
import org.springframework.cache.CacheManager | ||
import org.springframework.cache.annotation.EnableCaching | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.data.redis.cache.RedisCacheManager | ||
import org.springframework.data.redis.connection.RedisConnectionFactory | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.data.redis.serializer.RedisSerializer | ||
import org.springframework.session.SessionRepository | ||
import org.springframework.session.data.redis.RedisIndexedSessionRepository | ||
import org.springframework.session.data.redis.RedisSessionRepository | ||
|
||
class SessionAutoConfigurationTest { | ||
|
||
private val contextRunner = ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration::class.java)) | ||
|
||
@Test | ||
fun defaultRedisHttpSession() { | ||
this.contextRunner.withUserConfiguration(BasicRedisConfiguration::class.java) | ||
.run { context -> | ||
Assertions.assertThat(context).hasSingleBean(SessionRepository::class.java) | ||
Assertions.assertThat(context).getBean("sessionRepository") | ||
.isInstanceOf(RedisSessionRepository::class.java) | ||
} | ||
} | ||
|
||
@Test | ||
fun redisHttpSessionBackOff() { | ||
this.contextRunner.withUserConfiguration(CustomRedisIndexedHttpSessionConfiguration::class.java) | ||
.run { context -> | ||
Assertions.assertThat(context).hasSingleBean(SessionRepository::class.java) | ||
Assertions.assertThat(context).getBean("sessionRepository") | ||
.isInstanceOf(RedisIndexedSessionRepository::class.java) | ||
} | ||
} | ||
|
||
@Test | ||
fun safeRedisSessionSerializerWithDefaultSessionRepository() { | ||
this.contextRunner.withUserConfiguration(BasicRedisConfiguration::class.java) | ||
.run { context -> | ||
Assertions.assertThat(context).hasSingleBean(RedisSerializer::class.java) | ||
Assertions.assertThat(context).getBean("springSessionDefaultRedisSerializer") | ||
.isInstanceOf(SafeRedisSessionSerializer::class.java) | ||
|
||
} | ||
} | ||
|
||
@Test | ||
fun safeRedisSessionSerializerWithUserSessionRepository() { | ||
this.contextRunner.withUserConfiguration(CustomRedisIndexedHttpSessionConfiguration::class.java) | ||
.run { context -> | ||
Assertions.assertThat(context).hasSingleBean(RedisSerializer::class.java) | ||
Assertions.assertThat(context).getBean("springSessionDefaultRedisSerializer") | ||
.isInstanceOf(SafeRedisSessionSerializer::class.java) | ||
|
||
} | ||
} | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@EnableCaching | ||
private class BasicRedisConfiguration { | ||
@Bean | ||
fun redisConnectionFactory(): RedisConnectionFactory { | ||
return mock(RedisConnectionFactory::class.java) | ||
} | ||
|
||
@Bean | ||
fun cacheManager(connectionFactory: RedisConnectionFactory): CacheManager { | ||
return RedisCacheManager.create(connectionFactory) | ||
} | ||
} | ||
|
||
|
||
@Configuration(proxyBeanMethods = false) | ||
private class CustomRedisIndexedHttpSessionConfiguration { | ||
|
||
@Bean | ||
fun redisTemplate(): RedisTemplate<*, *>? { | ||
return mock(RedisTemplate::class.java) | ||
} | ||
|
||
@Bean | ||
fun sessionRepository(): RedisIndexedSessionRepository { | ||
return mock(RedisIndexedSessionRepository::class.java) | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...form-spring-bom/platform-spring-caching/src/test/resources/application-testcontainer.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
spring: | ||
session: | ||
store-type: redis | ||
cache: | ||
redis: | ||
time-to-live: PT5M | ||
time-to-live: PT5M |