java 在 Spring+Hibernate 中调试“自动装配依赖项的注入失败”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4978455/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:55:01  来源:igfitidea点击:

Debugging "Injection of autowired dependencies failed" Error in Spring+Hibernate

javahibernatespring

提问by naiquevin

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. But when i packaged it into war and deployed on tomcat it throws a org.springframework.beans.factory.BeanCreationException with following messages :

我有一个 ContactController,其中 ContactService 类型的属性是 Autowired。但是当我将它打包成 war 并部署在 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. But still it shows the same error.

因此,我从 ContactController 类中注释掉了该属性及其所有引用,并重新部署了它。但它仍然显示相同的错误。

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

谢谢

回答by skaffman

I suspect this be a clash between AnnotationSessionFactoryBeanand 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 cfgfile might be eclipsing the Spring config.

我怀疑这是AnnotationSessionFactoryBean和之间的冲突hibernate.cfg.xml。如果您使用的是前者,则不需要后者。如果您同时使用两者,您将不得不复制一些设置,并且该cfg文件可能会覆盖 Spring 配置。

Whatever you have in hibernate.cfg.xmlyou should be able to move into the bean definition for the AnnotationSessionFactoryBean, and that should resolve your error.

无论您拥有什么,都hibernate.cfg.xml应该能够进入 的 bean 定义AnnotationSessionFactoryBean,这应该可以解决您的错误。

回答by axtavt

I guess you try to configure Hibernate with org.springframework.orm.hibernate3.LocalSessionFactoryBean. However, you use annotations in Hibernate mapping, therefore you need to use org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeaninstead.

我猜您尝试使用org.springframework.orm.hibernate3.LocalSessionFactoryBean. 但是,您在 Hibernate 映射中使用了注解,因此您需要org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean改用。