spel -> jexl

This commit is contained in:
Hua
2024-07-22 17:29:05 +08:00
parent 3c2595b3ff
commit c53c86d86d
5 changed files with 32 additions and 17 deletions

13
pom.xml
View File

@ -12,19 +12,14 @@
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springContext.version>5.2.1.RELEASE</springContext.version>
<hutool.version>5.8.26</hutool.version>
<reflections.version>0.9.10</reflections.version>
<validation.version>2.0.1.Final</validation.version>
<jexl.version>3.2.1</jexl.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springContext.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
@ -40,6 +35,10 @@
<artifactId>validation-api</artifactId>
<version>${validation.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-jexl3</artifactId>
<version>${jexl.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -8,7 +8,7 @@ import com.hua.valid.annotation.ConditionNotNull;
*/
public class Student {
@ConditionNotNull(test = "teaName == '王老师' and teaAge == 28 and teaSex == 1", message = "老师名字为王老师年龄为18性别为1的时候 name不能为null")
@ConditionNotNull(expression = "teaName == '王老师' and teaAge == 28 and teaSex == 1", message = "老师名字为王老师年龄为18性别为1的时候 name不能为null")
private String name;
private int age;

View File

@ -1,10 +1,13 @@
package com.hua.valid;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.expression.ExpressionUtil;
import com.hua.annotation.Handles;
import com.hua.valid.annotation.ConditionNotNull;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import java.util.Map;
/**
* @author Hua
@ -15,13 +18,26 @@ public class ConditionNotNullHandler implements AnnotationHandler {
@Override
public boolean handle(HandleDTO handleDTO) {
ConditionNotNull conditionValidate = (ConditionNotNull) handleDTO.getCurrentAnnotation();
String test = conditionValidate.test();
if (StrUtil.isEmpty(test)) {
String expression = conditionValidate.expression();
if (StrUtil.isEmpty(expression)) {
return true;
}
ExpressionParser parser = new SpelExpressionParser();
if (handleDTO.getField() == null) {
return true;
}
Dict context = new Dict();
Boolean value = parser.parseExpression(test).getValue(handleDTO.getOriginObj(), Boolean.class);
Map<String, Object> fieldMap = BeanUtil.beanToMap(handleDTO.getOriginObj(), false, true);
context.putAll(fieldMap);
try {
Object fieldValue = BeanUtil.getFieldValue(handleDTO.getOriginObj(), handleDTO.getField().getName());
context.set(handleDTO.getField().getName(), fieldValue);
} catch (Exception e) {
throw new RuntimeException("Unable to access field value", e);
}
Boolean value = (Boolean) ExpressionUtil.eval(expression, context);
if (Boolean.TRUE.equals(value) && handleDTO.getFieldValue() == null) {
handleDTO.getErrorMessage().addErrorObj(conditionValidate.message(), handleDTO.getField());
return false;

View File

@ -10,7 +10,7 @@ import java.lang.annotation.*;
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ConditionNotNull {
String test() default "";
String expression() default "";
String message() default "";
}

View File

@ -8,7 +8,7 @@ import java.lang.annotation.Target;
@Target(ElementType.TYPE) // 类或接口上使用
@Retention(RetentionPolicy.RUNTIME) // 在运行时可见
public @interface PackageScanConfig {
boolean useSPI() default false; // 默认为空数组
boolean useReflection() default false; // 默认为空数组
boolean useSPI() default false; // 默认为false
boolean useReflection() default false; // 默认为false
String[] packages() default {}; // 默认为空数组
}