分类目录归档:Eclipse

Eclipse-Extjs插件之JSEclipse

使用 JSEclipse,JavaScript 程序员现在有了自己的 Eclipse 插件,该插件将提供许多重要功能来辅助开发 JavaScript 应用程序。像 Eclipse 多年来为 Java™ 语言和其他语言提供了易用性一样,JSEclipse 为 JavaScript 开发人员提供了同样的优点。

获得 JSEclipse

如果尚未安装 Eclipse IDE,请立即安装(请参阅 系统要求 )。本教程使用的是 Eclipse V3.3.1.1,因此如果遇到插件安装问题,则可以尝试安装该版本。

下一步是获得 JSEclipse 插件。完成该操作的最简单方法是使用 Eclipse 的内置更新系统:

  1. 安装 Eclipse。
  2. 启动 Eclipse。
  3. 如果有必要,请选择 “Go to Workbench”。
  4. 从 Workbench 菜单中,选择 Help > Software Updates > Find and Install
  5. 选择 Search for new features to install
  6. 单击 Next 继续创建一个新更新站点。

创建新更新站点

新功能和插件托管在特定的更新站点中。Eclipse 安装中预先配置了这其中的一些站点,但是需要为 JSEclipse 添加一个新更新站点:

  1. 单击窗口右侧的 New Update Site
  2. 输入名称 JSEclipse(或者记得住的任何其他名称)以及 URLhttp://download.macromedia.com/pub/labs/jseclipse/autoinstall 
  3. 单击 OK 将新站点添加到列表中。

安装 JSEclipse

新站点就绪后,可以使用它安装 JSEclipse:

  1. 确保通过选中新站点旁边的复选框选中要更新的新站点,然后单击 Finish
  2. JSEclipse 是托管在此站点中的惟一一个功能,因此它应当十分易于查找。选择 JSEclipse 功能并单击 Next
    接受许可证协议并单击 Next
  3. 检验 Eclipse 安装位置并单击 Finish
  4. 出于安全(以及代码完整性)原因,实际安装将查看功能是否已被签名。它尚未被签名,因此请确认是否需要安装这个未签名的功能,如图 5 所示,然后单击 Install

安装完成后,单击 Apply Changes。应当不必重新启动 Eclipse,但是如果 Eclipse 运行不正常,则最好重新启动。

现在 JSEclipse 已经安装,接下来需要进行配置。

配置 JSEclipse

使用 JSEclipse 插件可以让您很好地控制用户体验。要配置 JSEclipse,请选择Window > Preferences > JSEclipse 打开首选项。

JSEclipse 首选项有四种。

General 首选项
这些是首选项,例如弹出自动完成下拉菜单需要等待的时间、是否执行错误报告以及是否突出显示当前变量的其他出现位置。
Syntax coloring
使用 JSEclipse 不但可以完全控制彩色编码内容,例如函数名、变量等,而且还可以完全控制哪个颜色代表哪个条目,以便可以更轻松地阅读,或者匹配现有颜色模式,比方说,来自另一个编辑器。
Templates
正如您将在 使用代码模板轻松编程:多维数组 部分中看到的那样,JSEclipse 包括常用类型的代码模板,例如 for 循环、函数等。使用此模板可以激活或者取消激活现有模板,并且可以根据需要导入新模板。
Typing
使用 IDE 的优点之一是可以自动化完成诸如添加结束括号之类的任务。如果您觉得使用该功能会让自己变得懒惰,则可以在这里的首选项中关闭此功能。

接下来,需要设置 Eclipse 使用该插件。

将 JSEclipse 设为 JavaScript 编辑器

需要告诉 Eclipse 它有一个 JavaScript 文件的专用编辑器,这样它将不使用通用文本编辑器。选择 Window > Preferences > General > Editors > File Associations。高亮选中 *.js 文件扩展名并确保 JSEclipse 是相关编辑器。

如果没有 *.js 扩展名,请单击顶部的 Add 按钮,然后将该扩展名添加到对话框中。如果 *.js 扩展名没有用 JSEclipse 作为其默认编辑器,请单击底部的 Add 按钮。选择 Internal Editors > JSEclipse HTML Editor 并单击 Add 将其设为默认值。

