0

我是 sakai 开发的新手,我选择使用 spring mvc。该工具构建良好,但我得到 No bean named 'org.sakaiproject.logic.SakaiProxy' is defined 错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/index.htm' defined in ServletContext resource [/WEB-INF/springapp-servlet.xml]: Cannot resolve reference to bean 'org.sakaiproject.logic.SakaiProxy' while setting bean property 'sakaiProxy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.sakaiproject.logic.SakaiProxy' is defined

这是我的springapp-servlet.xml

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

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
<!--<property name="prefix" value="/WEB-INF/jsp/" />-->
    <property name="suffix" value=".jsp" />
    <property name="order" value="10" />
   </bean>

<bean name="/index.htm"
    class="org.sakaiproject.tool.HelloWorldController">
    <property name="sakaiProxy" ref="org.sakaiproject.logic.SakaiProxy"/>
</bean>

这是我的controller

package org.sakaiproject.tool;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.Getter;
import lombok.Setter;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import org.sakaiproject.logic.SakaiProxy;

public class HelloWorldController implements Controller {

/**
 * Hello World Controller
 * 
 * @author Mike Jennings (mike_jennings@unc.edu)
 * 
 */

private SakaiProxy sakaiProxy = null;

public ModelAndView handleRequest(HttpServletRequest arg0,
        HttpServletResponse arg1) throws Exception {

    Map<String, Object> map = new HashMap<String,Object>();
    map.put("currentSiteId", sakaiProxy.getCurrentSiteId());
    map.put("userDisplayName", sakaiProxy.getCurrentUserDisplayName());

    return new ModelAndView("index", map);
}

}

我不知道为什么会出现这个错误我用谷歌搜索但没有太多帮助:(

4

1 回答 1

1

问题在于

<property name="sakaiProxy" ref="org.sakaiproject.logic.SakaiProxy"/>

您正在使用ref属性,该属性通过其id. 没有调用 bean org.sakaiproject.logic.SakaiProxy,因此出现错误。您可能想要创建一个名为SakaiProxy并引用它的 bean,即

<bean id="SakaiProxy" class="org.sakaiproject.logic.SakaiProxy" />
...
<property name="sakaiProxy><ref bean="SakaiProxy" /></property>

有关更多信息,请参阅

编辑:

sakaiProxy是一个private字段。这可能会导致问题,因为通常 spring 通过publicsetter 注入 bean,即public void setSakaiProxy(SakaiProxy proxy) 除非字段被注释@Autowired(它也可以处理private字段)。有关更多信息,请参阅

于 2013-08-19T13:50:38.967 回答