|
1 | 1 | package com.github.cadecode.uniboot.common.plugin.storage.config; |
2 | 2 |
|
3 | | -import cn.hutool.core.util.ObjUtil; |
4 | | -import com.github.cadecode.uniboot.common.plugin.storage.handler.AbstractStorageHandler; |
5 | | -import lombok.RequiredArgsConstructor; |
6 | 3 | 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; |
13 | | -import org.springframework.context.annotation.Bean; |
14 | 4 | import org.springframework.context.annotation.Configuration; |
15 | 5 |
|
16 | | -import java.util.Arrays; |
17 | | -import java.util.Optional; |
18 | | - |
19 | 6 | /** |
20 | 7 | * x-file-storage 配置类 |
21 | 8 | * |
22 | 9 | * @author Cade Li |
23 | 10 | * @since 2023/10/25 |
24 | 11 | */ |
25 | 12 | @EnableFileStorage |
26 | | -@RequiredArgsConstructor |
27 | 13 | @Configuration |
28 | 14 | public class XFileStorageConfig { |
29 | 15 |
|
30 | | - public static final String BEAN_NAME_FILE_RECORDER = "fileRecorder"; |
31 | | - |
32 | | - @Bean |
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 | | - } |
64 | | - } |
65 | 16 | } |
0 commit comments