2013-11-26 35 views
1

我是新來的Java,Spring和規劃環境地政司,我不能讓這個簡單的代碼工作(它的工作原理沒有評估進口THO)春規劃環境地政司錯誤

這是我的課RunSpring.java:

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({)'

任何提示?

謝謝你的時間。

回答

2

調整greeeting方法如下。請注意0​​傳遞給getValue()

instance.greeting("5+5"); 
+0

任何運氣這樣的:

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); } 

然後打電話? –

+0

其實它應該是'parser.parseExpression(name)':) –

+0

@kocko謝謝你的指出。這是從我做的測試中遺留下來的。 –