org.springframework.context.expression.BeanFactoryResolver Java Examples

The following examples show how to use org.springframework.context.expression.BeanFactoryResolver. 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: SpELFunction.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 创建SpEL执行上下文
 *
 * @param rootObject
 *            SpEL表达式根对象
 * @param context
 *            Beetl上下文对象
 * @return SpEL表达式执行上下文
 */
private EvaluationContext createEvaluationContext(Object rootObject, Context beetlContext) {
	StandardEvaluationContext context = new StandardEvaluationContext(rootObject);

	// 允许使用#context访问Beetl上下文
	context.setVariable("context", beetlContext);
	// 允许使用#global访问Beetl上下文的全局变量
	context.setVariable("global", beetlContext.globalVar);
	// 注册WebRender定义的全局变量
	context.setVariable("ctxPath", beetlContext.getGlobal("ctxPath"));
	context.setVariable("servlet", beetlContext.getGlobal("servlet"));
	context.setVariable("parameter", beetlContext.getGlobal("parameter"));
	context.setVariable("request", beetlContext.getGlobal("request"));
	context.setVariable("session", beetlContext.getGlobal("session"));
	// 允许使用属性格式访问Map
	context.addPropertyAccessor(new MapAccessor());
	// 允许访问Spring容器Bean
	context.setBeanResolver(new BeanFactoryResolver(applicationContext));

	return context;
}
 
Example #2
Source File: ExpressionEvaluatorTest.java    From eclair with Apache License 2.0 6 votes vote down vote up
@Test
public void evaluateToString() {
    // given
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(expressionParser, evaluationContext);

    String string = "toString()";
    String string2 = "#this";
    String string3 = "#root";
    Argument argument = new Argument();
    // when
    Object result = expressionEvaluator.evaluate(string, argument);
    Object result2 = expressionEvaluator.evaluate(string2, argument);
    Object result3 = expressionEvaluator.evaluate(string3, argument);
    // then
    assertThat(result, is("!"));
    assertThat(result2, is(argument));
    assertThat(result3, is(argument));
}
 
Example #3
Source File: Bucket4JBaseConfiguration.java    From bucket4j-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
private List<MetricTagResult> getMetricTags(ExpressionParser expressionParser, 
		ConfigurableBeanFactory beanFactory,
		FilterConfiguration<R> filterConfig, 
		R servletRequest) {
	
	List<MetricTagResult> metricTagResults = filterConfig
		.getMetrics().getTags()
		.stream()
		.map( (metricMetaTag) -> {
			String expression = metricMetaTag.getExpression();
			if(StringUtils.isEmpty(expression)) {
				throw new MissingMetricTagExpressionException(metricMetaTag.getKey());
			}
			StandardEvaluationContext context = new StandardEvaluationContext();
			context.setBeanResolver(new BeanFactoryResolver(beanFactory));
			//TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
			Expression expr = expressionParser.parseExpression(expression); 
			final String value = expr.getValue(context, servletRequest, String.class);
			
			return new MetricTagResult(metricMetaTag.getKey(), value, metricMetaTag.getTypes());
	}).collect(toList());
	if(metricTagResults == null) {
		metricTagResults = new ArrayList<>();
	}
	return metricTagResults;
}
 
Example #4
Source File: Bucket4JBaseConfiguration.java    From bucket4j-spring-boot-starter with Apache License 2.0 6 votes vote down vote up
/**
 * Creates the key filter lambda which is responsible to decide how the rate limit will be performed. The key
 * is the unique identifier like an IP address or a username.
 * 
 * @param url is used to generated a unique cache key
 * @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
 * @param expressionParser is used to evaluate the expression if the filter key type is EXPRESSION.
 * @param beanFactory used to get full access to all java beans in the SpEl
 * @return should not been null. If no filter key type is matching a plain 1 is returned so that all requests uses the same key.
 */
public KeyFilter<R> getKeyFilter(String url, RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
	
	String expression = rateLimit.getExpression();
	if(StringUtils.isEmpty(expression)) {
		throw new MissingKeyFilterExpressionException();
	}
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setBeanResolver(new BeanFactoryResolver(beanFactory));
	return  (request) -> {
		//TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
		Expression expr = expressionParser.parseExpression(rateLimit.getExpression()); 
		final String value = expr.getValue(context, request, String.class);
		return url + "-" + value;
	};

	
}
 
Example #5
Source File: ConfigurationUtils.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
private static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory,
                               String entryValue) {
    Object value;
    String rawValue = entryValue;
    if (rawValue != null) {
        rawValue = rawValue.trim();
    }
    if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {
        // assume it's spel
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setBeanResolver(new BeanFactoryResolver(beanFactory));
        Expression expression = parser.parseExpression(entryValue,
                new TemplateParserContext());
        context.lookupVariable("api");
        value = expression.getValue(context);
    } else {
        value = entryValue;
    }
    return value;
}
 
Example #6
Source File: CacheOperationExpressionEvaluator.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 * @param caches the current caches
 * @param method the method
 * @param args the method arguments
 * @param target the target object
 * @param targetClass the target class
 * @param result the return value (can be {@code null}) or
 * {@link #NO_RESULT} if there is no return at this time
 * @return the evaluation context
 */
public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches,
		Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
		@Nullable Object result, @Nullable BeanFactory beanFactory) {

	CacheExpressionRootObject rootObject = new CacheExpressionRootObject(
			caches, method, args, target, targetClass);
	CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
			rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (result == RESULT_UNAVAILABLE) {
		evaluationContext.addUnavailableVariable(RESULT_VARIABLE);
	}
	else if (result != NO_RESULT) {
		evaluationContext.setVariable(RESULT_VARIABLE, result);
	}
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #7
Source File: CacheOperationExpressionEvaluator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 * @param caches the current caches
 * @param method the method
 * @param args the method arguments
 * @param target the target object
 * @param targetClass the target class
 * @param result the return value (can be {@code null}) or
 * {@link #NO_RESULT} if there is no return at this time
 * @return the evaluation context
 */
public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches,
		Method method, Object[] args, Object target, Class<?> targetClass, Object result,
		BeanFactory beanFactory) {

	CacheExpressionRootObject rootObject = new CacheExpressionRootObject(
			caches, method, args, target, targetClass);
	Method targetMethod = getTargetMethod(targetClass, method);
	CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
			rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (result == RESULT_UNAVAILABLE) {
		evaluationContext.addUnavailableVariable(RESULT_VARIABLE);
	}
	else if (result != NO_RESULT) {
		evaluationContext.setVariable(RESULT_VARIABLE, result);
	}
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #8
Source File: ShortcutConfigurable.java    From spring-cloud-gateway with Apache License 2.0 6 votes vote down vote up
static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory,
		String entryValue) {
	Object value;
	String rawValue = entryValue;
	if (rawValue != null) {
		rawValue = rawValue.trim();
	}
	if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {
		// assume it's spel
		StandardEvaluationContext context = new StandardEvaluationContext();
		context.setBeanResolver(new BeanFactoryResolver(beanFactory));
		Expression expression = parser.parseExpression(entryValue,
				new TemplateParserContext());
		value = expression.getValue(context);
	}
	else {
		value = entryValue;
	}
	return value;
}
 
Example #9
Source File: DatastoreRepositoryFactory.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private QueryMethodEvaluationContextProvider delegateContextProvider(
		QueryMethodEvaluationContextProvider evaluationContextProvider) {
	return new QueryMethodEvaluationContextProvider() {
		@Override
		public <T extends Parameters<?, ?>> EvaluationContext getEvaluationContext(
				T parameters, Object[] parameterValues) {
			StandardEvaluationContext evaluationContext = (StandardEvaluationContext)
					evaluationContextProvider
					.getEvaluationContext(parameters, parameterValues);
			evaluationContext.setRootObject(
					DatastoreRepositoryFactory.this.applicationContext);
			evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
			evaluationContext.setBeanResolver(new BeanFactoryResolver(
					DatastoreRepositoryFactory.this.applicationContext));
			return evaluationContext;
		}
	};
}
 
Example #10
Source File: SpannerRepositoryFactory.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
private QueryMethodEvaluationContextProvider delegateContextProvider(
		QueryMethodEvaluationContextProvider evaluationContextProvider) {
	return new QueryMethodEvaluationContextProvider() {
		@Override
		public <T extends Parameters<?, ?>> EvaluationContext getEvaluationContext(
				T parameters, Object[] parameterValues) {
			StandardEvaluationContext evaluationContext = (StandardEvaluationContext) evaluationContextProvider
					.getEvaluationContext(parameters, parameterValues);
			evaluationContext
					.setRootObject(SpannerRepositoryFactory.this.applicationContext);
			evaluationContext.addPropertyAccessor(new BeanFactoryAccessor());
			evaluationContext.setBeanResolver(new BeanFactoryResolver(
					SpannerRepositoryFactory.this.applicationContext));
			return evaluationContext;
		}
	};
}
 
Example #11
Source File: CacheOperationExpressionEvaluator.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 * @param caches the current caches
 * @param method the method
 * @param args the method arguments
 * @param target the target object
 * @param targetClass the target class
 * @param result the return value (can be {@code null}) or
 * {@link #NO_RESULT} if there is no return at this time
 * @return the evaluation context
 */
public EvaluationContext createEvaluationContext(Collection<? extends Cache> caches,
		Method method, Object[] args, Object target, Class<?> targetClass, Method targetMethod,
		@Nullable Object result, @Nullable BeanFactory beanFactory) {

	CacheExpressionRootObject rootObject = new CacheExpressionRootObject(
			caches, method, args, target, targetClass);
	CacheEvaluationContext evaluationContext = new CacheEvaluationContext(
			rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (result == RESULT_UNAVAILABLE) {
		evaluationContext.addUnavailableVariable(RESULT_VARIABLE);
	}
	else if (result != NO_RESULT) {
		evaluationContext.setVariable(RESULT_VARIABLE, result);
	}
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #12
Source File: SimpleSolrPersistentEntity.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

	context.addPropertyAccessor(new BeanFactoryAccessor());
	context.setBeanResolver(new BeanFactoryResolver(applicationContext));
	context.setRootObject(applicationContext);
}
 
Example #13
Source File: EventExpressionEvaluator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create the suitable {@link EvaluationContext} for the specified event handling
 * on the specified method.
 */
public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass,
		Method method, Object[] args, BeanFactory beanFactory) {

	Method targetMethod = getTargetMethod(targetClass, method);
	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
			root, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #14
Source File: DatastorePersistentEntityImpl.java    From spring-cloud-gcp with Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(ApplicationContext applicationContext)
		throws BeansException {
	this.context.addPropertyAccessor(new BeanFactoryAccessor());
	this.context.setBeanResolver(new BeanFactoryResolver(applicationContext));
	this.context.setRootObject(applicationContext);
}
 
Example #15
Source File: EvalTag.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #16
Source File: Bucket4JBaseConfiguration.java    From bucket4j-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the lambda for the execute condition which will be evaluated on each request.
 * 
 * @param rateLimit the {@link RateLimit} configuration which holds the execute condition string
 * @param expressionParser is used to evaluate the execution expression
 * @param beanFactory used to get full access to all java beans in the SpEl
 * @return the lamdba condition which will be evaluated lazy - null if there is no condition available.
 */
public Condition<R> executeCondition(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setBeanResolver(new BeanFactoryResolver(beanFactory));
	
	if(rateLimit.getExecuteCondition() != null) {
		return (request) -> {
			Expression expr = expressionParser.parseExpression(rateLimit.getExecuteCondition()); 
			Boolean value = expr.getValue(context, request, Boolean.class);
			return value;
		};
	}
	return null;
}
 
Example #17
Source File: Bucket4JBaseConfiguration.java    From bucket4j-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the lambda for the skip condition which will be evaluated on each request
 * 
 * @param rateLimit the {@link RateLimit} configuration which holds the skip condition string
 * @param expressionParser is used to evaluate the skip expression
 * @param beanFactory used to get full access to all java beans in the SpEl
 * @return the lamdba condition which will be evaluated lazy - null if there is no condition available.
 */
public Condition<R> skipCondition(RateLimit rateLimit, ExpressionParser expressionParser, BeanFactory beanFactory) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setBeanResolver(new BeanFactoryResolver(beanFactory));
	
	if(rateLimit.getSkipCondition() != null) {
		return  (request) -> {
			Expression expr = expressionParser.parseExpression(rateLimit.getSkipCondition()); 
			Boolean value = expr.getValue(context, request, Boolean.class);
			return value;
		};
	}
	return null;
}
 
Example #18
Source File: EvalTag.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #19
Source File: ArangoTemplate.java    From spring-data with Apache License 2.0 5 votes vote down vote up
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
	context.setRootObject(applicationContext);
	context.setBeanResolver(new BeanFactoryResolver(applicationContext));
	context.addPropertyAccessor(new BeanFactoryAccessor());
	eventPublisher = applicationContext;
	arango._setCursorInitializer(new ArangoCursorInitializer(converter, applicationContext));
}
 
Example #20
Source File: ELExample.java    From pragmatic-java-engineer with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
    ctx.refresh();
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new BeanFactoryResolver(ctx));
    Properties result = parser.parseExpression("@systemProperties").getValue(context, Properties.class);
    Assert.assertEquals(System.getProperties(), result);
}
 
Example #21
Source File: MicaExpressionEvaluator.java    From mica with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Create an {@link EvaluationContext}.
 *
 * @param method      the method
 * @param args        the method arguments
 * @param target      the target object
 * @param targetClass the target class
 * @return the evaluation context
 */
public EvaluationContext createContext(Method method, Object[] args, Object target, Class<?> targetClass, @Nullable BeanFactory beanFactory) {
	Method targetMethod = getTargetMethod(targetClass, method);
	MicaExpressionRootObject rootObject = new MicaExpressionRootObject(method, args, target, targetClass, targetMethod);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(rootObject, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}
	return evaluationContext;
}
 
Example #22
Source File: ExpressionEvaluatorTest.java    From eclair with Apache License 2.0 5 votes vote down vote up
@Test
public void evaluateWithArgumentAndBeanReferencing() {
    // given
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(expressionParser, evaluationContext);

    String string = "@string.toString()";
    Argument argument = new Argument();
    // when
    Object result = expressionEvaluator.evaluate(string, argument);
    // then
    assertThat(result, is("bean string"));
}
 
Example #23
Source File: ExpressionEvaluatorTest.java    From eclair with Apache License 2.0 5 votes vote down vote up
@Test
public void evaluateWithBeanReferencing() {
    // given
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(expressionParser, evaluationContext);

    String string = "@string.toString()";
    // when
    Object result = expressionEvaluator.evaluate(string);
    // then
    assertThat(result, is("bean string"));
}
 
Example #24
Source File: EclairAutoConfiguration.java    From eclair with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public ExpressionEvaluator expressionEvaluator() {
    ExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.MIXED, null));
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
    evaluationContext.setBeanResolver(new BeanFactoryResolver(applicationContext));
    return new ExpressionEvaluator(expressionParser, evaluationContext);
}
 
Example #25
Source File: AuthAspect.java    From blade-tool with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取方法上的参数
 *
 * @param method 方法
 * @param args   变量
 * @return {SimpleEvaluationContext}
 */
private StandardEvaluationContext getEvaluationContext(Method method, Object[] args) {
	// 初始化Sp el表达式上下文,并设置 AuthFun
	StandardEvaluationContext context = new StandardEvaluationContext(new AuthFun());
	// 设置表达式支持spring bean
	context.setBeanResolver(new BeanFactoryResolver(applicationContext));
	for (int i = 0; i < args.length; i++) {
		// 读取方法参数
		MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
		// 设置方法 参数名和值 为sp el变量
		context.setVariable(methodParam.getParameterName(), args[i]);
	}
	return context;
}
 
Example #26
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;
}
 
Example #27
Source File: EvalTag.java    From java-technology-stack with MIT License 5 votes vote down vote up
private EvaluationContext createEvaluationContext(PageContext pageContext) {
	StandardEvaluationContext context = new StandardEvaluationContext();
	context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
	context.addPropertyAccessor(new MapAccessor());
	context.addPropertyAccessor(new EnvironmentAccessor());
	context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
	ConversionService conversionService = getConversionService(pageContext);
	if (conversionService != null) {
		context.setTypeConverter(new StandardTypeConverter(conversionService));
	}
	return context;
}
 
Example #28
Source File: EventExpressionEvaluator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Specify if the condition defined by the specified expression matches.
 */
public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod,
		AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) {

	EventExpressionRootObject root = new EventExpressionRootObject(event, args);
	MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(
			root, targetMethod, args, getParameterNameDiscoverer());
	if (beanFactory != null) {
		evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
	}

	return (Boolean.TRUE.equals(getExpression(this.conditionCache, methodKey, conditionExpression).getValue(
			evaluationContext, Boolean.class)));
}
 
Example #29
Source File: SecureCheckUtil.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 设置 request/response 参数的上下文
 *
 * @param secureExpressionHandler 上下文处理器
 * @param request                 请求
 * @param response                响应
 * @return 上下文
 */
public static StandardEvaluationContext getEvaluationContext(SecureExpressionHandler secureExpressionHandler, HttpServletRequest request, HttpServletResponse response) {
	// 初始化Sp el表达式上下文,并设置 处理函数
	StandardEvaluationContext context = new StandardEvaluationContext(secureExpressionHandler);
	// 设置表达式支持spring bean
	context.setBeanResolver(new BeanFactoryResolver(SpringUtil.getContext()));
	context.setVariable(SecureConstants.REQUEST, request);
	context.setVariable(SecureConstants.RESPONSE, response);
	return context;
}
 
Example #30
Source File: SecureCheckUtil.java    From magic-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 包含方法上的参数的上下文
 *
 * @param secureExpressionHandler 上下文处理器
 * @param method                  方法
 * @param args                    参数
 * @return 上下文
 */
public static StandardEvaluationContext getEvaluationContext(SecureExpressionHandler secureExpressionHandler, Method method, Object[] args) {
	// 初始化Spel表达式上下文,并设置 处理函数
	StandardEvaluationContext context = new StandardEvaluationContext(secureExpressionHandler);
	// 设置表达式支持spring bean
	context.setBeanResolver(new BeanFactoryResolver(SpringUtil.getContext()));
	for (int i = 0; i < args.length; i++) {
		// 读取方法参数
		MethodParameter methodParam = ClassUtil.getMethodParameter(method, i);
		// 设置方法 参数名和值 为sp el变量
		context.setVariable(methodParam.getParameterName(), args[i]);
	}
	return context;
}