繁体   English   中英

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

[英]Debugging “Injection of autowired dependencies failed” Error in Spring+Hibernate

我刚开始使用Spring Framework,并尝试使用Spring + Hibernate + Maven开发和运行一个简单的应用程序以进行依赖项管理。

我有一个ContactController,其中ContactService类型的属性是自动装配的。 但是,当我将其包装到战争中并部署到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"/>

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

这是整个应用程序中的唯一控制器,并且在其他任何地方都没有使用实现ContactService接口的对象。

这里发生了什么 ? 我想念什么吗?

编辑:为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>

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>

谢谢

我怀疑这是AnnotationSessionFactoryBeanhibernate.cfg.xml之间的冲突。 如果使用前者,则不需要后者。 如果同时使用两者,则将不得不重复一些设置,并且cfg文件可能会使Spring配置黯然失色。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM