文章正文

使用spring2.0.5配置事务时出现关于配置文件报错问题的解决办法!

来源:    2007-5-10 17:13:05 网友评论 1 条 字体:[ ] ~我要投稿!
以下是我在其它论坛上发表的问题:

QUOTE:
我采用AbstractDependencyInjectionSpringContextTests进行测试,通过以下代码进行加载.
        protected String[] getConfigLocations() {                return new String[] { "classpath:springapplicationcontext.xml" };        }
以下是我原来的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
        <bean id="dataSource"
                class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName">
                        <value>oracle.jdbc.driver.OracleDriver</value>
                </property>
                <property name="url">
                        <value>jdbc:oracle:thin:@localhost:1521:oracle9</value>
                </property>
                <property name="username">
                        <value>gap</value>
                </property>
                <property name="password">
                        <value>gap</value>
                </property>
        </bean>
        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="configLocation">
                        <value>classpath:hibernate.cfg.xml</value>
                </property>
        </bean>
        <bean id="txManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <bean id="gapdictionaryDao" class="org.gap.dao.GapdictionaryDao">
                <property name="sessionFactory">
                        <ref bean="sessionFactory" />
                </property>
        </bean>
        <bean id="gap" class="org.gap.service.GapImpl">
                <property name="gapdictionaryDao" ref="gapdictionaryDao" />
        </bean>
</beans>

以下是我新的配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
        <bean id="dataSource"
                class="org.apache.commons.dbcp.BasicDataSource">
                <property name="driverClassName">
                        <value>oracle.jdbc.driver.OracleDriver</value>
                </property>
                <property name="url">
                        <value>jdbc:oracle:thin:@localhost:1521:oracle9</value>
                </property>
                <property name="username">
                        <value>gap</value>
                </property>
                <property name="password">
                        <value>gap</value>
                </property>
        </bean>
        <!--                <bean id="dataSource"                class="org.springframework.jndi.JndiObjectFactoryBean">                <property name="jndiName">                <value>gappool</value>                </property>                </bean>        -->
        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="configLocation">
                        <value>classpath:hibernate.cfg.xml</value>
                </property>
        </bean>
        <bean id="txManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <aop:config>
                <aop:pointcut id="gapServiceMethods"
                        expression="execution(* org.gap.service.*Service.*(..))" />
                <aop:advisor advice-ref="txAdvice"
                        pointcut-ref="gapServiceMethods" />
        </aop:config>
        <tx:advice id="txAdvice" transaction-manager="txManager">
                <tx:attributes>
                        <tx:method name="saveOrUpdate*" propagation="REQUIRED" />
                        <tx:method name="delete*" propagation="REQUIRED" />
                        <tx:method name="handle*" propagation="REQUIRED" />
                        <tx:method name="*" propagation="SUPPORTS" read-only="true" />
                </tx:attributes>
        </tx:advice>
        <bean id="gapdictionaryDao" class="org.gap.dao.GapdictionaryDao">
                <property name="sessionFactory">
                        <ref bean="sessionFactory" />
                </property>
        </bean>
        <bean id="gap" class="org.gap.service.GapImpl">
                <property name="gapdictionaryDao" ref="gapdictionaryDao" />
        </bean>
</beans>


用原来的配置文件,可以顺利通过测试,改用新的后,就报以下错:
org.springframework.beans.factory.BeanDefinitionStoreException: Line 14 in XML document from class path resource [springapplicationcontext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.  

在matrix上问了,没人理.在CSDN上问了,也没人理.在javaeye上问了,还扣掉了我10分,说这是新手才问的问题。可能在他们看来,所有和配置有关的问题都是只有新手才问的。不管它。

在求人不能后,终于在google上找到了一篇文章,是spring的官方论坛上的一个高手给一个新手的解答。
有兴趣的可以看看,
http://forum.springframework.org/showthread.php?t=37877
主要原因就是在classpath里还有以前spring老版本的jar包,而采用以上新配置文件的写法,则只能用于spring2.x的版本。这种新的写法对于有大量方法和类需要配置事务的系统可以节省很多时间。去掉以前版本的jar后就可以了。

真是求人不如求已。

一些网站真是店大欺客啊。


上一篇:spring+hibernate删除单条记录的几种方法.
下一篇:中文参考手册在线阅读
用户名:新注册) 密码: 匿名评论 [所有评论]
评论内容:不能超过250字,请自觉遵守互联网相关政策法规。
本栏搜索

  • Google Chinesedocument.com
 网站首页 -  网站地图 -  技术论坛 -  网站投稿 -  广告服务 -  手机游戏
©2007 分享文档 Chinesedocument.com [京ICP备06000384号]