Spring & MongoDB初试

最近在学习Spring和MongoDB的结合,在Spring官网下载了Spring Data MongoDB hello world example 例子,配置过程种出现各种错误。和大家分享下,有兴趣的可以一起研究下:

错误一:
Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.springframework.context.support.AbstractApplicationContext.<init>

(AbstractApplicationContext.java:160)
at org.springframework.context.support.AbstractApplicationContext.<init>

(AbstractApplicationContext.java:213)
at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:101)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>

(AnnotationConfigApplicationContext.java:63)
at com.mkyong.core.App.main(App.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 5 more
这是因为缺少包commons-logging-1.1.1.jar把包加入项目问题就解决了。

错误二:
如果你运行的是//For Annotation
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
出现如下错误:
Exception in thread “main” java.lang.IllegalStateException: Cannot load configuration class: 

com.mkyong.config.SpringMongoConfig
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses

(ConfigurationClassPostProcessor.java:246)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanFactory

(ConfigurationClassPostProcessor.java:160)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors

(AbstractApplicationContext.java:663)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors

(AbstractApplicationContext.java:602)
at org.springframework.context.support.AbstractApplicationContext.refresh

(AbstractApplicationContext.java:407)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>

(AnnotationConfigApplicationContext.java:65)
at com.mkyong.core.App.main(App.java:21)
Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/Type
at net.sf.cglib.core.TypeUtils.parseType(TypeUtils.java:180)
at net.sf.cglib.core.KeyFactory.<clinit>(KeyFactory.java:66)
at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
at org.springframework.context.annotation.ConfigurationClassEnhancer.newEnhancer

(ConfigurationClassEnhancer.java:101)
at org.springframework.context.annotation.ConfigurationClassEnhancer.enhance

(ConfigurationClassEnhancer.java:89)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.enhanceConfigurationClasses

(ConfigurationClassPostProcessor.java:238)
… 6 more
Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 12 more
这是因为项目中SpringMongoConfig文件中多了一个注解@Configuration,把它注释就可以。

错误三:
Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception 

parsing XML document from class path resource [mongo-config.xml]; nested exception is 

java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions

(XmlBeanDefinitionReader.java:412)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions

(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions

(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.GenericXmlApplicationContext.load

(GenericXmlApplicationContext.java:105)
at org.springframework.context.support.GenericXmlApplicationContext.<init>

(GenericXmlApplicationContext.java:68)
at com.mkyong.core.App.main(App.java:24)
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceExceptionTranslator
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.springframework.data.document.mongodb.config.MongoParser.getBeanClass(MongoParser.java:38)
at org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser.parseInternal

(AbstractSingleBeanDefinitionParser.java:66)
at org.springframework.beans.factory.xml.AbstractBeanDefinitionParser.parse

(AbstractBeanDefinitionParser.java:59)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement

(BeanDefinitionParserDelegate.java:1335)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement

(BeanDefinitionParserDelegate.java:1325)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions

(DefaultBeanDefinitionDocumentReader.java:135)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions

(DefaultBeanDefinitionDocumentReader.java:93)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions

(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions

(XmlBeanDefinitionReader.java:390)
… 9 more
Caused by: java.lang.ClassNotFoundException: org.springframework.dao.support.PersistenceExceptionTranslator
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 31 more
缺少包spring-tx-3.0.5.RELEASE.jar

错误四:
Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 84 in XML 

document from class path resource [mongo-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException; 

systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd; lineNumber: 84; columnNumber: 51; 

src-resolve: 无法将名称 ‘repository:repository’ 解析为 ‘type definition’ 组件。
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions

(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions

(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions

(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:149)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions

(AbstractBeanDefinitionReader.java:212)
at org.springframework.context.support.GenericXmlApplicationContext.load

(GenericXmlApplicationContext.java:105)
at org.springframework.context.support.GenericXmlApplicationContext.<init>

(GenericXmlApplicationContext.java:68)
at com.mkyong.core.App.main(App.java:24)
Caused by: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-

1.0.xsd; lineNumber: 84; columnNumber: 51; src-resolve: 无法将名称 ‘repository:repository’ 解析为 ‘type definition’ 

组件。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexContent

(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl

(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseGlobal(Unknown 

Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown 

Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions

(XmlBeanDefinitionReader.java:388)
… 9 more
缺少包spring-data-commons-core-1.0.0.RC1.jar

错误五:
Exception in thread “main” org.springframework.beans.factory.BeanCreationException: Error creating bean with name 

‘mongo’: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: 

Could not instantiate bean class [org.springframework.data.document.mongodb.MongoFactoryBean]: Constructor threw 

exception; nested exception is java.lang.NoClassDefFoundError: 

org/springframework/data/document/UncategorizedDocumentStoreException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean

(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance

(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean

(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean

(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton

(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons

(DefaultListableBeanFactory.java:563)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization

(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh

(AbstractApplicationContext.java:425)
at org.springframework.context.support.GenericXmlApplicationContext.<init>

(GenericXmlApplicationContext.java:69)
at com.mkyong.core.App.main(App.java:24)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class 

[org.springframework.data.document.mongodb.MongoFactoryBean]: Constructor threw exception; nested exception is 

java.lang.NoClassDefFoundError: org/springframework/data/document/UncategorizedDocumentStoreException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate

(SimpleInstantiationStrategy.java:74)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean

(AbstractAutowireCapableBeanFactory.java:958)
… 12 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/document/UncategorizedDocumentStoreException
at org.springframework.data.document.mongodb.MongoFactoryBean.<init>(MongoFactoryBean.java:54)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126)
… 14 more
Caused by: java.lang.ClassNotFoundException: org.springframework.data.document.UncategorizedDocumentStoreException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 20 more
缺少包spring-data-document-core-1.0.0.M2.jar

错误六:
Exception in thread “main” java.lang.NoClassDefFoundError: org/springframework/expression/PropertyAccessor
at org.springframework.context.support.AbstractApplicationContext.prepareBeanFactory

(AbstractApplicationContext.java:483)
at org.springframework.context.support.AbstractApplicationContext.refresh

(AbstractApplicationContext.java:400)
at org.springframework.context.support.GenericXmlApplicationContext.<init>

(GenericXmlApplicationContext.java:69)
at com.mkyong.core.App.main(App.java:24)
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.PropertyAccessor
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 4 more
缺少包org.springframework.expression-3.0.5.RELEASE.jar

错误七:
Exception in thread “main” java.lang.NoClassDefFoundError: org/springframework/asm/ClassVisitor
at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:103)
at org.springframework.context.support.GenericXmlApplicationContext.<init>

(GenericXmlApplicationContext.java:67)
at com.mkyong.core.App.main(App.java:24)
Caused by: java.lang.ClassNotFoundException: org.springframework.asm.ClassVisitor
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
… 3 more
缺少包org.springframework.asm-3.0.5.RELEASE.jar

最后介绍几个下载包的地方:


 http://www.findjar.com/index.x

Maven-Focused Java Class and Archive Search Enginehttp://www.jarvana.com/jarvana/

Programming tutorials and source code examples http://www.java2s.com/

http://www.jar114.com/


发表评论

电子邮件地址不会被公开。 必填项已用*标注