/** * Copyright © 2018 organization 苞米豆 *

* 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 *

* http://www.apache.org/licenses/LICENSE-2.0 *

* 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 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); } }