♻️ Refactoring code. 兼容 dynamic-datasource 默认策略

This commit is contained in:
lbw 2023-08-11 17:52:09 +08:00
parent 53d92363e2
commit 42c7d6d7f6

View File

@ -19,18 +19,23 @@ package com.pig4cloud.pig.common.datasource;
import com.baomidou.dynamic.datasource.creator.DataSourceCreator; import com.baomidou.dynamic.datasource.creator.DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator; import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator; import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.processor.DsJakartaHeaderProcessor;
import com.baomidou.dynamic.datasource.processor.DsJakartaSessionProcessor;
import com.baomidou.dynamic.datasource.processor.DsProcessor; import com.baomidou.dynamic.datasource.processor.DsProcessor;
import com.baomidou.dynamic.datasource.processor.DsSpelExpressionProcessor;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider; import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.pig4cloud.pig.common.datasource.config.ClearTtlDataSourceFilter; import com.pig4cloud.pig.common.datasource.config.ClearTtlDataSourceFilter;
import com.pig4cloud.pig.common.datasource.config.DataSourceProperties; import com.pig4cloud.pig.common.datasource.config.DataSourceProperties;
import com.pig4cloud.pig.common.datasource.config.JdbcDynamicDataSourceProvider; import com.pig4cloud.pig.common.datasource.config.JdbcDynamicDataSourceProvider;
import com.pig4cloud.pig.common.datasource.config.LastParamDsProcessor; import com.pig4cloud.pig.common.datasource.config.LastParamDsProcessor;
import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.expression.BeanFactoryResolver;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -46,17 +51,41 @@ import java.util.List;
@EnableConfigurationProperties(DataSourceProperties.class) @EnableConfigurationProperties(DataSourceProperties.class)
public class DynamicDataSourceAutoConfiguration { public class DynamicDataSourceAutoConfiguration {
/**
* 动态数据源提供者
* @param defaultDataSourceCreator 默认数据源创建器
* @param stringEncryptor 字符串加密器
* @param properties 数据源配置属性
* @return 动态数据源提供者
*/
@Bean @Bean
public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCreator defaultDataSourceCreator, public DynamicDataSourceProvider dynamicDataSourceProvider(DefaultDataSourceCreator defaultDataSourceCreator,
StringEncryptor stringEncryptor, DataSourceProperties properties) { StringEncryptor stringEncryptor, DataSourceProperties properties) {
return new JdbcDynamicDataSourceProvider(defaultDataSourceCreator, stringEncryptor, properties); return new JdbcDynamicDataSourceProvider(defaultDataSourceCreator, stringEncryptor, properties);
} }
/**
* 获取数据源处理器
* @return 数据源处理器
*/
@Bean @Bean
public DsProcessor dsProcessor() { public DsProcessor dsProcessor(BeanFactory beanFactory) {
return new LastParamDsProcessor(); DsProcessor lastParamDsProcessor = new LastParamDsProcessor();
DsProcessor headerProcessor = new DsJakartaHeaderProcessor();
DsProcessor sessionProcessor = new DsJakartaSessionProcessor();
DsSpelExpressionProcessor spelExpressionProcessor = new DsSpelExpressionProcessor();
spelExpressionProcessor.setBeanResolver(new BeanFactoryResolver(beanFactory));
lastParamDsProcessor.setNextProcessor(headerProcessor);
headerProcessor.setNextProcessor(sessionProcessor);
sessionProcessor.setNextProcessor(spelExpressionProcessor);
return lastParamDsProcessor;
} }
/**
* 默认数据源创建器
* @param hikariDataSourceCreator Hikari数据源创建器
* @return 默认数据源创建器
*/
@Bean @Bean
public DefaultDataSourceCreator defaultDataSourceCreator(HikariDataSourceCreator hikariDataSourceCreator) { public DefaultDataSourceCreator defaultDataSourceCreator(HikariDataSourceCreator hikariDataSourceCreator) {
DefaultDataSourceCreator defaultDataSourceCreator = new DefaultDataSourceCreator(); DefaultDataSourceCreator defaultDataSourceCreator = new DefaultDataSourceCreator();
@ -66,6 +95,10 @@ public class DynamicDataSourceAutoConfiguration {
return defaultDataSourceCreator; return defaultDataSourceCreator;
} }
/**
* 清除Ttl数据源过滤器
* @return 清除Ttl数据源过滤器
*/
@Bean @Bean
public ClearTtlDataSourceFilter clearTtlDsFilter() { public ClearTtlDataSourceFilter clearTtlDsFilter() {
return new ClearTtlDataSourceFilter(); return new ClearTtlDataSourceFilter();