gpt4 book ai didi

java - spring BeanCreationException 关于映射的困惑

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:41:11 26 4
gpt4 key购买 nike

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

SEVERE: Context initialization failed org.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 [class org.me.spring.hib.school.web.SchoolController] mapped.

我的 Controller 看起来像

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

}

我已经在应用程序上下文文件中注入(inject)了 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。

最佳答案

之所以会这样,是因为你们两者兼而有之

<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> ,或删除 Controller 和 DAO 的显式 bean 定义。如果您选择后者,那么您将需要使用 Autowiring 来注入(inject)它们的依赖项。

关于java - spring BeanCreationException 关于映射的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4802293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com