[英]Debugging “Injection of autowired dependencies failed” Error in Spring+Hibernate
I have just started with Spring Framework and trying to develop and run a simple a app using Spring + Hibernate + Maven for dependency management. 我刚开始使用Spring Framework,并尝试使用Spring + Hibernate + Maven开发和运行一个简单的应用程序以进行依赖项管理。
I have a ContactController in which a property of type ContactService is Autowired. 我有一个ContactController,其中ContactService类型的属性是自动装配的。 But when i packaged it into war and deployed on tomcat it throws a org.springframework.beans.factory.BeanCreationException with following messages :
但是,当我将其包装到战争中并部署到tomcat上时,它会抛出org.springframework.beans.factory.BeanCreationException并显示以下消息:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]:
Invocation of init method failed; nested exception is org.hibernate.MappingException:
An AnnotationConfiguration instance is required to use <mapping
class="org.kodeplay.contact.form.Contact"/>
So I commented out that property from the ContactController Class along with all its references and re deployed it. 因此,我从ContactController类中注释了该属性及其所有引用,然后重新部署了它。 But still it shows the same error.
但是它仍然显示相同的错误。
This is the only controller in the entire application and an object implementing the ContactService interface is not being used any where else. 这是整个应用程序中的唯一控制器,并且在其他任何地方都没有使用实现ContactService接口的对象。
Whats going on here ? 这里发生了什么 ? Am I missing something ?
我想念什么吗?
Edit : Adding the code for spring-servlet.xml 编辑:为spring-servlet.xml添加代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.kodeplay.contact" />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Internationalization -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- To load database connection details from jdbc.properties file -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<!-- To establish a connection to the database -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!-- Hibernate configuration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>org.kodeplay.contact.form.Contact</value>
</list>
</property>
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
code for hibernate.cfg.xml hibernate.cfg.xml的代码
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="org.kodeplay.contact.form.Contact" />
</session-factory>
</hibernate-configuration>
Thanks 谢谢
I suspect this be a clash between AnnotationSessionFactoryBean
and hibernate.cfg.xml
. 我怀疑这是
AnnotationSessionFactoryBean
和hibernate.cfg.xml
之间的冲突。 If you're using the former, you shouldn't need the latter. 如果使用前者,则不需要后者。 If you use both, you're going to have to duplicate some settings, and the
cfg
file might be eclipsing the Spring config. 如果同时使用两者,则将不得不重复一些设置,并且
cfg
文件可能会使Spring配置黯然失色。
Whatever you have in hibernate.cfg.xml
you should be able to move into the bean definition for the AnnotationSessionFactoryBean
, and that should resolve your error. 无论
hibernate.cfg.xml
有什么内容,您都应该能够进入AnnotationSessionFactoryBean
的bean定义中,这应该可以解决您的错误。
I guess you try to configure Hibernate with org.springframework.orm.hibernate3.LocalSessionFactoryBean
. 我猜您尝试使用
org.springframework.orm.hibernate3.LocalSessionFactoryBean
配置Hibernate。 However, you use annotations in Hibernate mapping, therefore you need to use org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
instead. 但是,您在Hibernate映射中使用批注,因此需要改用
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.