我采用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分,说这是新手才问的问题。可能在他们看来,所有和配置有关的问题都是只有新手才问的。不管它。
主要原因就是在classpath里还有以前spring老版本的jar包,而采用以上新配置文件的写法,则只能用于spring2.x的版本。这种新的写法对于有大量方法和类需要配置事务的系统可以节省很多时间。去掉以前版本的jar后就可以了。