Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring SpEL-如何使用SpEL解析消息_Java_Spring - Fatal编程技术网

Java Spring SpEL-如何使用SpEL解析消息

Java Spring SpEL-如何使用SpEL解析消息,java,spring,Java,Spring,我正在尝试使用SpringSpel解析UDP中接收到的消息 为了了解如何使用Spring SpEL,我写了以下内容: context.xml: <bean id="message" class="springsimulator.Message"> <property name="strMessage" value="$TEST,11,22,33"/> </bean> <bean id="nmeamessage" class="springsimu

我正在尝试使用SpringSpel解析UDP中接收到的消息

为了了解如何使用Spring SpEL,我写了以下内容:

context.xml:

<bean id="message" class="springsimulator.Message">
    <property name="strMessage" value="$TEST,11,22,33"/>
</bean>
<bean id="nmeamessage" class="springsimulator.NMEAMessage">
    <property name="fields" value="#{message.strMessage.split(',')}"/>
</bean>
<bean id="parser" class="springsimulator.SPELParser">
    <property name="values">
        <map>
            <entry key="val1" value="#{nmeamessage.fields[1]}"/>
            <entry key="val2" value="#{nmeamessage.fields[2]}"/>
        </map>
    </property>     
</bean>
N留言:

public class NMEAMessage {
    public String[] fields;
    public void setFields(String[] fields) {
        this.fields = fields;
    }
}
解析器:

public class Parser {

    Map<String,String> values;

    public void setValues(Map<String, String> values) {
        this.values = values;
    }

    public String toString() {
        String message = "";
        for (Entry<String, String> entry : values.entrySet()) {
            message += entry.getKey() + ":" + entry.getValue() + "\n";
        }
        return message;
    }
}
这是有效的,我已经解析了我的消息

但是现在,我想在每次收到消息时在循环中计算SpEL表达式

while(running) {
    socket.receive(message)
    //Split message to get fields
    //set fields into a map
}
使用SpEL和context.xml是否有合适的方法来实现这一点


感谢要在运行时解析SpEL表达式,请执行以下操作:

// Setup the SpEL parser: do this once
SpelParserConfiguration spelParserConfiguration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader());
ExpressionParser expressionParser = new SpelExpressionParser(spelParserConfiguration);
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();

// Parse (compile) the expression: try to do this once
Expression expression = expressionParser.parseExpression(unevaluatedExpression)

// Then within the loop ...

// Supply context, like the value of your namespace
evaluationContext.setVariable(variableName, value);

// Evaluate an expression as many times as you like
Object result = expression.getValue(evaluationContext);
while(running) {
    socket.receive(message)
    //Split message to get fields
    //set fields into a map
}
// Setup the SpEL parser: do this once
SpelParserConfiguration spelParserConfiguration = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, getClass().getClassLoader());
ExpressionParser expressionParser = new SpelExpressionParser(spelParserConfiguration);
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();

// Parse (compile) the expression: try to do this once
Expression expression = expressionParser.parseExpression(unevaluatedExpression)

// Then within the loop ...

// Supply context, like the value of your namespace
evaluationContext.setVariable(variableName, value);

// Evaluate an expression as many times as you like
Object result = expression.getValue(evaluationContext);