SSH项目整合——XML文档流

前端之家收集整理的这篇文章主要介绍了SSH项目整合——XML文档流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

编程中总会遇到头疼的问题,各种莫名奇妙的错误,这个错误调试了一下午,终于在老师的帮助下解决了。

错误信息:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'userDAo' while setting bean property 'userDAO'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAo' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor': Cannot resolve reference to bean 'txAdvice' while setting bean property 'advice';

这个错误竟然是因为ApplicationContext.xml文件中bean标签的顺序问题,将自己的bean定义放在了sessionFactory的bean的上面,而XML文档流是由上向下寻找节点的,hibernate是先建立sessionFactory,再由sessionFactory实例化其他bean,所以自己的bean定义要放在sessionFactory定义的下面,才能正确实例化。

例:
Appli
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans;
classpath:org/springframework/beans/factory/xml/spring-beans.xsd
http://www.springframework.org/schema/aop;
classpath:org/springframework/aop/config/spring-aop.xsd
http://www.springframework.org/schema/tx
classpath:org/springframework/transaction/config/spring-tx.xsd">

<!-- 定义数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 指定连接数据库的JDBC驱动 -->
<property name="driverClass" value="com.MysqL.jdbc.Driver">
</property>
<!-- 连接数据库所用的URL -->
<property name="jdbcUrl" value="jdbc:MysqL://localhost:3306/user?useUnicode=true&amp;characterEncoding=utf-8">
</property>
<!-- 连接数据库用户名 -->
<property name="user" value="root">
</property>
<!-- 连接数据库的密码 -->
<property name="password" value="root">
</property>
<!-- 设置数据库连接池的最大连接数 -->
<property name="maxPoolSize" value="20">
</property>
<!-- 设置数据库连接池的最小连接数 -->
<property name="minPoolSize" value="2">
</property>
<!-- 设置数据库连接池的初始化连接数 -->
<property name="initialPoolSize" value="2">
</property>
<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
<property name="maxIdleTime" value="20">
</property>
</bean>

<!-- 定义sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionfactorybean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 配置hibernate实体映射关系 -->
<property name="annotatedClasses">
<list>
<value>com.freedom.bean.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MysqLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!--<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_structured_entries">false</prop>
<prop key="hibernate.jdbc.use_scrollable_resultset">true</prop>-->
<prop key="hibernate.cache.use_query_cache">false</prop>

</props>
</property>
<!--hibernate search配置-->
<!--
<property name="eventListeners">
<map>
<entry key="post-update">
<bean class="org.hibernate.search.event.FullTextIndexEventListener"/>
</entry>
<entry key="post-insert">
<bean class="org.hibernate.search.event.FullTextIndexEventListener"/>
</entry>
<entry key="post-delete">
<bean class="org.hibernate.search.event.FullTextIndexEventListener"/>
</entry>
</map>
</property>
-->
</bean>

<!-- 定义数据源 end -->

<!-- 定义事务 -->

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- 定义事务 end -->

<!--bean的定义要放在SessionFactory的下面 -->
<bean id="loginBean" class="com.freedom.action.LoginAction">
<property name="userDAO" ref="userDAo"></property>
</bean>

<bean id="userDAo" class="com.freedom.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 配置spring事务特性,配置add,delete,update开始的方法,事务传播特性为required -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="required"/>
<tx:method name="delete*" propagation="required"/>
<tx:method name="update*" propagation="required"/>
<!-- <tx:method name="*" propagation="required" /> -->
</tx:attributes>
</tx:advice>

<!-- 配置哪些类的方法行进事务管理,当前com.model包中的子包,类中所有方法需要,还需要参考tx:advice的设置 -->
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.freedom.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>

猜你在找的XML相关文章