java spring BeanCreationException 关于映射的混淆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4802293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring BeanCreationException confusion about mapping
提问by markjason72
trying to integrate hibernate and spring ,I ran into this error
试图集成休眠和弹簧,我遇到了这个错误
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.
严重:上下文初始化失败
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
映射类型为 [类] 的处理程序。
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
我已经在应用上下文文件中注入了 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>
I cannot make out why SchoolController#0
is being mapped to the url.
我无法弄清楚为什么 SchoolController#0
被映射到 url。
回答by skaffman
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.
第一行将自动发现并SchoolController
为您注册。通过显式声明另一个,您会得到一个副本,并且 url 映射将发生冲突。
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.
您需要删除<context:component-scan>
,或者删除控制器和 DAO 的显式 bean 定义。如果你做后者,那么你需要使用自动装配来注入它们的依赖项。
回答by Sanjeevan
I faced similar kind of problem. I have defined the controller class with "@Controller" and also in the Spring-config.xml file as a bean and have injected the dependencies.
我遇到了类似的问题。我已经用“@Controller”定义了控制器类,也在 Spring-config.xml 文件中定义了一个 bean 并注入了依赖项。
This was causing the problem. @Controller is defining the bean and again, the bean defined in the xml file is redefining the dependencies. I tried autowiring the dependency and removed it as a bean from the xml file. Then it worked.
这导致了问题。@Controller 正在定义 bean,同样,xml 文件中定义的 bean 正在重新定义依赖项。我尝试自动装配依赖项并将其作为 bean 从 xml 文件中删除。然后它起作用了。
回答by Jaikrat
Or you can mention <mvc:annotation-driven/>
in your XML and remove the
或者您可以<mvc:annotation-driven/>
在 XML 中提及并删除
<context:component-scan base-package="org.me.spring.hib.school.web" />
part.
<context:component-scan base-package="org.me.spring.hib.school.web" />
部分。
If you are comfortable with XML notations.
如果您对 XML 表示法感到满意。