2cb5fa80cf3a9bc4ba34286be52238d3e2a99059.svn-base 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package org.jeecg.boot.starter.lock.aspect;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.expression.EvaluationContext;
  4. import org.springframework.expression.Expression;
  5. import org.springframework.expression.ExpressionParser;
  6. import org.springframework.expression.spel.standard.SpelExpressionParser;
  7. import org.springframework.expression.spel.support.StandardEvaluationContext;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. /**
  11. * @author zyf
  12. */
  13. @Slf4j
  14. public class BaseAspect {
  15. /**
  16. * 通过spring SpEL 获取参数
  17. *
  18. * @param key 定义的key值 以#开头 例如:#user
  19. * @param parameterNames 形参
  20. * @param values 形参值
  21. * @param keyConstant key的常亮
  22. * @return
  23. */
  24. public List<String> getValueBySpEL(String key, String[] parameterNames, Object[] values, String keyConstant) {
  25. List<String> keys = new ArrayList<>();
  26. if (!key.contains("#")) {
  27. String s = "redis:lock:" + key + keyConstant;
  28. log.info("lockKey:" + s);
  29. keys.add(s);
  30. return keys;
  31. }
  32. //spel解析器
  33. ExpressionParser parser = new SpelExpressionParser();
  34. //spel上下文
  35. EvaluationContext context = new StandardEvaluationContext();
  36. for (int i = 0; i < parameterNames.length; i++) {
  37. context.setVariable(parameterNames[i], values[i]);
  38. }
  39. Expression expression = parser.parseExpression(key);
  40. Object value = expression.getValue(context);
  41. if (value != null) {
  42. if (value instanceof List) {
  43. List value1 = (List) value;
  44. for (Object o : value1) {
  45. addKeys(keys, o, keyConstant);
  46. }
  47. } else if (value.getClass().isArray()) {
  48. Object[] obj = (Object[]) value;
  49. for (Object o : obj) {
  50. addKeys(keys, o, keyConstant);
  51. }
  52. } else {
  53. addKeys(keys, value, keyConstant);
  54. }
  55. }
  56. log.info("表达式key={},value={}", key, keys);
  57. return keys;
  58. }
  59. private void addKeys(List<String> keys, Object o, String keyConstant) {
  60. keys.add("redis:lock:" + o.toString() + keyConstant);
  61. }
  62. }