Avoid inheritance in configuration classes

This commit restructures configuration classes to avoid inheritance, where possible. This should provide more flexibility when composing custom configurations.

Closes gh-1415
This commit is contained in:
Vedran Pavic
2022-10-17 19:33:21 +02:00
committed by Rob Winch
parent fc81049cbe
commit 668f85788a
8 changed files with 24 additions and 16 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
@@ -51,8 +52,9 @@ import org.springframework.util.Assert;
* @see SpringSessionRedisConnectionFactory
*/
@Configuration(proxyBeanMethods = false)
@Import(SpringHttpSessionConfiguration.class)
public abstract class AbstractRedisHttpSessionConfiguration<T extends SessionRepository<? extends Session>>
extends SpringHttpSessionConfiguration implements BeanClassLoaderAware {
implements BeanClassLoaderAware {
private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;

View File

@@ -28,6 +28,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
@@ -57,8 +58,8 @@ import org.springframework.web.server.session.WebSessionManager;
* @see EnableRedisWebSession
*/
@Configuration(proxyBeanMethods = false)
public class RedisWebSessionConfiguration extends SpringWebSessionConfiguration
implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
@Import(SpringWebSessionConfiguration.class)
public class RedisWebSessionConfiguration implements BeanClassLoaderAware, EmbeddedValueResolverAware, ImportAware {
private Duration maxInactiveInterval = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL;

View File

@@ -191,7 +191,7 @@ class RedisWebSessionConfigurationTests {
void multipleConnectionFactoryRedisConfig() {
assertThatExceptionOfType(BeanCreationException.class)
.isThrownBy(() -> registerAndRefresh(RedisConfig.class, MultipleConnectionFactoryRedisConfig.class))
.withCauseInstanceOf(NoUniqueBeanDefinitionException.class).havingCause()
.havingRootCause().isInstanceOf(NoUniqueBeanDefinitionException.class)
.withMessageContaining("expected single matching bean but found 2");
}