fix: 修复配置多数据源的问题

This commit is contained in:
CaptainB 2024-08-12 19:15:13 +08:00 committed by 刘瑞斌
parent 7718bee0e7
commit 62a839d7b3
1 changed files with 26 additions and 13 deletions

View File

@ -11,6 +11,7 @@ import io.metersphere.commons.utils.MybatisInterceptorConfig;
import io.metersphere.interceptor.MybatisInterceptor; import io.metersphere.interceptor.MybatisInterceptor;
import io.metersphere.interceptor.UserDesensitizationInterceptor; import io.metersphere.interceptor.UserDesensitizationInterceptor;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
@ -63,12 +64,12 @@ public class CommonsDatabaseConfig {
@Bean @Bean
@Primary @Primary
@ConfigurationProperties(prefix = "spring.datasource.hikari") @ConfigurationProperties(prefix = "spring.datasource.hikari")
public DataSource dataSource(DataSourceProperties properties) { public DataSource dataSource(@Qualifier("dataSourceProperties") DataSourceProperties dataSourceProperties) {
return DataSourceBuilder.create(properties.getClassLoader()).type(HikariDataSource.class) return DataSourceBuilder.create(dataSourceProperties.getClassLoader()).type(HikariDataSource.class)
.driverClassName(properties.determineDriverClassName()) .driverClassName(dataSourceProperties.determineDriverClassName())
.url(properties.determineUrl()) .url(dataSourceProperties.determineUrl())
.username(properties.determineUsername()) .username(dataSourceProperties.determineUsername())
.password(properties.determinePassword()) .password(dataSourceProperties.determinePassword())
.build(); .build();
} }
@ -76,13 +77,25 @@ public class CommonsDatabaseConfig {
@ConfigurationProperties(prefix = "spring.datasource.quartz.hikari") @ConfigurationProperties(prefix = "spring.datasource.quartz.hikari")
@QuartzDataSource @QuartzDataSource
@ConditionalOnProperty(prefix = "quartz", value = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = "quartz", value = "enabled", havingValue = "true")
public DataSource quartzDataSource(DataSourceProperties properties) { public DataSource quartzDataSource(@Qualifier("quartzDataSourceProperties") DataSourceProperties quartzDataSourceProperties) {
return DataSourceBuilder.create(properties.getClassLoader()).type(HikariDataSource.class) return DataSourceBuilder.create(quartzDataSourceProperties.getClassLoader()).type(HikariDataSource.class)
.driverClassName(properties.determineDriverClassName()) .driverClassName(quartzDataSourceProperties.determineDriverClassName())
.url(properties.determineUrl()) .url(quartzDataSourceProperties.determineUrl())
.username(properties.determineUsername()) .username(quartzDataSourceProperties.determineUsername())
.password(properties.determinePassword()) .password(quartzDataSourceProperties.determinePassword())
.build(); .build();
} }
@Bean("dataSourceProperties")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Bean("quartzDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.quartz")
public DataSourceProperties quartzDataSourceProperties() {
return new DataSourceProperties();
}
} }