Java 弹簧速度误差
我不熟悉Java、Spring和SpEL,我无法让这个简单的代码正常工作(它在没有评估的情况下工作) 这是我的类RunSpring.java:Java 弹簧速度误差,java,spring,spring-el,Java,Spring,Spring El,我不熟悉Java、Spring和SpEL,我无法让这个简单的代码正常工作(它在没有评估的情况下工作) 这是我的类RunSpring.java: package run; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXml
package run;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import helloworld.HelloSpringWorld;
public class RunSpring
{
public static void main(String[] args)
{
//App Context
ApplicationContext appContext = new ClassPathXmlApplicationContext("bean-data.xml");
BeanFactory beanFactory = appContext;
HelloSpringWorld instance = (HelloSpringWorld);
beanFactory.getBean("helloSpringWorld");
//Expression to be evaluated
instance.greeting("${5+5}");
}
}
这是我的班级HelloSpringWorld:
package helloworld;
import org.springframework.stereotype.Service;
//Expression imports
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@Service
public class HelloSpringWorld
{
public void greeting(String name)
{
//Expression Setup
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(name);
String message = (String) exp.getValue();
System.out.println("Hello and welcome to Spring: " + message);
}
}
错误:解析有效表达式后,表达式中仍有更多数据:'lcurly({)'
有什么提示吗
谢谢您的时间。如下调整
greeeting
方法。请注意Integer.class
传递给getValue()
public void greeting(String name)
{
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(name);
String message = exp.getValue(Integer.class).toString();
System.out.println("Hello and welcome to Spring: " + message);
}
然后致电:
instance.greeting("5+5");
实际上,它应该是parser.parseExpression(name):)@kocko,谢谢你指出这一点。它是我做测试时留下的。