forked from github/dataease
refactor: 数据源接口抽象
This commit is contained in:
parent
35a075bdfe
commit
325cd6cda5
@ -1,16 +1,22 @@
|
|||||||
package io.dataease.extensions.datasource.factory;
|
package io.dataease.extensions.datasource.factory;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import io.dataease.extensions.datasource.provider.Provider;
|
||||||
import org.springframework.context.ApplicationContext;
|
import io.dataease.extensions.datasource.utils.SpringContextUtil;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import io.dataease.extensions.datasource.vo.DatasourceConfiguration;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author Junjun
|
* @Author Junjun
|
||||||
*/
|
*/
|
||||||
public class ProviderFactory implements ApplicationContextAware {
|
public class ProviderFactory {
|
||||||
@Override
|
|
||||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
||||||
|
|
||||||
|
public static Provider getProvider(String type) {
|
||||||
|
List<String> list = Arrays.stream(DatasourceConfiguration.DatasourceType.values()).map(DatasourceConfiguration.DatasourceType::getType).toList();
|
||||||
|
if (list.contains(type)) {
|
||||||
|
return SpringContextUtil.getApplicationContext().getBean("calciteProvider", Provider.class);
|
||||||
|
}
|
||||||
|
return SpringContextUtil.getApplicationContext().getBean(type + "DsProvider", Provider.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package io.dataease.extensions.datasource.utils;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class SpringContextUtil implements ApplicationContextAware {
|
||||||
|
|
||||||
|
//获取bean工厂,用来实现动态注入bean
|
||||||
|
//不能使用其他类加载器加载bean
|
||||||
|
//否则会出现异常:类未找到,类未定义
|
||||||
|
public static DefaultListableBeanFactory getBeanFactory(){
|
||||||
|
return (DefaultListableBeanFactory) getApplicationContext().getAutowireCapableBeanFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static List<Map<String, Object>> getAllBean() {
|
||||||
|
|
||||||
|
|
||||||
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
String[] beans = getApplicationContext()
|
||||||
|
.getBeanDefinitionNames();
|
||||||
|
|
||||||
|
for (String beanName : beans) {
|
||||||
|
Class<?> beanType = getApplicationContext()
|
||||||
|
.getType(beanName);
|
||||||
|
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
|
||||||
|
map.put("BeanName", beanName);
|
||||||
|
map.put("beanType", beanType);
|
||||||
|
map.put("package", beanType.getPackage());
|
||||||
|
list.add(map);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上下文对象实例
|
||||||
|
*/
|
||||||
|
private static ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
|
||||||
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取applicationContext
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ApplicationContext getApplicationContext() {
|
||||||
|
return applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Object getBean(String name) {
|
||||||
|
return getApplicationContext().getBean(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getBean(Class<T> clazz) {
|
||||||
|
return getApplicationContext().getBean(clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getBean(String name, Class<T> clazz) {
|
||||||
|
return getApplicationContext().getBean(name, clazz);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user