来自JustJava:http://www.blogjava.net/zJun/archive/2008/01/29/178386.html

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/


Spring Data & MongoDB

In this article we are going to cover the following topics:

  1. MongoDB Installation on Windows Machine.
  2. MongoDB Java Example.
  3. Spring Data & MongoDB.

  MongoDB Installation on Windows Machine:

     Download MongoDB from official MongoDB website.

     Unzip MongoDB files to your prefer location, for example : “D:mongodb-win32-x86_64-1.8.3“.

     MongoDB won’t create data directory automatically. You need to create a data directory for MongoDB, for example : “D:mongodb-data“.

    mongod.exe is the database server daemon, run it and point it to the data directory, via “–dbpath” argument.

D:mongodb-win32-x86_64-1.8.3bin>mongod --dbpath "D:mymongodb-data" 

  In the output, you will notice that MongoDB started a web admin interface listening on port 28017.

   To access it, just type http://your-ipaddress:28017.

   In order to connect to your MongoDB server type D:mongodb-win32-x86_64-1.8.3bin>mongo

   If you are able to view the MongoDB web admin interface and also connected to the database server via mongo.exe, then your MongoDB is installed on the Windows successfully.

 

  MongoDB Java Example:

    A simple Java MongoDB example.

    Technologies and tools used:

  1. MongoDB 1.8.3
  2. MongoDB-Java-Driver 2.6.5
  3. JDK 1.6
package edu.seua.mongo;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * Java MongoDB Example
 * 
 */
public class MongoApp {

    public static void main(String[] args) {

	try {
	    // connect to mongoDB
	    Mongo mongo = new Mongo("localhost", 27017);

	    // if database doesn't exists, mongoDB will create it
	    DB db = mongo.getDB("mydb");

	    // Get collection from MongoDB, database named "mydb"
	    // if collection doesn't exists, mongoDB will create it
	    DBCollection collection = db.getCollection("myCollection");

	    // create a document to store attributes
	    BasicDBObject document = new BasicDBObject();
	    document.put("id", 100);
	    document.put("name", "simple name");
	    document.put("message", "simple message");
	    

	    // save it into collection named "myCollection"
	    collection.insert(document);

	    // search query
	    BasicDBObject searchQuery = new BasicDBObject();
	    searchQuery.put("id", 100);

	    DBCursor cursor = collection.find(searchQuery);

	    // loop over the cursor and display the result
	    while (cursor.hasNext()) {
		System.out.println(cursor.next());
	    }

	} catch (UnknownHostException e) {
	    e.printStackTrace();
	} catch (MongoException e) {
	    e.printStackTrace();
	}
    }
}

Output

{ 
   "_id" : { "$oid" : "4e5936ca602fbf0c50e0c9d0"} , 
   "id" : 100 , "name" : "simple name" , "message" : "simple message"
}

Now it’s time for Spring Data & MongoDB:

    Technologies and jars used:

  1. MongoDB 1.8.3
  2. JDK 1.6
  3. mongo-2.6.5.jar
  4. org.springframework.context-3.0.5.RELEASE.jar
  5. org.springframework.core-3.0.5.RELEASE.jar
  6. org.springframework.beans-3.0.5.RELEASE.jar
  7. commons-logging-1.1.1.jar
  8. org.springframework.asm-3.0.5.RELEASE.jar
  9. spring-data-mongodb-1.0.0.M2.jar
  10. slf4j-api-1.6.1.jar
  11. slf4j-jdk14-1.6.1.jar
  12. spring-dao-2.0.8.jar
  13. spring-expression-3.1.0.M2.jar
  14. spring-data-commons-core-1.1.0.M1.jar
  15. spring-tx-3.1.0.M1.jar
  16. spring-data-document-core-1.0.0.M2.jar



Spring XML configuration file (mongo-config.xml):

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xsi:schemaLocation="http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/data/mongo
        http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
	<mongo:mongo host="localhost" port="27017" />
 
	<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate">
		<constructor-arg ref="mongo" />
		<constructor-arg name="databaseName" value="myNewdb" />
		<constructor-arg name="defaultCollectionName" value="myNewCollection" />
	</bean>
 
	<!-- To translate any MongoExceptions thrown in @Repository annotated classes -->
	<context:annotation-config />
 
