Skip to content

Commit

Permalink
support druid proxyFilters
Browse files Browse the repository at this point in the history
  • Loading branch information
huayanYu committed Oct 23, 2023
1 parent 5107fd5 commit e101390
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import lombok.Getter;
import lombok.Setter;

import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
* Druid参数配置
Expand Down Expand Up @@ -89,5 +91,5 @@ public class DruidConfig {
private Map<String, Object> commonsLog = new HashMap<>();
private Map<String, Object> stat = new HashMap<>();

private List<String> proxyFilters = new ArrayList<>();
private String proxyFilters;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class DruidDataSourceCreator implements DataSourceCreator {

private DruidConfig gConfig;

private DruidFilterCallBack druidFilterCallBack;

/**
* Druid since 1.2.17 use 'configFromPropeties' to copy config
* Druid < 1.2.17 use 'configFromPropety' to copy config
Expand Down Expand Up @@ -114,6 +116,9 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) {

String configFilters = properties.getProperty("druid.filters");
List<Filter> proxyFilters = this.initFilters(config, configFilters);
if (druidFilterCallBack != null) {
proxyFilters.addAll(druidFilterCallBack.getFilters(config.getProxyFilters()));
}
dataSource.setProxyFilters(proxyFilters);
try {
configMethod.invoke(dataSource, properties);
Expand Down Expand Up @@ -172,11 +177,6 @@ private List<Filter> initFilters(DruidConfig config, String filters) {
}
}
}
// if (this.applicationContext != null) {
// for (String filterId : gConfig.getProxyFilters()) {
// proxyFilters.add(this.applicationContext.getBean(filterId, Filter.class));
// }
// }
return proxyFilters;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2018 organization baomidou
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.baomidou.dynamic.datasource.creator.druid;

import com.alibaba.druid.filter.Filter;

import java.util.List;

public interface DruidFilterCallBack {

List<Filter> getFilters(String proxyFilters);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,33 @@
package com.baomidou.dynamic.datasource.spring.boot.autoconfigure;

import cn.beecp.BeeDataSource;
import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.baomidou.dynamic.datasource.creator.atomikos.AtomikosDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.basic.BasicDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.beecp.BeeCpDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.dbcp.Dbcp2DataSourceCreator;
import com.baomidou.dynamic.datasource.creator.druid.DruidConfig;
import com.baomidou.dynamic.datasource.creator.druid.DruidDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.hikaricp.HikariDataSourceCreator;
import com.baomidou.dynamic.datasource.creator.jndi.JndiDataSourceCreator;
import com.baomidou.dynamic.datasource.toolkit.DsStrUtils;
import com.baomidou.dynamic.datasource.tx.AtomikosTransactionFactory;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.ibatis.transaction.TransactionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.List;

/**
* @author TaoYu
*/
Expand Down Expand Up @@ -65,12 +74,29 @@ public JndiDataSourceCreator jndiDataSourceCreator() {
*/
@ConditionalOnClass(DruidDataSource.class)
@Configuration
@Slf4j
static class DruidDataSourceCreatorConfiguration {

@Autowired(required = false)
private ApplicationContext applicationContext;

@Bean
@Order(DRUID_ORDER)
public DruidDataSourceCreator druidDataSourceCreator(DynamicDataSourceProperties properties) {
return new DruidDataSourceCreator(properties.getDruid());
DruidConfig druid = properties.getDruid();
return new DruidDataSourceCreator(druid, proxyFilters -> {
List<Filter> filters = new ArrayList<>();
if (applicationContext != null && DsStrUtils.hasText(proxyFilters)) {
for (String filterId : proxyFilters.split(",")) {
try {
filters.add(applicationContext.getBean(filterId, Filter.class));
} catch (Exception e) {
log.warn("dynamic-datasource cannot load druid filter with name [{}], will be ignored", filterId);
}
}
}
return filters;
});
}
}

Expand Down

0 comments on commit e101390

Please sign in to comment.