6

试图整合hibernate和spring,我遇到了这个错误

严重:上下文初始化失败 org.springframework.beans.factory.BeanCreationException:创建名为“ org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”的 bean 时出错:bean 初始化失败;嵌套异常是java.lang.IllegalStateException:无法将处理程序“ org.me.spring.hib.school.web.SchoolController#0”映射到 URL 路径 [ /allschools]:已经org.me.spring.hib.school.web.SchoolController映射了 [class] 类型的处理程序。

我的控制器看起来像

package org.me.spring.hib.school.web;

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

import org.me.spring.hib.school.dao.SchoolDAO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SchoolController {
    private SchoolDAO schoolDao;

    public SchoolDAO getSchoolDao() {
        return schoolDao;
    }

    public void setSchoolDao(SchoolDAO schoolDao) {
        this.schoolDao = schoolDao;
    }
        @RequestMapping("/allschools")
    public ModelAndView showAllSchools(HttpServletRequest request,HttpServletResponse response) throws Exception{
        if(this.schoolDao ==null){
            System.out.println("this.schoolDao is null");
        }
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("schoolsList", this.schoolDao.getAllSchools());
        return new ModelAndView("allschools", modelMap);
    }

}

我已经在应用上下文文件中注入了 dao 实现

    <context:component-scan base-package="org.me.spring.hib.school.web" />

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
       <property name="sessionFactory" >
           <ref bean="sessionFactory" />
       </property>    
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="dataSource">
          <ref local="dataSource"/>
       </property>
       <property name="annotatedClasses">
            <list>
                <value>org.me.spring.hib.school.domain.School</value>
                <value>org.me.spring.hib.school.domain.Teacher</value>
                <value>org.me.spring.hib.school.domain.Student</value>
            </list>
       </property>


       <property name="hibernateProperties">
        <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
       </property>

   </bean>

    <bean id="schoolDao" class="org.me.spring.hib.school.dao.SchoolDAOImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate" />
    </bean>


    <bean  class="org.me.spring.hib.school.web.SchoolController" >
            <property name="schoolDao" ref="schoolDao" />
    </bean>

我无法弄清楚为什么 SchoolController#0被映射到 url。

4

3 回答 3

22

发生这种情况是因为您同时拥有

<context:component-scan base-package="org.me.spring.hib.school.web" />

<bean  class="org.me.spring.hib.school.web.SchoolController" >

第一行将自动发现并SchoolController为您注册一个。通过显式声明另一个,您会得到一个副本,并且 url-mapping 将发生冲突。

您需要删除<context:component-scan>或删除控制器和 DAO 的显式 bean 定义。如果您执行后者,那么您将需要使用自动装配来注入它们的依赖项。

于 2011-01-26T12:12:10.547 回答
0

我遇到了类似的问题。我已经用“@Controller”定义了控制器类,并且在 Spring-config.xml 文件中定义了一个 bean 并注入了依赖项。

这是造成问题的原因。@Controller 正在定义 bean,xml 文件中定义的 bean 正在重新定义依赖项。我尝试自动装配依赖项并将其作为 bean 从 xml 文件中删除。然后它起作用了。

于 2015-06-21T05:17:41.870 回答
0

或者您可以在您的 XML 中提及<mvc:annotation-driven/>并删除

<context:component-scan base-package="org.me.spring.hib.school.web" />部分。

如果您对 XML 表示法感到满意。

于 2015-11-26T07:23:27.830 回答