我采用AbstractDependencyInjectionSpringContextTests进行测试,通过以下代码进行加载.java 代码
protected String[] getConfigLocations() {    
    return new String[] { "classpath:springapplicationcontext.xml" };    
}   
以下是我原来的配置文件:xml 代码
<!--sp-->xml version="1.0" encoding="utf-8"?>   
<!--CTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"</sp-->>   
<beans>   
   
 <bean id="dataSource"   
  class="org.apache.commons.dbcp.BasicDataSource">   
  <property name="driverClassName">   
   <value>oracle.jdbc.driver.OracleDrivervalue>   
  property>   
  <property name="url">   
   <value>jdbc:oracle:thin:@localhost:1521:oracle9value>   
  property>   
  <property name="username">   
   <value>gapvalue>   
  property>   
  <property name="password">   
   <value>gapvalue>   
  property>   
 bean>   
   
 <!--   </sp--> 
  <bean id="dataSource"   
  class="org.springframework.jndi.JndiObjectFactoryBean">   
  <property name="jndiName">   
  <value>gappoolvalue>   
  property>   
  bean>   
 -->   
 <bean id="sessionFactory"   
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  <property name="dataSource" ref="dataSource" />   
  <property name="configLocation">   
   <value>classpath:hibernate.cfg.xmlvalue>   
  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 代码
<!--sp-->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.OracleDrivervalue>   
  property>   
  <property name="url">   
   <value>jdbc:oracle:thin:@localhost:1521:oracle9value>   
  property>   
  <property name="username">   
   <value>gapvalue>   
  property>   
  <property name="password">   
   <value>gapvalue>   
  property>   
 bean>   
   
 <!--   </sp--> 
  <bean id="dataSource"   
  class="org.springframework.jndi.JndiObjectFactoryBean">   
  <property name="jndiName">   
  <value>gappoolvalue>   
  property>   
  bean>   
 -->   
 <bean id="sessionFactory"   
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  <property name="dataSource" ref="dataSource" />   
  <property name="configLocation">   
   <value>classpath:hibernate.cfg.xmlvalue>   
  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'.
 at weblogic.apache.xerces.parsers.DOMParser.parse(DOMParser.java:271)
 at weblogic.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:201)
 at weblogic.xml.jaxp.RegistryDocumentBuilder.parse(RegistryDocumentBuilder.java:149)
 at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:76)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:145)
 at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:99)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:102)
 at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:70)
 at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:87)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:262)
 at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:80)
 at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:65)
 at org.springframework.test.AbstractSpringContextTests.loadContextLocations(AbstractSpringContextTests.java:113)
 at org.springframework.test.AbstractDependencyInjectionSpringContextTests.loadContextLocations(AbstractDependencyInjectionSpringContextTests.java:147)
 at org.springframework.test.AbstractSpringContextTests.getContext(AbstractSpringContextTests.java:95)
 at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:114)
 at junit.framework.TestCase.runBare(TestCase.java:125)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at junit.framework.TestSuite.runTest(TestSuite.java:208)
 at junit.framework.TestSuite.run(TestSuite.java:203)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)