Skip to content

Ensure configuration classes can be used with @Import #2168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ void customIndexResolverConfigurationWithProvidedMongoSessionConverter() {
indexResolver);
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
MongoIndexedSessionRepository sessionRepository = this.context.getBean(MongoIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {

this.context.register(annotatedClasses);
Expand Down Expand Up @@ -333,4 +340,15 @@ IndexResolver<MongoSession> indexResolver() {

}

@Configuration(proxyBeanMethods = false)
@Import(MongoHttpSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration extends BaseConfiguration {

@Bean
SessionRepositoryCustomizer<MongoIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.index.IndexOperations;
Expand Down Expand Up @@ -220,6 +221,15 @@ void customIndexResolverConfigurationWithProvidedMongoSessionConverter() {
indexResolver);
}

@Test
void importConfigAndCustomize() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(ImportConfigAndCustomizeConfiguration.class);
this.context.refresh();
ReactiveMongoSessionRepository sessionRepository = this.context.getBean(ReactiveMongoSessionRepository.class);
assertThat(sessionRepository).extracting("maxInactiveIntervalInSeconds").isEqualTo(0);
}

/**
* Reflectively extract the {@link AbstractMongoSessionConverter} from the
* {@link ReactiveMongoSessionRepository}. This is to avoid expanding the surface area
Expand Down Expand Up @@ -393,4 +403,20 @@ IndexResolver<MongoSession> indexResolver() {

}

@Configuration(proxyBeanMethods = false)
@Import(ReactiveMongoWebSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration {

@Bean
ReactiveMongoOperations operations() {
return mock(ReactiveMongoOperations.class);
}

@Bean
ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setMaxInactiveIntervalInSeconds(0);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> attributeMap = importMetadata
.getAnnotationAttributes(EnableRedisWebSession.class.getName());
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
if (attributes == null) {
return;
}
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
String redisNamespaceValue = attributes.getString("redisNamespace");
if (StringUtils.hasText(redisNamespaceValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -198,6 +199,13 @@ void sessionRepositoryCustomizer() {
Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
RedisSessionRepository sessionRepository = this.context.getBean(RedisSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(Duration.ZERO);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand Down Expand Up @@ -362,4 +370,15 @@ SessionRepositoryCustomizer<RedisSessionRepository> sessionRepositoryCustomizerT

}

@Configuration(proxyBeanMethods = false)
@Import(RedisHttpSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration {

@Bean
SessionRepositoryCustomizer<RedisSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(Duration.ZERO);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -231,6 +232,13 @@ void sessionRepositoryCustomizer() {
MAX_INACTIVE_INTERVAL_IN_SECONDS);
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
RedisIndexedSessionRepository sessionRepository = this.context.getBean(RedisIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand Down Expand Up @@ -424,4 +432,15 @@ SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCust

}

@Configuration(proxyBeanMethods = false)
@Import(RedisIndexedHttpSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration {

@Bean
SessionRepositoryCustomizer<RedisIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
Expand Down Expand Up @@ -225,6 +226,13 @@ void sessionRepositoryCustomizer() {
MAX_INACTIVE_INTERVAL_IN_SECONDS);
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(RedisConfig.class, ImportConfigAndCustomizeConfiguration.class);
ReactiveRedisSessionRepository sessionRepository = this.context.getBean(ReactiveRedisSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand Down Expand Up @@ -381,4 +389,15 @@ ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepos

}

@Configuration(proxyBeanMethods = false)
@Import(RedisWebSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration {

@Bean
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> attributeMap = importMetadata
.getAnnotationAttributes(EnableHazelcastHttpSession.class.getName());
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
if (attributes == null) {
return;
}
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
String sessionMapNameValue = attributes.getString("sessionMapName");
if (StringUtils.hasText(sessionMapNameValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;
import org.springframework.session.FlushMode;
Expand Down Expand Up @@ -230,6 +231,14 @@ void sessionRepositoryCustomizer() {
MAX_INACTIVE_INTERVAL_IN_SECONDS);
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(ImportConfigAndCustomizeConfiguration.class);
HazelcastIndexedSessionRepository sessionRepository = this.context
.getBean(HazelcastIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand Down Expand Up @@ -446,4 +455,15 @@ SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepository

}

@Configuration(proxyBeanMethods = false)
@Import(HazelcastHttpSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration extends BaseConfiguration {

@Bean
SessionRepositoryCustomizer<HazelcastIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> attributeMap = importMetadata
.getAnnotationAttributes(EnableJdbcHttpSession.class.getName());
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
if (attributes == null) {
return;
}
this.maxInactiveIntervalInSeconds = attributes.getNumber("maxInactiveIntervalInSeconds");
String tableNameValue = attributes.getString("tableName");
if (StringUtils.hasText(tableNameValue)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -310,6 +311,13 @@ void defaultConfigurationJdbcTemplateHasExpectedExceptionTranslator() {
assertThat(jdbcTemplate.getExceptionTranslator()).isInstanceOf(SQLErrorCodeSQLExceptionTranslator.class);
}

@Test
void importConfigAndCustomize() {
registerAndRefresh(DataSourceConfiguration.class, ImportConfigAndCustomizeConfiguration.class);
JdbcIndexedSessionRepository sessionRepository = this.context.getBean(JdbcIndexedSessionRepository.class);
assertThat(sessionRepository).extracting("defaultMaxInactiveInterval").isEqualTo(0);
}

private void registerAndRefresh(Class<?>... annotatedClasses) {
this.context.register(annotatedClasses);
this.context.refresh();
Expand Down Expand Up @@ -556,4 +564,15 @@ SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCusto

}

@Configuration(proxyBeanMethods = false)
@Import(JdbcHttpSessionConfiguration.class)
static class ImportConfigAndCustomizeConfiguration {

@Bean
SessionRepositoryCustomizer<JdbcIndexedSessionRepository> sessionRepositoryCustomizer() {
return (sessionRepository) -> sessionRepository.setDefaultMaxInactiveInterval(0);
}

}

}