将 spring 会话 JDBC 与现有数据库一起使用(不是 springboot)
Use spring session JDBC with existing database (not springboot)
我的应用程序 运行 spring web mvc 框架没有 spring 引导。现在我想使用 spring session JDBC 将会话存储到应用程序使用的数据库中。我在网上找到的所有例子都是使用 spring 启动,如果不使用 spring 启动,他们使用的数据源配置是 EmbeddedDatabase
像这样:
@Bean
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("org/springframework/session/jdbc/schema-h2.sql").build();
}
我的数据源配置使用 HikariCP,我希望 spring 会话使用此数据源配置。
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
config.setJdbcUrl(env.getRequiredProperty("jdbc.url"));
config.setUsername(env.getRequiredProperty("jdbc.username"));
config.setPassword(env.getRequiredProperty("jdbc.password"));
config.setMinimumIdle(env.getRequiredProperty("jdbc.pool.minimumIdle", Integer.class));
config.setMaximumPoolSize(env.getRequiredProperty("jdbc.pool.maximumPoolSize", Integer.class));
config.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("jdbc.prop.cachePrepStmts"));
config.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("jdbc.prop.prepStmtCacheSize"));
config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("jdbc.prop.prepStmtCacheSqlLimit"));
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
如何使用当前配置与 spring 会话集成?
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager () {
EntityManagerFactory factory = entityManagerFactory();
return new JpaTransactionManager(factory);
}
@Bean
public EntityManagerFactory entityManagerFactory () {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(Boolean.TRUE);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.your.domain.project");
factory.setDataSource(dataSource());
factory.setJpaProperties(additionalProperties()); // any addtional properties of your ORM
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory.getObject();
}
@Bean
public DataSource dataSource () {
final com.mchange.v2.c3p0.ComboPooledDataSource comboDataSource = new ComboPooledDataSource();
try {
comboDataSource.setDriverClass(env.getProperty("jdbc.driver"));
comboDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
comboDataSource.setUser(env.getProperty("jdbc.user"));
comboDataSource.setPassword(env.getProperty("jdbc.properties"));
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return comboDataSource;
}
这一定会对你有所帮助。
据我了解spring-session javaconfig-jdbc sample / doc,您"just"需要:
用org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession
注释"your config class" (YourConfig
).
姓名你的DataSource
"dataSource"。 (完成!;)
提供一个PlatformTransactionManager
bean,基于dataSource
in YourConfig
.
(在 servlet 环境中 - 就像你的一样)引入一个 AbstractHttpSessionApplicationInitializer
(在 class 路径中)引用 YourConfig
:
public class Initializer extends org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer { // <1>
public Initializer() {
super(YourConfig.class); // <2>
}
}
如果您希望手动数据库模式或使用外部工具安装, SQL 脚本位于 spring-session.jar(!org/springframework/session/jdbc/schema-@@platform@@.sql) 文件或 in the source code repository。
这些(应用程序)属性允许进一步定制:
# Session store type. [jdbc|redis|hazelcast|mongodb]
spring.session.store-type=jdbc
# Session timeout. If a duration suffix is not specified, seconds will be used.
server.servlet.session.timeout=
# Database schema initialization mode. [alwys | never | embedded]
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.(see: https://github.com/spring-projects/spring-session/tree/master/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc)
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql
# custom spring session table name (see : https://github.com/spring-projects/spring-session/issues/1230)
spring.session.jdbc.table-name=SPRING_SESSION
- 在 jar/source 发行版中,您还会发现 "cleanup" (-drop) 脚本
目前提供的平台有:
db2
derby
h2
hsqldb
mysql
oracle
postgresql
sqlite
sqlserver
sybase
我的应用程序 运行 spring web mvc 框架没有 spring 引导。现在我想使用 spring session JDBC 将会话存储到应用程序使用的数据库中。我在网上找到的所有例子都是使用 spring 启动,如果不使用 spring 启动,他们使用的数据源配置是 EmbeddedDatabase
像这样:
@Bean
public EmbeddedDatabase dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("org/springframework/session/jdbc/schema-h2.sql").build();
}
我的数据源配置使用 HikariCP,我希望 spring 会话使用此数据源配置。
@Bean
public DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(env.getRequiredProperty("jdbc.driver"));
config.setJdbcUrl(env.getRequiredProperty("jdbc.url"));
config.setUsername(env.getRequiredProperty("jdbc.username"));
config.setPassword(env.getRequiredProperty("jdbc.password"));
config.setMinimumIdle(env.getRequiredProperty("jdbc.pool.minimumIdle", Integer.class));
config.setMaximumPoolSize(env.getRequiredProperty("jdbc.pool.maximumPoolSize", Integer.class));
config.addDataSourceProperty("cachePrepStmts", env.getRequiredProperty("jdbc.prop.cachePrepStmts"));
config.addDataSourceProperty("prepStmtCacheSize", env.getRequiredProperty("jdbc.prop.prepStmtCacheSize"));
config.addDataSourceProperty("prepStmtCacheSqlLimit", env.getRequiredProperty("jdbc.prop.prepStmtCacheSqlLimit"));
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
如何使用当前配置与 spring 会话集成?
@Autowired
private Environment env;
@Bean
public PlatformTransactionManager transactionManager () {
EntityManagerFactory factory = entityManagerFactory();
return new JpaTransactionManager(factory);
}
@Bean
public EntityManagerFactory entityManagerFactory () {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setShowSql(Boolean.TRUE);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.your.domain.project");
factory.setDataSource(dataSource());
factory.setJpaProperties(additionalProperties()); // any addtional properties of your ORM
factory.afterPropertiesSet();
factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
return factory.getObject();
}
@Bean
public DataSource dataSource () {
final com.mchange.v2.c3p0.ComboPooledDataSource comboDataSource = new ComboPooledDataSource();
try {
comboDataSource.setDriverClass(env.getProperty("jdbc.driver"));
comboDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
comboDataSource.setUser(env.getProperty("jdbc.user"));
comboDataSource.setPassword(env.getProperty("jdbc.properties"));
} catch (PropertyVetoException e) {
e.printStackTrace();
}
return comboDataSource;
}
这一定会对你有所帮助。
据我了解spring-session javaconfig-jdbc sample / doc,您"just"需要:
用
org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession
注释"your config class" (YourConfig
).姓名你的
DataSource
"dataSource"。 (完成!;)提供一个
PlatformTransactionManager
bean,基于dataSource
inYourConfig
.(在 servlet 环境中 - 就像你的一样)引入一个
AbstractHttpSessionApplicationInitializer
(在 class 路径中)引用YourConfig
:public class Initializer extends org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer { // <1> public Initializer() { super(YourConfig.class); // <2> } }
如果您希望手动数据库模式或使用外部工具安装, SQL 脚本位于 spring-session.jar(!org/springframework/session/jdbc/schema-@@platform@@.sql) 文件或 in the source code repository。
这些(应用程序)属性允许进一步定制:
# Session store type. [jdbc|redis|hazelcast|mongodb]
spring.session.store-type=jdbc
# Session timeout. If a duration suffix is not specified, seconds will be used.
server.servlet.session.timeout=
# Database schema initialization mode. [alwys | never | embedded]
spring.session.jdbc.initialize-schema=always
# Path to the SQL file to use to initialize the database schema.(see: https://github.com/spring-projects/spring-session/tree/master/spring-session-jdbc/src/main/resources/org/springframework/session/jdbc)
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql
# custom spring session table name (see : https://github.com/spring-projects/spring-session/issues/1230)
spring.session.jdbc.table-name=SPRING_SESSION
- 在 jar/source 发行版中,您还会发现 "cleanup" (-drop) 脚本
目前提供的平台有:
db2 derby h2 hsqldb mysql oracle postgresql sqlite sqlserver sybase