WangHan
2024-09-12 d5855a4926926698b740bc6c7ba489de47adb68b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package tech.powerjob.server.common.spring.condition;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
import tech.powerjob.common.utils.CollectionUtils;
 
import java.util.List;
 
/**
 * PropertyAndOneBeanCondition
 * 存在多个接口实现时的唯一规则
 *
 * @author tjq
 * @since 2023/7/30
 */
@Slf4j
public abstract class PropertyAndOneBeanCondition implements Condition {
 
    /**
     * 配置中存在任意一个 Key 即可加载该 Bean,空代表不校验
     * @return Keys
     */
    protected abstract List<String> anyConfigKey();
 
    /**
     * Bean 唯一性校验,空代表不校验
     * @return beanType
     */
    protected abstract Class<?> beanType();
 
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
 
        boolean anyCfgExist = checkAnyConfigExist(context);
        log.info("[PropertyAndOneBeanCondition] [{}] check any config exist result with keys={}: {}", thisName(), anyConfigKey(), anyCfgExist);
        if (!anyCfgExist) {
            return false;
        }
 
        Class<?> beanType = beanType();
        if (beanType == null) {
            return true;
        }
        boolean exist = checkBeanExist(context);
        log.info("[PropertyAndOneBeanCondition] [{}] bean of type[{}] exist check result: {}", thisName(), beanType.getSimpleName(), exist);
        if (exist) {
            log.info("[PropertyAndOneBeanCondition] [{}] bean of type[{}] already exist, skip load!", thisName(), beanType.getSimpleName());
            return false;
        }
        return true;
    }
 
    private boolean checkAnyConfigExist(ConditionContext context) {
        Environment environment = context.getEnvironment();
 
        List<String> keys = anyConfigKey();
 
        if (CollectionUtils.isEmpty(keys)) {
            return true;
        }
 
        // 判断前缀是否符合,任意满足即可
        for (String key : keys) {
            if (StringUtils.isNotEmpty(environment.getProperty(key))) {
                return true;
            }
        }
 
        return false;
    }
 
    private boolean checkBeanExist(ConditionContext context) {
 
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        if (beanFactory == null) {
            return false;
        }
        try {
            beanFactory.getBean(beanType());
            return true;
        } catch (NoSuchBeanDefinitionException ignore) {
            return false;
        }
    }
 
    private String thisName() {
        return this.getClass().getSimpleName();
    }
}