Skip to content

Commit 86e347c

Browse files
committed
fix:获取锁key时,当参数值为null时抛空指针的异常
1 parent 756dc99 commit 86e347c

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/main/java/org/springframework/boot/autoconfigure/klock/core/BusinessKeyProvider.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.expression.ExpressionParser;
1313
import org.springframework.expression.spel.standard.SpelExpressionParser;
1414
import org.springframework.expression.spel.support.StandardEvaluationContext;
15+
import org.springframework.util.ObjectUtils;
1516
import org.springframework.util.StringUtils;
1617

1718
import java.lang.reflect.Method;
@@ -56,10 +57,10 @@ private Method getMethod(JoinPoint joinPoint) {
5657
private List<String> getSpelDefinitionKey(String[] definitionKeys, Method method, Object[] parameterValues) {
5758
List<String> definitionKeyList = new ArrayList<>();
5859
for (String definitionKey : definitionKeys) {
59-
if (definitionKey != null && !definitionKey.isEmpty()) {
60+
if (!ObjectUtils.isEmpty(definitionKey)) {
6061
EvaluationContext context = new MethodBasedEvaluationContext(null, method, parameterValues, nameDiscoverer);
61-
String key = parser.parseExpression(definitionKey).getValue(context).toString();
62-
definitionKeyList.add(key);
62+
Object objKey = parser.parseExpression(definitionKey).getValue(context);
63+
definitionKeyList.add(ObjectUtils.nullSafeToString(objKey));
6364
}
6465
}
6566
return definitionKeyList;
@@ -71,11 +72,12 @@ private List<String> getParameterKey(Parameter[] parameters, Object[] parameterV
7172
if (parameters[i].getAnnotation(KlockKey.class) != null) {
7273
KlockKey keyAnnotation = parameters[i].getAnnotation(KlockKey.class);
7374
if (keyAnnotation.value().isEmpty()) {
74-
parameterKey.add(parameterValues[i].toString());
75+
Object parameterValue = parameterValues[i];
76+
parameterKey.add(ObjectUtils.nullSafeToString(parameterValue));
7577
} else {
7678
StandardEvaluationContext context = new StandardEvaluationContext(parameterValues[i]);
77-
String key = parser.parseExpression(keyAnnotation.value()).getValue(context).toString();
78-
parameterKey.add(key);
79+
Object key = parser.parseExpression(keyAnnotation.value()).getValue(context);
80+
parameterKey.add(ObjectUtils.nullSafeToString(key));
7981
}
8082
}
8183
}

src/test/java/org/springframework/boot/autoconfigure/klock/test/KlockTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void jvm3()throws Exception{
8484
*/
8585
@Test
8686
public void businessKeyJvm1()throws Exception{
87-
String result=testService.getValue("user1",1);
87+
String result=testService.getValue("user1",null);
8888
Assert.assertEquals(result,"success");
8989
}
9090
/**
@@ -108,7 +108,7 @@ public void businessKeyJvm3()throws Exception{
108108
*/
109109
@Test
110110
public void businessKeyJvm4()throws Exception{
111-
String result=testService.getValue(new User(3,"kl"));
111+
String result=testService.getValue(new User(3,null));
112112
Assert.assertEquals(result,"success");
113113
}
114114

src/test/java/org/springframework/boot/autoconfigure/klock/test/TestService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public String getValue(String param) throws Exception {
2020
}
2121

2222
@Klock(keys = {"#userId"})
23-
public String getValue(String userId,@KlockKey int id)throws Exception{
23+
public String getValue(String userId,@KlockKey Integer id)throws Exception{
2424
Thread.sleep(60*1000);
2525
return "success";
2626
}

0 commit comments

Comments
 (0)