spring BeanCreationException关于映射的混淆 [英] spring BeanCreationException confusion about mapping
问题描述
尝试集成hibernate和spring,我遇到了这个错误
trying to integrate hibernate and spring ,I ran into this error
SEVERE:上下文初始化失败
org。 springframework.beans.factory.BeanCreationException
:创建名称为
'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
':bean的初始化失败;嵌套异常是java.lang.IllegalStateException
:无法映射处理程序'org.me.spring.hib.school.web.SchoolController#0
'到URL路径[/ allschools
]:已经存在[classorg.me.spring.hib.school类型的处理程序。 web.SchoolController
]已映射。
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 isjava.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.
我的控制器看起来像
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实现应用程序上下文文件
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>
我无法弄清楚为什么 SchoolController#0
被映射到网址。
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" />
和
<bean class="org.me.spring.hib.school.web.SchoolController" >
第一行将自动发现并注册 SchoolController
为你服务。通过明确声明另一个,你得到一个副本,url-mapping将发生冲突。
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.
你需要删除< context: component-scan>
,或删除控制器和DAO的显式bean定义。如果您执行后者,则需要使用自动装配来注入其依赖项。
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.
这篇关于spring BeanCreationException关于映射的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!