diff --git a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/factory/ProviderFactory.java b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/factory/ProviderFactory.java index 95766b4a10..9c4df31a99 100644 --- a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/factory/ProviderFactory.java +++ b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/factory/ProviderFactory.java @@ -1,16 +1,22 @@ package io.dataease.extensions.datasource.factory; -import org.springframework.beans.BeansException; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; +import io.dataease.extensions.datasource.provider.Provider; +import io.dataease.extensions.datasource.utils.SpringContextUtil; +import io.dataease.extensions.datasource.vo.DatasourceConfiguration; + +import java.util.Arrays; +import java.util.List; /** * @Author Junjun */ -public class ProviderFactory implements ApplicationContextAware { - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { +public class ProviderFactory { + public static Provider getProvider(String type) { + List 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); } - } diff --git a/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/utils/SpringContextUtil.java b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/utils/SpringContextUtil.java new file mode 100644 index 0000000000..94d496ed96 --- /dev/null +++ b/sdk/extensions/extensions-datasource/src/main/java/io/dataease/extensions/datasource/utils/SpringContextUtil.java @@ -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> getAllBean() { + + + List> list = new ArrayList<>(); + + + String[] beans = getApplicationContext() + .getBeanDefinitionNames(); + + for (String beanName : beans) { + Class beanType = getApplicationContext() + .getType(beanName); + + Map 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 getBean(Class clazz) { + return getApplicationContext().getBean(clazz); + } + + public static T getBean(String name, Class clazz) { + return getApplicationContext().getBean(name, clazz); + } +}