【问题标题】:Properties(other than username,pasword,url) are not getting loaded using @ConfigurationProperties没有使用 @ConfigurationProperties 加载属性(除了用户名、密码、url)
【发布时间】:2018-09-27 04:25:38
【问题描述】:

我有两个数据源,但我只能加载用户名、密码和网址。其他属性,如 poolPingEnabled、poolPingQuery、poolPingConnectionsNotUsedFor、poolMaximumActiveConnections。

注意:我的代码流正在运行,我能够连接到两个数据库。我的主要问题是为什么其他属性没有被加载。我找不到任何有助于添加我上面提到的属性的示例。

@Configuration
    public class DatabaseConfiguration {
  public static final String GDPR_DATASOURCE = "gdpr";
  public static final String CONFIG_DATASOURCE = "config";

  @Bean(name = GDPR_DATASOURCE)
  @ConfigurationProperties(prefix = "gdpr.jdbc")
  public DataSource gdprDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = CONFIG_DATASOURCE)
  @ConfigurationProperties(prefix = "config.jdbc")
  public DataSource configDataSource() {
    return DataSourceBuilder.create().build();
  }
}

Application.properties:这里的用户名和密码只是示例。在我的例子中,url、用户名和密码都是不同的。

spring.datasource.initialize=false
gdpr.jdbc.url=jdbc:mysql://localhost:3306/gdpr
gdpr.jdbc.driverClassName=com.mysql.jdbc.Driver
gdpr.jdbc.username=root
gdpr.jdbc.password=local
gdpr.jdbc.poolPingEnabled=true
gdpr.jdbc.poolPingQuery=SELECT 1
gdpr.jdbc.poolPingConnectionsNotUsedFor=9000
gdpr.jdbc.poolMaximumActiveConnections=25

config.jdbc.url=jdbc:mysql://localhost:3306/config
config.jdbc.driverClassName=com.mysql.jdbc.Driver
config.jdbc.username=root
config.jdbc.password=local
config.jdbc.poolPingEnabled=true
config.jdbc.poolPingQuery=SELECT 1
config.jdbc.poolPingConnectionsNotUsedFor=10000
config.jdbc.poolMaximumActiveConnections=25

MyBatisConfiguration.java :这里我创建了两个 SqlSessionFactoryBean。这些用于联系各个数据库。

@Configuration
public class MyBatisConfiguration {
  private static final String GDPR_SESSION_FACTORY = "gdprSessionFactory";
  private static final String CONFIG_SESSION_FACTORY = "configSessionFactory";

  /** This method is used for generating SqlSessionFactoryBean for gdpr DB.
   * @param gdprDataSource gdpr datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for gdpr DB
   */
  @Bean(name = GDPR_SESSION_FACTORY, destroyMethod = "")
  @Primary
  public SqlSessionFactoryBean gdprSqlSessionFactory(
      @Named(DatabaseConfiguration.GDPR_DATASOURCE) final DataSource gdprDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(gdprDataSource);
    SqlSessionFactory sqlSessionFactory;
    sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(GdprDatabaseMapper.class);
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:GdprDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }

  /** This method creates MapperFactory for SqlSessionFactoryBean GDPR_SESSION_FACTORY.
   * @param sqlSessionFactoryBean gdpr SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<GdprDatabaseMapper> gdprMapper(
  @Named(GDPR_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
    MapperFactoryBean<GdprDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(GdprDatabaseMapper.class);
    factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
    return factoryBean;
  }

  /** This method is used for generating SqlSessionFactoryBean for config DB.
   * @param configDataSource config datasource with configuration properties loaded
   * @return sqlSessionFactoryBean for config DB
   */
  @Bean(name = CONFIG_SESSION_FACTORY, destroyMethod = "")
  public SqlSessionFactoryBean configSqlSessionFactory(
  @Named(DatabaseConfiguration.CONFIG_DATASOURCE) final DataSource configDataSource)
  throws Exception {
    final SqlSessionFactoryBean sqlSessionFactoryBean =
    new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(configDataSource);
    final SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBean.getObject();
    sqlSessionFactory.getConfiguration().addMapper(ConfigDatabaseMapper.class);
PathMatchingResourcePatternResolver resolver = new     PathMatchingResourcePatternResolver();
    sqlSessionFactoryBean.setMapperLocations(resolver.getResources(
    "classpath:ConfigDatabaseMapper.xml"));
    return sqlSessionFactoryBean;
  }

  /** This method creates MapperFactory for SqlSessionFactoryBean CONFIG_SESSION_FACTORY.
   * @param sqlSessionFactoryBean config SqlSessionFactoryBean
   * @return MapperFactoryBean for SqlSessionFactoryBean
   */
  @Bean
  public MapperFactoryBean<ConfigDatabaseMapper> configMapper(
  @Named(CONFIG_SESSION_FACTORY) final SqlSessionFactoryBean sqlSessionFactoryBean)
  throws Exception {
MapperFactoryBean<ConfigDatabaseMapper> factoryBean =
    new MapperFactoryBean<>(ConfigDatabaseMapper.class);
factoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
return factoryBean;
  }
}

【问题讨论】:

  • 其中一个 bean 应该有注释 @Primary。另见:Another stack question
  • 我添加了更多细节。我正在为 SqlSessionFactoryBean 使用 @Primary。我的代码按我的需要工作,我只想加载其他属性。
  • 你有mybatis依赖吗?
  • 是的。我删除了 mybatis-config.xml 文件,因为我正在通过 application.properties 加载 Datasource 中的所有属性。此外,我没有使用 spring 默认数据源,如您所见,我已设置 spring.datasource.initialize=false。
  • 你用的是什么连接池?

标签: spring-boot datasource mybatis


【解决方案1】:

问题是你试图使用mybatis内置的连接池理解的配置参数。 Spring 不知道它们。

使用spring boot时不使用Mybatis连接池。改为使用spring中配置的连接池。

要解决此问题,您需要添加一些连接池,如 here 所述。

然后需要通过应用属性配置spring boot托管的连接池。确切的属性取决于您使用的连接池。更多详情here

以 tomcat 连接池为例,它看起来像这样:

# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000

# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50

# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true

【讨论】:

  • 感谢您的回复。我认为这应该对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 2017-05-18
相关资源
最近更新 更多