【问题标题】:Spring boot - init dataSources before anything elseSpring boot - 在其他任何事情之前初始化数据源
【发布时间】:2018-12-27 08:19:19
【问题描述】:

我有 2 个配置类,一个带有数据源配置,另一个是我的 WebSecurityConfig 扩展 WebSecurityConfigurerAdapter。 我想加载要在WebSecurityConfig 中使用的DB 值,所以我需要在WebSecurityConfig 之前完成初始化数据源。

Oracle配置

@Configuration
@ConfigurationProperties("oracle")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class OracleConfiguration {
   //data source beans...
}

网络安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@Order(Ordered.LOWEST_PRECEDENCE)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//security configs like SAML...
}

当我调试时,WebSecurityConfig 中的代码在数据源初始化之前运行,数据源初始化示例(在 OracleConfiguration 内)

@Bean
@Primary
DataSource dataSource() throws SQLException {
     //Init
    OracleDataSource dataSource = new OracleDataSource();
    dataSource.setUser(username);
    dataSource.setPassword(password);
    dataSource.setURL(url);
    dataSource.setImplicitCachingEnabled(true);
    dataSource.setFastConnectionFailoverEnabled(true);
    return dataSource;
}

【问题讨论】:

    标签: java spring-boot datasource spring-config


    【解决方案1】:

    尝试将 dataSource 放入 WebSecurityConfig 中的某个 bean 参数。在这种情况下,webSecurityConfig 中的 bean 将依赖于 OracleConfiguration 中的 dataSource bean。我认为它会起作用。

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 2016-11-10
      • 2016-12-16
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2018-07-25
      • 2023-03-15
      • 2018-03-22
      相关资源
      最近更新 更多