</beans>

  Let’s create Employee DTO

package edu.seua.mongo;

public class Employee {

    private String id;
    private String firstname;
    private String lastname;
    private int age;

    public Employee() {
    }

    public Employee(String id, String firstname, String lastname, int age) {
	super();
	this.id = id;
	this.firstname = firstname;
	this.lastname = lastname;
	this.age = age;
    }

    public String getId() {
	return id;
    }

    public void setId(String id) {
	this.id = id;
    }

    public String getFirstname() {
	return firstname;
    }

    public void setFirstname(String firstname) {
	this.firstname = firstname;
    }

    public String getLastname() {
	return lastname;
    }

    public void setLastname(String lastname) {
	this.lastname = lastname;
    }

    public int getAge() {
	return age;
    }

    public void setAge(int age) {
	this.age = age;
    }

    @Override
    public String toString() {
	return "Employee [age=" + age + ", firstname=" + firstname + ", id="
		+ id + ", lastname=" + lastname + "]";
    }

}

Full Example   

    Full example shows you how to use Spring data to perform CRUD operations in mongoD.

    The employee object is saved into a collection named employees.

package edu.seua.mongo;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
import org.springframework.data.document.mongodb.query.Criteria;
import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update;

/**
 * Java Spring Data & MongoDB Example
 * 
 */
public class SpringDataMongoApp {

    public static void main(String[] args) {

	ApplicationContext ctx = new ClassPathXmlApplicationContext("edu/seua/mongo/mongo-config.xml");

	MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");

	Employee employee = new Employee("100", "firstName", "lastName", 23);

	// save
	mongoOperation.save("employees", employee);

	// find
	Employee savedEmployee = mongoOperation.findOne("employees", new Query(Criteria.where(
		"id").is("100")), Employee.class);

	System.out.println("Saved Employee: " + savedEmployee);

	// update
	mongoOperation.updateFirst("employees", new Query(Criteria.where(
		"firstname").is("firstName")), Update.update("lastname",
		"new lastName"));

	// find
	Employee updatedEmployee = mongoOperation.findOne("employees", new Query(
		Criteria.where("id").is("100")), Employee.class);

	System.out.println("Updated Employee: " + updatedEmployee);

	// delete
	mongoOperation.remove("employees", new Query(Criteria.where("id").is(
		"100")), Employee.class);

	// List
	List<Employee> listEmployee = mongoOperation.getCollection("employees", Employee.class);
	System.out.println("size of employees = " + listEmployee.size());

    }
}

  Output

Saved Employee: Employee [age=23, firstname=firstName, id=100, lastname=lastName]
Updated Employee: Employee [age=23, firstname=firstName, id=100, lastname=new lastName]
size of employees = 0
Article Type: 
Opinion/Editorial

Eclipse中安装配置Maven及其插件m2e(Indigo)的安装配置

系统:Windows7

使用软件: Maven3.0.3 + Eclipse Indigo JEE

 

安装Maven

1. 下载解压缩Maven3.0.3,放在某一个目录下

我的目录为:D:studygreensoftapache-maven-3.0.3

 

2. 确认JDK环境已经配置好,JAVA_HOME环境变量配置好。

 

3. 配置环境变量M2_HOME为Maven的方目录

我的配置为:M2_HOME=D:studygreensoftapache-maven-3.0.3

 

4. 增加Path配置到Maven的bin目录:如…;%M2_HOME%bin;

 

打开Dos,输入: mvn -v 结果显示正常表示Maven安装完成

 

配置Maven

配置Maven包括两点:

一点是Maven的本地仓库目录设置,默认仓库目录是${user}/.m2/repository,这里有很多不便之处,如该目录在C盘,我有时喜欢还原一下系统,忘记备份就不好了; 同时这个仓库只对当前用户有效,其它用户又会在自己的用户目录下建库,那就浪费了,这点对个人PC倒是没什么。总之这个目录最好改在一个安全公用的目录里。

 

第二点,就是Maven的全局配置文件M2_HOME/conf/settings.xml,需要修改一下,以便更好的支持一些功能。

 

