医院废弃物信息化管理平台
“mitao”
2026-04-21 2f18b3a28054c3917010f95ba84f7425280a2622
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
/**
 * Copyright © 2018 organization 苞米豆
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.besteam.common.aspect;
 
import cn.besteam.common.aspect.annotation.LockApi;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.StringUtils;
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 分布式锁Key生成器
 *
 *
 * @author chenqilong
 * @since 2019/4/19
 */
public class LockKeyGenerator {
 
    private static final ParameterNameDiscoverer NAME_DISCOVERER = new DefaultParameterNameDiscoverer();
 
    private static final ExpressionParser PARSER = new SpelExpressionParser();
 
    public String getKeyName(ProceedingJoinPoint joinPoint, LockApi lockApi) {
 
        StringBuilder sb = new StringBuilder();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        sb.append(method.getDeclaringClass().getName()).append(".").append(method.getName());
        if (lockApi.keys().length > 1 || !"".equals(lockApi.keys()[0])) {
            sb.append(getSpelDefinitionKey(lockApi.keys(), method, joinPoint.getArgs()));
        }
        return sb.toString();
    }
 
    private String getSpelDefinitionKey(String[] definitionKeys, Method method, Object[] parameterValues) {
 
        EvaluationContext context = new MethodBasedEvaluationContext(null, method, parameterValues, NAME_DISCOVERER);
        List<String> definitionKeyList = new ArrayList<>(definitionKeys.length);
        for (String definitionKey : definitionKeys) {
            if (definitionKey != null && !definitionKey.isEmpty()) {
                String key = PARSER.parseExpression(definitionKey).getValue(context).toString();
                definitionKeyList.add(key);
            }
        }
        return StringUtils.collectionToDelimitedString(definitionKeyList, ".", "", "");
    }
 
    public static void main(String[] args) {
 
        class Simple {
 
            public int val = 500;
 
        }
        Simple simple = new Simple();
        String el = "#{val > 100 && val < 400}";
        EvaluationContext context = new StandardEvaluationContext(simple);
        Object value = PARSER.parseExpression(el, new TemplateParserContext()).getValue(context);
        System.out.println(value);
    }
 
}