|
1 | 1 | package com.github.cadecode.uniboot.common.plugin.storage.config; |
2 | 2 |
|
| 3 | +import cn.hutool.core.util.ObjUtil; |
3 | 4 | import com.github.cadecode.uniboot.common.plugin.storage.handler.AbstractStorageHandler; |
4 | 5 | import lombok.RequiredArgsConstructor; |
5 | | -import org.dromara.x.file.storage.core.recorder.FileRecorder; |
6 | 6 | import org.dromara.x.file.storage.spring.EnableFileStorage; |
| 7 | +import org.springframework.beans.BeansException; |
| 8 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 9 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 10 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 11 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 12 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
7 | 13 | import org.springframework.context.annotation.Bean; |
8 | 14 | import org.springframework.context.annotation.Configuration; |
9 | 15 |
|
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Optional; |
| 18 | + |
10 | 19 | /** |
11 | 20 | * x-file-storage 配置类 |
12 | 21 | * |
|
18 | 27 | @Configuration |
19 | 28 | public class XFileStorageConfig { |
20 | 29 |
|
| 30 | + public static final String BEAN_NAME_FILE_RECORDER = "fileRecorder"; |
| 31 | + |
21 | 32 | @Bean |
22 | | - public FileRecorder fileRecorder(AbstractStorageHandler storageRecordHandler) { |
23 | | - return storageRecordHandler; |
| 33 | + public FileRecorderBeanDefinitionRegistryPostProcessor fileRecorderBeanDefinitionRegistryPostProcessor() { |
| 34 | + return new FileRecorderBeanDefinitionRegistryPostProcessor(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 解决自定义 AbstractStorageHandler 不能替代 x-file-storage 框架默认 FileRecorder |
| 39 | + */ |
| 40 | + public static class FileRecorderBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { |
| 41 | + |
| 42 | + @Override |
| 43 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 49 | + String[] handlerBeanNames = beanFactory.getBeanNamesForType(AbstractStorageHandler.class); |
| 50 | + // 查找 primary 的,不存在则取第一个 |
| 51 | + if (ObjUtil.isNotEmpty(handlerBeanNames)) { |
| 52 | + Optional<BeanDefinition> primaryBF = Arrays.stream(handlerBeanNames) |
| 53 | + .map(beanFactory::getBeanDefinition) |
| 54 | + .filter(BeanDefinition::isPrimary) |
| 55 | + .findFirst(); |
| 56 | + BeanDefinition handlerBF; |
| 57 | + handlerBF = primaryBF.orElseGet(() -> beanFactory.getBeanDefinition(handlerBeanNames[0])); |
| 58 | + if (beanFactory instanceof DefaultListableBeanFactory) { |
| 59 | + ((DefaultListableBeanFactory) beanFactory).removeBeanDefinition(BEAN_NAME_FILE_RECORDER); |
| 60 | + ((DefaultListableBeanFactory) beanFactory).registerBeanDefinition(BEAN_NAME_FILE_RECORDER, handlerBF); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
24 | 64 | } |
25 | 65 | } |
0 commit comments