这两点的修改都只需要修改settings.xml文件就行了:

 

 

Xml代码

  <!-- localRepository
   设置本地库的路径:我直接修改在Maven主目录下的repository目录中
  -->
  <localRepository>${env.M2_HOME}/repository</localRepository>

  <proxies>
    <!-- proxy
    设置网络连接的代理,现在的公司一般都会通过代理上网
     -->
    <proxy>
      <id>我的公司</id>
      <active>true</active>
      <protocol>http</protocol>
      <username></username>
      <password></password>
      <host>10.1.2.188</host>
      <port>80</port>
      <nonProxyHosts>local.*</nonProxyHosts>
    </proxy>
  </proxies>

  <pluginGroups>
    <!-- pluginGroup
     在调试maven的web程序时可能会用的jetty插件,以后就可以直接使用mvn jetty:run 等命令了
    -->
    <pluginGroup>org.mortbay.jetty</pluginGroup>
  </pluginGroups>

 

安装Maven的Eclipse插件m2e

 

1. 登陆Apache Maven的官方网站,找到Eclipse IDE的插件安装地址,如2011-10-13可用地址为:http://download.eclipse.org/technology/m2e/releases

 

2. 使用Eclipse的在线插件安装安装该插件

 

打开eclipse,选择new–>Project…可以看到多了一个Maven组,下有Maven Project等,说明插件安装成功。

 

配置Maven插件m2eclipse

 

1. 配置Maven的版本。在安装插件后,eclipse默认使用的是插件内嵌的maven,版本和使用的配置文件与我们在Dos中使用的是不一致的,这样就会出现很多问题。比如我们已经换了本地仓库,插件还会使用默认的位置作为本地仓库。

打开 preference–>Maven–>Installations

点击Add…,选择我们自己安装的Maven目录,增加条目后,将其打勾(选择上)。

这时我们发现下面的一个条框里的全局配置文件也修改成了我们自己的Maven目录下的settings.xml文件。

 

2. 修改eclipse的默认JRE。在eclipse中运行maven需要JDK的支持, 如 mvn compile命令。eclipse默认使用JRE下的JVM程序,所以需要修改默认的JVM程序

打开 preference–>Java–>Installed JREs

点击Add…, 选择我们自己安装的JDK(不是JRE),增加条目后打勾。

这里如果不修改在运行maven时会报JDK错误。

来自: http://b-l-east.iteye.com/blog/1194540

The Maven Integration requires that Eclipse be running in a JDK

在eclipse中安装了m2eclipse(maven插件)

在安装后,出现下列警告:

The Maven Integration requires that Eclipse be running in a JDK, because a number of Maven core plugins are using jars from the JDK.

Please make sure the -vm option in eclipse.ini is pointing to a JDK and verify that Installed JRE’s are also using JDK installs.

解决:

(1)set your JDK as a default Java environment in eclipse (default installed JRE)

Open eclipse. Click on ‘Window’ -> ‘Preferences’ -> ‘Java’ -> ‘Installed JREs’.

If you can’t find any JDK on the list -> click ‘Add’ -> ‘Standard VM’ -> set all paths and click ‘Finish’.

After that – check this JDK on the list of installed JREs and click ‘OK’.

(2)add path to your SDK in eclipse.ini

open ‘eclipse.ini’ and add there: -vm path_to_javaw_on_your_machine,so in my example it will be (jdk1.6.0_17 on Windows):

-vm
D:\Javajdk1.6.0_13jrebinjavaw

Notice 1: Do not use paths with spaces! Replace all dir names with spaces to their shortcuts. For example: ‘Program Files’ is ‘Progra~1′.
Notice 2: ‘-vm’ is in the first line, path is in the second line

your ‘eclipse.ini’ looks like this:

-startup
plugins/org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar
–launcher.library
plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.0.200.v20090519
-showsplash
org.eclipse.platform
–launcher.XXMaxPermSize
256m
-vm
D:Javajdk1.6.0_13jrebinjavaw
-vmargs
-Xms40m
-Xmx256m

Notice : ‘-vm’ must be  “-vmargs” above

Restart eclipse and everything should be fine.