Java Code Examples for org.springframework.context.expression.MethodBasedEvaluationContext#setVariable()

The following examples show how to use org.springframework.context.expression.MethodBasedEvaluationContext#setVariable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExpressionEvaluator.java    From Milkomeda with MIT License 6 votes vote down vote up
/**
 * 根据方法创建一个 {@link EvaluationContext}
 * @param object        目标对象
 * @param targetClass   目标类型
 * @param method        方法
 * @param args          参数
 * @return  EvaluationContext
 */
public StandardEvaluationContext createEvaluationContext(Object object, Class<?> targetClass,
                                                         Method method, Object[] args) {
    Method targetMethod = getTargetMethod(targetClass, method);
    // 创建自定义EL Root
    ExpressionRootObject root = new ExpressionRootObject(object, args);
    // 创建基于方法的执行上下文
    MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
    // 添加变量引用
    Environment env = ApplicationContextHolder.getEnvironment();
    if (env != null) {
        evaluationContext.setVariable("env", env.getProperties());
    }
    evaluationContext.setVariable("target", object);
    ServletRequestAttributes requestAttributes = WebContext.getRequestAttributes();
    if (requestAttributes != null) {
        evaluationContext.setVariable("request", requestAttributes.getRequest());
        evaluationContext.setVariable("reqParams", requestAttributes.getRequest().getParameterMap());
    }
    return evaluationContext;
}
 
Example 2
Source File: LimiterOperationExpressionEvaluator.java    From Limiter with Apache License 2.0 5 votes vote down vote up
public EvaluationContext createEvaluationContext(Limiter limiter, Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
                                                 Map<String, Object> injectArgs, BeanFactory beanFactory) {

    LimiterExpressionRootObject rootObject = new LimiterExpressionRootObject(limiter, method, args, target, targetClass);
    MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, this.parameterNameDiscoverer);
    for (String key : injectArgs.keySet()) {
        evaluationContext.setVariable(key, injectArgs.get(key));
    }

    if (beanFactory != null) {
        evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }
    return evaluationContext;
}