我正在以下网站上尝试示例应用程序:
JSF 2,PrimeFaces 3,Spring 3和Hibernate 4集成项目
但我发现在运行项目时,我得到:
严重:异常发送上下文初始化事件监听器类org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException的实例:错误创建名为"UserService"在ServletContext的资源定义[/ WEB-INF /的applicationContext豆.xml]:设置bean属性'userDAO'时无法解析bean'UserDAO'的引用; 嵌套异常是org.springframework.beans.factory.BeanCreationException:错误创建具有名称豆"的UserDAO"在ServletContext的资源定义[/WEB-INF/applicationContext.xml的]
但是,在applicationContext.xml文件中,相关代码如下:
<!-- Beans Declaration -->
<bean id="User" class="com.otv.model.User"/>
<!-- User Service Declaration -->
<bean id="UserService" class="com.otv.user.service.UserService">
<property name="userDAO" ref="UserDAO" />
</bean>
<!-- User DAO Declaration -->
<bean id="UserDAO" class="com.otv.user.dao.UserDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>com.otv.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
Run Code Online (Sandbox Code Playgroud)
这些类确实存在于相关的包中,并且可以在下面看到各种配置文件的位置.

我在教程和我的实现之间看到的唯一区别是我使用的是NetBeans 7.2而不是Eclipse.
谁有任何想法为什么这是?
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO';
Run Code Online (Sandbox Code Playgroud)
这告诉您无法创建 UserService,因为它缺少属性定义
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]
Run Code Online (Sandbox Code Playgroud)
这告诉您找不到 UserDAO 的定义。
您缺少 UserDao 定义,ref 只是意味着它应该是该类型,它仍然需要 bean 定义。
基本上,每当你使用“ref”时,你就是在告诉 spring 创建该类型的属性。该类型需要在其自己的 bean 定义中定义。
因此,如果 UserDao 使用再次由“ref”定义的其他属性,则该属性也需要其自己的 bean 定义。
您必须将类和 spring 定义视为两个完全独立的实体。这些类可能已经存在并放置在它们应该放置的位置,但是 spring 需要它的 bean 定义才能调用它们。它不知道 UserDao 或 SessionFactory 是什么,除非您明确告诉它您想要调用哪个包/类。
| 归档时间: |
|
| 查看次数: |
39952 次 |
| 最近记录: |