【问题标题】:Use spring session JDBC with existing database (not springboot)将 spring session JDBC 与现有数据库一起使用(不是 springboot)
【发布时间】:2019-02-01 01:21:01
【问题描述】:

我的应用程序在没有 spring boot 的情况下使用 spring web mvc 框架运行。现在我想使用 spring session JDBC 将 session 存储到应用程序使用的数据库中。我在网上找到的所有示例都是使用spring boot,如果不使用spring boot,他们使用的数据源配置是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 session 集成?

【问题讨论】:

    标签: spring spring-session spring-config


    【解决方案1】:

    据我了解spring-session javaconfig-jdbc sample / doc,您“只是”需要:

    1. 用org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession注释“你的配置类”(YourConfig)。

    2. 名称你的DataSource“数据源”。 (完成!;)

    3. 提供一个PlatformTransactionManager bean,基于dataSource in YourConfig。

    4. (在 servlet 环境中 - 与您一样)引入引用 YourConfig 的 AbstractHttpSessionApplicationInitializer(在类路径中):

      public class Initializer extends org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer { // <1>
      
        public Initializer() {
          super(YourConfig.class); // <2>
        }
      }
      

    如果您希望手动安装 db 架构 或使用外部工具,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/源代码分发中,您还可以找到“清理”(-drop)脚本
    • 目前提供的平台有:

      db2
      derby
      h2
      hsqldb
      mysql
      oracle
      postgresql
      sqlite
      sqlserver
      sybase
      

    【讨论】:

    • 是的,我错过了Initializer 的那部分。但它没有使用我当前的配置创建表 SPRING_SESSION。 spring session提供的SQL如何执行?
    • 查看编辑 (5. + 6.)!我认为特别是。重要的是 5. 和(如果您希望通过 Spring Boot 初始化会话模式):spring.session.jdbc.initialize-schema=always 和 spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql(其中 @@platform@@ 指的是您的数据库供应商/类型)。
    • 第 5 点和第 6 点与 Spring Boot 有关,所以我没有使用该配置。所以我运行脚本来使用 liquibase 创建 Spring 会话表。
    • 我认为我不需要在 application.properties 中添加设置,因为它不是 Spring Boot 项目。如果我添加它,什么都不会读取该设置。
    • 您能否更新您的答案,以便我将其标记为已接受?它需要更新有关如何创建春季会议所需的表格,这是我的主要问题。
    【解决方案2】:
    @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 会话?
    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多