trying to integrate hibernate and spring ,I ran into this error

问题:

trying to integrate hibernate and spring ,I ran into this error

SEVERE: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'org.me.spring.hib.school.web.SchoolController#0' to URL path [/allschools]: There is already handler of type [classorg.me.spring.hib.school.web.SchoolController] mapped.

My Controller looks like

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

}

I have injected the dao implementation in app context file

    <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>

I cannot make out why SchoolController#0 is being mapped to the url.


答案:


This is happening because you have both

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

and

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

The first line will auto-discover and register a SchoolController for you. By explicitly declaring another one, you get a duplicate, and the url-mapping will clash.

You need to either remove the <context:component-scan>, or remove the explicit bean definitions for the controller and the DAO. If you do the latter, then you'll need to use autowiring to inject their dependencies.



原文地址:http://stackoverflow.com/questions/4802293/spring-beancreationexception-confusion-about-mapping


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值