Spring没有正确实例化对象。为什么?

时间:2014-03-31 16:32:02

标签: java spring maven spring-mvc zk

我现在正在学习如何使用ZKoss,Maven和Spring,我有一些我不理解的问题,如果有人可以帮助我,我将非常感激。 对我来说,看起来Spring并没有在“CalculadoraServlet”中正确地实例化我的计算器对象。

以下是代码:

Zkoss(inidex.zul):

<?page title="Auto Generated index.zul"?>

<window title="Calculadora" border="normal" width="400px" apply="sdec.CalculadoraServlet">

    <grid >
      <rows>
        <row>
            Num1: <textbox id="num1" value="${win$composer.current.num1}" />
        </row>
        <row>
            Num2: <textbox id="num2" value="${win$composer.current.num2}" />
        </row>  
      </rows>
    </grid>
    <button id="submitButton" label="Submit"/> 

    <grid >
      <rows>
        <row>
            Resultado <textbox id="result" value="${win$composer.current.result}" />
        </row>
      </rows>
    </grid>
</window>

CalculadoraServlet:

@Controller
public class CalculadoraServlet extends SelectorComposer<Component>{
 /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
    private Calculadora servicioCalculadora;


        @Wire
        private Textbox num1;
        @Wire
        private Textbox num2;
        @Wire
        private Textbox result;


     @Listen("onClick = #submitButton")
     public void calcula() throws IOException {

        int numero1=Integer.parseInt(num1.getValue());
        int numero2=Integer.parseInt(num2.getValue());

        int resultado=servicioCalculadora.sumar(numero1, numero2);

        String resul=Integer.toString(resultado);
        result.setValue(resul);
        System.out.println(resultado);

     }

}

最终的计算方法:

package sdec;

public class Calculadora {

        public Calculadora(){

        //"Traza". Se mostrará cada vez que inicie la app y spring instancie un obj calculadora
            System.out.println("Calculadora inicializada por Spring");
        }

        public int sumar(int num1, int num2){

            return num1+num2;
        }
}

提前多多谢谢!!我是一名初级程序员,这有点难以理解! =(

编辑:(添加applicationContext文件):

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"  
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:oxm="http://www.springframework.org/schema/oxm"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <context:annotation-config />

 <context:component-scan base-package="sdec" />

 <bean id="servicioCalculadora" class="sdec.Calculadora"/>

</beans>

当我按下“提交”sk按钮时,我得到的错误是可怕的nullpointer异常:

abr 01, 2014 9:41:30 AM org.zkoss.zk.ui.impl.UiEngineImpl handleError:1359
Grave: >>java.lang.NullPointerException
>>  at sdec.CalculadoraServlet.calcula(CalculadoraServlet.java:37)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>>  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>>  at java.lang.reflect.Method.invoke(Method.java:606)
...

对每个人都有很多帮助!真!这一定是最有帮助的!

1 个答案:

答案 0 :(得分:1)

奥凯,

首先,松开@Controller中的CalculadoraServlet 其次,你尝试从spring容器初始化一个spring bean,zk有一个zk容器,所以zk找不到你的Calculadora类是正常的。

您必须将@WireVariable用于Calculadora类。

所以它应该是这样的:

public class CalculadoraServlet extends SelectorComposer<Component>{

    private static final long serialVersionUID = 1L;

    @WireVariable
    private Calculadora servicioCalculadora;

    ....
}