Polish configuration classes

This commit is contained in:
Vedran Pavic
2017-11-03 07:11:00 +01:00
parent f5912da089
commit 17e56dda18
9 changed files with 155 additions and 137 deletions

View File

@@ -22,17 +22,20 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.sql.DataSource;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.session.MapSession;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.web.http.SessionRepositoryFilter;
/**
* Add this annotation to an {@code @Configuration} class to expose the
* SessionRepositoryFilter as a bean named "springSessionRepositoryFilter" and backed by a
* relational database. In order to leverage the annotation, a single
* {@link javax.sql.DataSource} must be provided. For example:
* {@link SessionRepositoryFilter} as a bean named {@code springSessionRepositoryFilter}
* and backed by a relational database. In order to leverage the annotation, a single
* {@link DataSource} must be provided. For example:
*
* <pre class="code">
* &#064;Configuration
@@ -73,22 +76,23 @@ import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
@Configuration
public @interface EnableJdbcHttpSession {
/**
* The session timeout in seconds. By default, it is set to 1800 seconds (30 minutes).
* This should be a non-negative integer.
* @return the seconds a session can be inactive before expiring
*/
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
/**
* The name of database table used by Spring Session to store sessions.
* @return the database table name
*/
String tableName() default JdbcOperationsSessionRepository.DEFAULT_TABLE_NAME;
/**
* The session timeout in seconds. By default, it is set to 1800 seconds (30
* minutes). This should be a non-negative integer.
* @return the seconds a session can be inactive before expiring
*/
int maxInactiveIntervalInSeconds() default MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
/**
* The cron expression for expired session cleanup job. By default runs every minute.
* @return the session cleanup cron expression
* @since 2.0.0
*/
String cleanupCron() default JdbcHttpSessionConfiguration.DEFAULT_CLEANUP_CRON;

View File

@@ -42,17 +42,18 @@ import org.springframework.session.MapSession;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.jdbc.JdbcOperationsSessionRepository;
import org.springframework.session.jdbc.config.annotation.SpringSessionDataSource;
import org.springframework.session.web.http.SessionRepositoryFilter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
/**
* Spring @Configuration class used to configure and initialize a JDBC based HttpSession
* provider implementation in Spring Session.
* Spring {@code @Configuration} class used to configure and initialize a JDBC based
* {@code HttpSession} provider implementation in Spring Session.
* <p>
* Exposes the {@link org.springframework.session.web.http.SessionRepositoryFilter} as a
* bean named "springSessionRepositoryFilter". In order to use this a single
* {@link DataSource} must be exposed as a Bean.
* Exposes the {@link SessionRepositoryFilter} as a bean named
* {@code springSessionRepositoryFilter}. In order to use this a single {@link DataSource}
* must be exposed as a Bean.
*
* @author Vedran Pavic
* @author Eddú Meléndez
@@ -67,10 +68,10 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
private String tableName = JdbcOperationsSessionRepository.DEFAULT_TABLE_NAME;
private Integer maxInactiveIntervalInSeconds = MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS;
private String tableName = JdbcOperationsSessionRepository.DEFAULT_TABLE_NAME;
private String cleanupCron = DEFAULT_CLEANUP_CRON;
private DataSource dataSource;
@@ -112,14 +113,14 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
return sessionRepository;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setMaxInactiveIntervalInSeconds(Integer maxInactiveIntervalInSeconds) {
this.maxInactiveIntervalInSeconds = maxInactiveIntervalInSeconds;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setCleanupCron(String cleanupCron) {
this.cleanupCron = cleanupCron;
}
@@ -173,16 +174,16 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
Map<String, Object> attributeMap = importMetadata
.getAnnotationAttributes(EnableJdbcHttpSession.class.getName());
AnnotationAttributes attributes = AnnotationAttributes.fromMap(attributeMap);
this.maxInactiveIntervalInSeconds = attributes
.getNumber("maxInactiveIntervalInSeconds");
String tableNameValue = attributes.getString("tableName");
if (StringUtils.hasText(tableNameValue)) {
this.tableName = this.embeddedValueResolver
.resolveStringValue(tableNameValue);
}
this.maxInactiveIntervalInSeconds = attributes
.getNumber("maxInactiveIntervalInSeconds");
String cleanupCron = attributes.getString("cleanupCron");
if (StringUtils.hasText(cleanupCron)) {
this.cleanupCron = this.embeddedValueResolver.resolveStringValue(cleanupCron);
this.cleanupCron = cleanupCron;
}
}