0

我正在将我的知识转移到春天,我真的不明白这是如何工作的。我正在尝试制作一个非常基本的示例以使其工作,但我无法使其工作:-((

应用程序上下文.xml

<bean id="serviceTest" class="es.mov.jose.agenda.ServiceTest">
</bean>

    <bean id="serviceMovility" class="es.mov.jose.agenda.ServiceMovility">
    <property name="serviceTest" ref="serviceTest" />
</bean>

    <bean name="/jose/req1.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
    <property name="viewName" value="jose/req1" />
</bean>

ServiceMovility.java

package es.mov.jose.agenda;

public interface ServiceMovility {
        public String callTest();
}

ServiceMovilityImpl.java

package es.mov.jose.agenda;

import es.mov.jose.agenda.ServiceMovility;

public class ServiceMovilityImpl implements ServiceMovility {

    private ServiceTest serviceTest;    
    public String callTest() {
        return serviceTest.getValue();
    }

}  

服务测试.java

package es.mov.jose.agenda;

public interface ServiceTest {
        public String getValue();
}    

ServiceTestImpl.java

package es.mov.jose.agenda;

import es.mov.jose.agenda.ServiceTest;

public class ServiceTestImpl implements ServiceTest {

    public String getValue() {
        return "OK";
    }

}

最后是我的 req1.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="es.mov.jose.agenda.ServiceMovility"%>
<%@ page import="es.mov.jose.agenda.ServiceMovilityImpl"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="sp-forms" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">    
</head>
<body>
<jsp:useBean id="serviceMovility" scope="request"     class="es.mov.jose.agenda.ServiceMovilityImpl" />
<div class="main" >
    <%
    String result = serviceMovility.callTest();
    out.println(result);
    %>
</div>
</body>
</html>

它完美地调用 serviceMovility 但在 callTest 方法中调用 serviceTest 失败,它说 java.lang.NullPointerException ¿我应该以这种方式在 jsp 文件中包含 bean serviceMovility 吗?¿ 为什么 spring 不注入在 serviceMovility 上初始化的 serviceTest?

我花了 2 天时间,但我的所有解决方案都失败了。我不知道该怎么办。

提前致谢。

4

2 回答 2

0

在定义 bean 时使用实现而不是接口:

<bean id="serviceTest" class="es.mov.jose.agenda.ServiceTestImpl" />

<bean id="serviceMovility" class="es.mov.jose.agenda.ServiceMovilityImpl">
  <property name="serviceTest" ref="serviceTest" />
</bean>

<bean name="/jose/req1.do"
    class="es.mov.jose.agenda.Req1Controller">
  <property name="viewName" value="jose/req1" />
  <property name="serviceMovility" ref="serviceMovility" />
</bean>

另外,在您的 *Impl 类中定义注入依赖项的设置器,即ServiceMovilityImpl

public void setServiceTest(ServiceTest serviceTest) {
  this.serviceTest = serviceTest;
}

编辑

你做错了......不要将bean注入JSP(通过jsp:useBean),而是直接注入Controller。定义自己的控制器:

package es.mov.jose.agenda;

final class Req1Controller extends ParameterizableViewController {

  private ServiceMovility serviceMovility;

  @Override
  protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    ModelAndView mav = new ModelAndView(getViewName());
    mav.addObject("callTest", serviceMovility.callTest());
    return mav;
  }

  public void setServiceMovility(ServiceMovility serviceMovility) {
    this.serviceMovility = serviceMovility;
  }
}

ApplicationContext.xml像我上面所做的那样编辑你的(使用Req1Controllerinstead ParameterizableViewController),在 JSP 中你将有${callTest}变量:

<body>
  <div class="main" >
    <c:out value="${callTest}" />
  </div>
</body>
于 2012-09-12T12:16:08.313 回答
0

一些事情:

您不能创建接口实例:

<bean id="serviceMovility" class="es.mov.jose.agenda.ServiceMovility">
it shout be
<bean id="serviceMovility" class="es.mov.jose.agenda.ServiceMovilityImpl">

ServiceMovilityImpl 必须为字段定义 set 方法。为了使这项工作:

<property name="serviceTest" ref="serviceTest" />

做这个:

package es.mov.jose.agenda;

import es.mov.jose.agenda.ServiceMovility;

public class ServiceMovilityImpl implements ServiceMovility {

    private ServiceTest serviceTest;    

    public void setServiceTest(ServiceTest serviceTest){
        this.serviceTest = serviceTest;
    }

    public String callTest() {
        return serviceTest.getValue();
    }

}  
于 2012-09-12T12:16:53.670 回答