parent
0506002277
commit
ec8c042a3d
4 changed files with 119 additions and 0 deletions
@ -0,0 +1,29 @@ |
|||||||
|
package org.springblade.desk.basic.aviator.config; |
||||||
|
|
||||||
|
import com.googlecode.aviator.AviatorEvaluator; |
||||||
|
import com.googlecode.aviator.AviatorEvaluatorInstance; |
||||||
|
import com.googlecode.aviator.Options; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public class AviatorConfig { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public AviatorEvaluatorInstance aviatorEvaluatorInstance() { |
||||||
|
AviatorEvaluatorInstance instance = AviatorEvaluator.newInstance(); |
||||||
|
// 核心配置项
|
||||||
|
instance.setOption(Options.USE_USER_ENV_AS_TOP_ENV_DIRECTLY, true); // 环境变量透传
|
||||||
|
// instance.setOption(Options.MAX_CACHE_SIZE, 2000); // 表达式缓存大小
|
||||||
|
instance.setOption(Options.TRACE_EVAL, false); // 生产环境关闭追踪
|
||||||
|
|
||||||
|
// JDK17+ 优化配置
|
||||||
|
instance.setOption(Options.OPTIMIZE_LEVEL, AviatorEvaluator.EVAL); // 优化级别
|
||||||
|
// 开启表达式缓存,提升性能
|
||||||
|
instance.setCachedExpressionByDefault(true); |
||||||
|
// 注册自定义函数(如有)
|
||||||
|
// instance.addFunction(new YourCustomFunction());
|
||||||
|
return instance; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
@ -0,0 +1,32 @@ |
|||||||
|
package org.springblade.desk.basic.aviator.service; |
||||||
|
|
||||||
|
import com.googlecode.aviator.AviatorEvaluatorInstance; |
||||||
|
import jakarta.annotation.Resource; |
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import lombok.Data; |
||||||
|
import lombok.EqualsAndHashCode; |
||||||
|
import lombok.ToString; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springframework.stereotype.Service; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@Service |
||||||
|
@Data |
||||||
|
@AllArgsConstructor |
||||||
|
@EqualsAndHashCode(callSuper = false) |
||||||
|
@ToString(callSuper = false) |
||||||
|
@Slf4j |
||||||
|
public class AviatorService { |
||||||
|
|
||||||
|
@Resource |
||||||
|
private AviatorEvaluatorInstance aviatorEvaluator; |
||||||
|
|
||||||
|
public Object evaluateExpression(String expression, Map<String, Object> variables) { |
||||||
|
try { |
||||||
|
return aviatorEvaluator.execute(expression, variables); |
||||||
|
} catch (Exception e) { |
||||||
|
throw new RuntimeException("表达式执行失败: " + e.getMessage(), e); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,50 @@ |
|||||||
|
package org.springblade.desk.basic.controller; |
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.tags.Tag; |
||||||
|
import jakarta.annotation.Resource; |
||||||
|
import lombok.AllArgsConstructor; |
||||||
|
import lombok.Data; |
||||||
|
import lombok.EqualsAndHashCode; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.springblade.desk.basic.aviator.service.AviatorService; |
||||||
|
import org.springblade.desk.basic.constant.BAModuleConst; |
||||||
|
import org.springframework.web.bind.annotation.PostMapping; |
||||||
|
import org.springframework.web.bind.annotation.RequestBody; |
||||||
|
import org.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import org.springframework.web.bind.annotation.RestController; |
||||||
|
|
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@RequestMapping(BAModuleConst.CONTROLLER_PREFIX + "/Aviator") |
||||||
|
@Data |
||||||
|
@AllArgsConstructor |
||||||
|
@EqualsAndHashCode(callSuper = false) |
||||||
|
@Slf4j |
||||||
|
@Tag(name = "Aviator", description = "Aviator接口") |
||||||
|
public class AviatorController { |
||||||
|
|
||||||
|
@Resource |
||||||
|
private AviatorService aviatorService; |
||||||
|
|
||||||
|
/** |
||||||
|
* { |
||||||
|
* "expression": "a + b * (c - d)", |
||||||
|
* "variables": { |
||||||
|
* "a": 5, |
||||||
|
* "b": 2, |
||||||
|
* "c": 10, |
||||||
|
* "d": 3 |
||||||
|
* } |
||||||
|
* } |
||||||
|
* |
||||||
|
* @param request |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
@PostMapping("/evaluate") |
||||||
|
public Object evaluate(@RequestBody Map<String, Object> request) { |
||||||
|
String expression = (String) request.get("expression"); |
||||||
|
Map<String, Object> variables = (Map<String, Object>) request.get("variables"); |
||||||
|
return aviatorService.evaluateExpression(expression, variables); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue