/* * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/** * An expression capable of evaluating itself against context objects. * Encapsulates the details of a previously parsed expression string. * Provides a common abstraction for expression evaluation. * * @author Keith Donald * @author Andy Clement * @author Juergen Hoeller * @since 3.0 */ publicinterfaceExpression {
/** * Return the original string used to create this expression (unmodified). * @return the original expression string */ String getExpressionString();
/** * Evaluate this expression in the default standard context. * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable Object getValue()throws EvaluationException;
/** * Evaluate the expression in the default context. If the result * of the evaluation does not match (and cannot be converted to) * the expected result type then an exception will be returned. * @param desiredResultType the class the caller would like the result to be * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable <T> T getValue(@Nullable Class<T> desiredResultType)throws EvaluationException;
/** * Evaluate this expression against the specified root object. * @param rootObject the root object against which to evaluate the expression * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable Object getValue(@Nullable Object rootObject)throws EvaluationException;
/** * Evaluate the expression in the default context against the specified root * object. If the result of the evaluation does not match (and cannot be * converted to) the expected result type then an exception will be returned. * @param rootObject the root object against which to evaluate the expression * @param desiredResultType the class the caller would like the result to be * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable <T> T getValue(@Nullable Object rootObject, @Nullable Class<T> desiredResultType)throws EvaluationException;
/** * Evaluate this expression in the provided context and return the result * of evaluation. * @param context the context in which to evaluate the expression * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable Object getValue(EvaluationContext context)throws EvaluationException;
/** * Evaluate this expression in the provided context and return the result * of evaluation, but use the supplied root context as an override for any * default root object specified in the context. * @param context the context in which to evaluate the expression * @param rootObject the root object against which to evaluate the expression * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable Object getValue(EvaluationContext context, @Nullable Object rootObject)throws EvaluationException;
/** * Evaluate the expression in a specified context which can resolve references * to properties, methods, types, etc. The type of the evaluation result is * expected to be of a particular class and an exception will be thrown if it * is not and cannot be converted to that type. * @param context the context in which to evaluate the expression * @param desiredResultType the class the caller would like the result to be * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable <T> T getValue(EvaluationContext context, @Nullable Class<T> desiredResultType)throws EvaluationException;
/** * Evaluate the expression in a specified context which can resolve references * to properties, methods, types, etc. The type of the evaluation result is * expected to be of a particular class and an exception will be thrown if it * is not and cannot be converted to that type. The supplied root object * overrides any default specified on the supplied context. * @param context the context in which to evaluate the expression * @param rootObject the root object against which to evaluate the expression * @param desiredResultType the class the caller would like the result to be * @return the evaluation result * @throws EvaluationException if there is a problem during evaluation */ @Nullable <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException;
/** * Return the most general type that can be passed to a {@link #setValue} * method using the default context. * @return the most general type of value that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable Class<?> getValueType() throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(Object, Object)} method using the default context. * @param rootObject the root object against which to evaluate the expression * @return the most general type of value that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable Class<?> getValueType(@Nullable Object rootObject) throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(EvaluationContext, Object)} method for the given context. * @param context the context in which to evaluate the expression * @return the most general type of value that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable Class<?> getValueType(EvaluationContext context) throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(EvaluationContext, Object, Object)} method for the given * context. The supplied root object overrides any specified in the context. * @param context the context in which to evaluate the expression * @param rootObject the root object against which to evaluate the expression * @return the most general type of value that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable Class<?> getValueType(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException;
/** * Return the most general type that can be passed to a {@link #setValue} * method using the default context. * @return a type descriptor for values that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable TypeDescriptor getValueTypeDescriptor()throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(Object, Object)} method using the default context. * @param rootObject the root object against which to evaluate the expression * @return a type descriptor for values that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable TypeDescriptor getValueTypeDescriptor(@Nullable Object rootObject)throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(EvaluationContext, Object)} method for the given context. * @param context the context in which to evaluate the expression * @return a type descriptor for values that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable TypeDescriptor getValueTypeDescriptor(EvaluationContext context)throws EvaluationException;
/** * Return the most general type that can be passed to the * {@link #setValue(EvaluationContext, Object, Object)} method for the given * context. The supplied root object overrides any specified in the context. * @param context the context in which to evaluate the expression * @param rootObject the root object against which to evaluate the expression * @return a type descriptor for values that can be set on this context * @throws EvaluationException if there is a problem determining the type */ @Nullable TypeDescriptor getValueTypeDescriptor(EvaluationContext context, @Nullable Object rootObject)throws EvaluationException;
/** * Determine if an expression can be written to, i.e. setValue() can be called. * @param rootObject the root object against which to evaluate the expression * @return {@code true} if the expression is writable; {@code false} otherwise * @throws EvaluationException if there is a problem determining if it is writable */ booleanisWritable(@Nullable Object rootObject)throws EvaluationException;
/** * Determine if an expression can be written to, i.e. setValue() can be called. * @param context the context in which the expression should be checked * @return {@code true} if the expression is writable; {@code false} otherwise * @throws EvaluationException if there is a problem determining if it is writable */ booleanisWritable(EvaluationContext context)throws EvaluationException;
/** * Determine if an expression can be written to, i.e. setValue() can be called. * The supplied root object overrides any specified in the context. * @param context the context in which the expression should be checked * @param rootObject the root object against which to evaluate the expression * @return {@code true} if the expression is writable; {@code false} otherwise * @throws EvaluationException if there is a problem determining if it is writable */ booleanisWritable(EvaluationContext context, @Nullable Object rootObject)throws EvaluationException;
/** * Set this expression in the provided context to the value provided. * @param rootObject the root object against which to evaluate the expression * @param value the new value * @throws EvaluationException if there is a problem during evaluation */ voidsetValue(@Nullable Object rootObject, @Nullable Object value)throws EvaluationException;
/** * Set this expression in the provided context to the value provided. * @param context the context in which to set the value of the expression * @param value the new value * @throws EvaluationException if there is a problem during evaluation */ voidsetValue(EvaluationContext context, @Nullable Object value)throws EvaluationException;
/** * Set this expression in the provided context to the value provided. * The supplied root object overrides any specified in the context. * @param context the context in which to set the value of the expression * @param rootObject the root object against which to evaluate the expression * @param value the new value * @throws EvaluationException if there is a problem during evaluation */ voidsetValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Object value)throws EvaluationException;
```java /* * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package org.springframework.expression;
import java.util.List;
import org.springframework.lang.Nullable;
/** * Expressions are executed in an evaluation context. It is in this context that * references are resolved when encountered during expression evaluation. * * <p>There is a default implementation of this EvaluationContext interface: * {@link org.springframework.expression.spel.support.StandardEvaluationContext} * which can be extended, rather than having to implement everything manually. * * @author Andy Clement * @author Juergen Hoeller * @since 3.0 */ public interface EvaluationContext {
/** * Return the default root context object against which unqualified * properties/methods/etc should be resolved. This can be overridden * when evaluating an expression. */ TypedValue getRootObject();
/** * Return a list of accessors that will be asked in turn to read/write a property. */ List<PropertyAccessor> getPropertyAccessors();
/** * Return a list of resolvers that will be asked in turn to locate a constructor. */ List<ConstructorResolver> getConstructorResolvers();
/** * Return a list of resolvers that will be asked in turn to locate a method. */ List<MethodResolver> getMethodResolvers();
/** * Return a bean resolver that can look up beans by name. */ @Nullable BeanResolver getBeanResolver();
/** * Return a type locator that can be used to find types, either by short or * fully qualified name. */ TypeLocator getTypeLocator();
/** * Return a type converter that can convert (or coerce) a value from one type to another. */ TypeConverter getTypeConverter();
/** * Return a type comparator for comparing pairs of objects for equality. */ TypeComparator getTypeComparator();
/** * Return an operator overloader that may support mathematical operations * between more than the standard set of types. */ OperatorOverloader getOperatorOverloader();
/** * Set a named variable within this evaluation context to a specified value. * @param name the name of the variable to set * @param value the value to be placed in the variable */ void setVariable(String name, @Nullable Object value);
/** * Look up a named variable within this evaluation context. * @param name variable to lookup * @return the value of the variable, or {@code null} if not found */ @Nullable Object lookupVariable(String name);
/* LOGIC SpEL:我自己所形成的一套理解操作,关于实现SpEL表达式所进行的理解操作,使用相关英文的理解形式来进行求解操作 **/ System.out.println(expression.parseExpression("(99 GT (33-20)) AND true").getValue(Boolean.class)); System.out.println(expression.parseExpression("NOT (2 BETWEEN {2,3})").getValue(Boolean.class)); System.out.println(expression.parseExpression("6>8 AND (1 GT 2) || ((2 LT 3) AND NOT true)").getValue(Boolean.class));
} }
BETWEEN 是两个都是闭区间
逻辑表达式
&& || ! AND OR NOT也可以进行逻辑操作
1 2 3 4 5 6 7
/* LOGIC SpEL:我自己所形成的一套理解操作,关于实现SpEL表达式所进行的理解操作,使用相关英文的理解形式来进行求解操作 **/ System.out.println(expression.parseExpression("(99 GT (33-20)) AND true").getValue(Boolean.class)); System.out.println(expression.parseExpression("NOT (2 BETWEEN {2,3})").getValue(Boolean.class)); System.out.println(expression.parseExpression("6>8 AND (1 GT 2) || ((2 LT 3) AND NOT true)").getValue(Boolean.class));
/** * Returns {@code true} if the arguments are equal to each other * and {@code false} otherwise. * Consequently, if both arguments are {@code null}, {@code true} * is returned. Otherwise, if the first argument is not {@code * null}, equality is determined by calling the {@link * Object#equals equals} method of the first argument with the * second argument of this method. Otherwise, {@code false} is * returned. * * @param a an object * @param b an object to be compared with {@code a} for equality * @return {@code true} if the arguments are equal to each other * and {@code false} otherwise * @see Object#equals(Object) */ publicstaticbooleanequals(Object a, Object b) { return (a == b) || (a != null && a.equals(b)); }
@Data publicclasstheBicycleInfo { public String name; public Integer age; public String theBicycleType; public String Description; public theBicycleFactoryInfo theBicycleFactoryInfo; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.example.springdemo.entity.SpEL.pojo;
import lombok.Builder; import lombok.Data;
@Data @Builder publicclasstheBicycleFactoryInfo { public String factoryName; public Integer factoryPrice; public String FactoryLocation; }
/* * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
/** * Standard implementation of the * {@link org.springframework.beans.factory.config.BeanExpressionResolver} * interface, parsing and evaluating Spring EL using Spring's expression module. * * <p>All beans in the containing {@code BeanFactory} are made available as * predefined variables with their common bean name, including standard context * beans such as "environment", "systemProperties" and "systemEnvironment". * * @author Juergen Hoeller * @since 3.0 * @see BeanExpressionContext#getBeanFactory() * @see org.springframework.expression.ExpressionParser * @see org.springframework.expression.spel.standard.SpelExpressionParser * @see org.springframework.expression.spel.support.StandardEvaluationContext */ publicclassStandardBeanExpressionResolverimplementsBeanExpressionResolver {
/** * Create a new {@code StandardBeanExpressionResolver} with default settings. */ publicStandardBeanExpressionResolver() { this.expressionParser = newSpelExpressionParser(); }
/** * Create a new {@code StandardBeanExpressionResolver} with the given bean class loader, * using it as the basis for expression compilation. * @param beanClassLoader the factory's bean class loader */ publicStandardBeanExpressionResolver(@Nullable ClassLoader beanClassLoader) { this.expressionParser = newSpelExpressionParser(newSpelParserConfiguration(null, beanClassLoader)); }
/** * Set the prefix that an expression string starts with. * The default is "#{". * @see #DEFAULT_EXPRESSION_PREFIX */ publicvoidsetExpressionPrefix(String expressionPrefix) { Assert.hasText(expressionPrefix, "Expression prefix must not be empty"); this.expressionPrefix = expressionPrefix; }
/** * Set the suffix that an expression string ends with. * The default is "}". * @see #DEFAULT_EXPRESSION_SUFFIX */ publicvoidsetExpressionSuffix(String expressionSuffix) { Assert.hasText(expressionSuffix, "Expression suffix must not be empty"); this.expressionSuffix = expressionSuffix; }
/** * Specify the EL parser to use for expression parsing. * <p>Default is a {@link org.springframework.expression.spel.standard.SpelExpressionParser}, * compatible with standard Unified EL style expression syntax. */ publicvoidsetExpressionParser(ExpressionParser expressionParser) { Assert.notNull(expressionParser, "ExpressionParser must not be null"); this.expressionParser = expressionParser; }