spring学习(一)


       今天开始学习《Spring 开发指南》,按照上面的配置,运行spring的第一程序,结果发现就出现错误。主要有下面2个错误,呵呵,建议有人修改一下《Spring 开发指南》,以免后人再犯同样的错误,而且找半天还找不到解决的办法。
      其一:错误现象如下:
      
java.lang.NoClassDefFoundError: org / apache / commons / logging / LogFactory
    at org.springframework.util.ClassUtils.
< clinit > (ClassUtils.java: 73 )
    at org.springframework.core.io.DefaultResourceLoader.
< init > (DefaultResourceLoader.java: 52 )
    at org.springframework.context.support.AbstractApplicationContext.
< init > (AbstractApplicationContext.java: 198 )
    at org.springframework.context.support.AbstractRefreshableApplicationContext.
< init >

(AbstractRefreshableApplicationContext.java:
80 )
    at org.springframework.context.support.AbstractXmlApplicationContext.
< init >

(AbstractXmlApplicationContext.java:
58 )
    at org.springframework.context.support.FileSystemXmlApplicationContext.
< init >

(FileSystemXmlApplicationContext.java:
121 )
    at org.springframework.context.support.FileSystemXmlApplicationContext.
< init >

(FileSystemXmlApplicationContext.java:
68 )
    at test.UpperActionTest.test_execte(UpperActionTest.java:
15 )
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39 )
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25 )
    at java.lang.reflect.Method.invoke(Method.java:
597 )
    at junit.framework.TestCase.runTest(TestCase.java:
168 )
    at junit.framework.TestCase.runBare(TestCase.java:
134 )
    at junit.framework.TestResult$
1 .protect(TestResult.java: 110 )
    at junit.framework.TestResult.runProtected(TestResult.java:
128 )
    at junit.framework.TestResult.run(TestResult.java:
113 )
    at junit.framework.TestCase.run(TestCase.java:
124 )
    at junit.framework.TestSuite.runTest(TestSuite.java:
232 )
    at junit.framework.TestSuite.run(TestSuite.java:
227 )
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:
76 )
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
45 )
    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 )
   这个错误,其实不是书上的错误,是因为我们少引入一个jar包:commons-logging.jar。

   其二,错误现象如下:
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line  1  in XML document from file [E:\eclipse - SDK - 3.4 \workspace\spring\bean.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc - elt. 1 : Cannot find the declaration of element  ' beans ' .
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:
369 )
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:
313 )
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:
290 )
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
142 )
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
158 )
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:
184 )
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:
112 )
    at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:
79 )
    at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:
97 )
    at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:
411 )
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:
338 )
    at org.springframework.context.support.FileSystemXmlApplicationContext.
< init > (FileSystemXmlApplicationContext.java: 124 )
    at org.springframework.context.support.FileSystemXmlApplicationContext.
< init > (FileSystemXmlApplicationContext.java: 68 )
    at test.UpperActionTest.test_execte(UpperActionTest.java:
15 )
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39 )
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25 )
    at java.lang.reflect.Method.invoke(Method.java:
597 )
    at junit.framework.TestCase.runTest(TestCase.java:
168 )
    at junit.framework.TestCase.runBare(TestCase.java:
134 )
    at junit.framework.TestResult$
1 .protect(TestResult.java: 110 )
    at junit.framework.TestResult.runProtected(TestResult.java:
128 )
    at junit.framework.TestResult.run(TestResult.java:
113 )
    at junit.framework.TestCase.run(TestCase.java:
124 )
    at junit.framework.TestSuite.runTest(TestSuite.java:
232 )
    at junit.framework.TestSuite.run(TestSuite.java:
227 )
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:
76 )
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
45 )
    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 )
Caused by: org.xml.sax.SAXParseException: cvc
- elt. 1 : Cannot find the declaration of element  ' beans ' .
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:
195 )
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:
131 )
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:
384 )
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:
318 )
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:
1887 )
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:
685 )
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:
400 )
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:
626 )
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:
3084 )
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:
912 )
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:
645 )
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:
140 )
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:
508 )
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:
807 )
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:
737 )
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:
107 )
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:
225 )
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:
283 )
    at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:
78 )
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:
361 )
     
32  more

     这个错误就是书上的错误了。问题是因为bean.xml写的有问题,书上这样
< beans >
    
< description > Spring Quick Start     description >
    
< bean id = " TheAction "   class = " com.neusoft.wang_xj.actionImpl.UpperAction " >
        
< property name = " message " >
            
< value > HeLLo value >
        
property >
    
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 "   
    xmlns:context
= " http://www.springframework.org/schema/context "   
    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   
          http: // www.springframework.org/schema/context   
          http: // www.springframework.org/schema/context/spring-context-2.5.xsd">   
< bean id = " TheAction "   class = " com.neusoft.wang_xj.actionImpl.UpperAction " >    
< property name = " message " >    
< value > HeLLo value >    
property >    
bean >     
beans >

     问题就解决了。

    不知道你按照我说的修改,问题解决了么?

你可能感兴趣的:(Spring,Java,junit,Eclipse,Apache)