分类目录归档:学习笔记

mac系统安装redis

      在终端进行如下操作,下载redis并解压,进入redis目录文件夹后,使用make编译,然后安装redis:

curl O http://download.redis.io/redis-stable.tar.gz
tar xvzf redisstable.tar.gz
rm redisstable.tar.gz
cd redisstable
make
sudo make install

安装完成后,在终端启动redis服务

redisserver

Office提取图片中的文字

微软在Microsoft Office 2003中的工具组件中有一个“Microsoft Office Document Imaging”的组件包,它可以直接执行光学字符识别(OCR),下面笔者就为大家介绍利用Office 2003新增的OCR功能从图片中提取文字的方法。



第一步 我们需要安装“Microsoft Office Document Imaging”的组件,点“开始→程序”,在 “Microsoft Office 工具” 里点“ Microsoft Office Document Imaging” 即可安装运行(如图1所示)。

图1 安装组件



第二步 打开带有文字的图片或电子书籍等,找到你希望提取的页面,按下键盘上的打印屏幕键(PrintScreen)进行屏幕取图。



第三步 打开Microsoft Office Word 2003 ,将刚才的屏幕截图粘贴进去;点击“文件”菜单中的“打印”,在安装Microsoft Office Document Imaging组件后,系统会自动安装一个名为“Microsoft Office Document Imaging Writer”的打印机。如图2所示,在“打印机”下拉列表框中选择“Microsoft Office Document Imaging Writer”打印机,其他选项无须额外设置,点击“确定”按钮后,设定好文件输出的路径及文件名(缺省使用源文件名),然后很快就可以自动生成一个MDI格式的文档了。

图2 选择打印机



打开刚才保存的MDI类型文件(如图3所示),根据你的需要用鼠标选择文字内容(被选中的内容在红色的框内),然后单击鼠标右键,在弹出的快捷菜单中选择“将文本发送到Word”,即可将图片内容自动转换为一个新的Word文档,然后你就可以在Word文档中随心所欲地进行编辑了。

图3 将文本发送到Word


提示:如果你不想将转换的内容输入到一个新的Word 文档中,而是希望粘贴到一个已经打开的Word 文档,只需在上面的操作中点击右键菜单的“复制”,然后再到指定文档中执行粘贴即可。()

http://www.pconline.com.cn/pcedu/soft/office/word/0407/406575.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

最强的翻墙方法之WallProxy教程详细版

之前有写过goagent教程,但是有些朋友因各种原因无法使用,下面介绍一个同样基于GAE的翻.墙软件WallProxy,希望能有所帮助,进行自由的网上冲浪

教程开始!!!

第一步:申请Google App Engine账号(其实就是申请Gmail账号,两者通用)

网址是http://appengine.google.com

如果已有谷歌账号,那就直接登录吧。
goagent

第二步: 创建Google App Engine的ID

顺利登录后,点击Creat an Application

goagent

接着输入你的手机号码,国家选择Other(Not Listed)

goagent

需要注意的是,手机号码前面要+86 格式如:+86 13888888888。然后等待收取手机短信,收到短信后(一串数字号码)填入表单,点send提交.
几秒后,谷歌会发来短信(免费的),里面有一串数字,填上即可。

goagent教程

点击send后,Google App Engine账号即被激活,然后就可以创建新的应用程序了。页面会自动转入“My Applications”页面,点击“Create an Application”新建应用,如下图:

goagent教程
一个Gmail账户最多可以创建十个Google App Engine应用。每个应用每天有1GB免费流量,不够的话,多申请几个就可以了(无需再进行手机验证)。下面填写新应用的必要信息,如下图:

goagent教程
在上图中第一处添加一个应用名称,如123abc验证一下是否可用,如果通过那么123abc就是你的Appid(一定要记住这个id!),而123abc.appspoft.com就是你的应用服务器地址了。第二个空格就是给你的应用取个名字,可以随便填,最后点击提交按钮,如果能看到下图这个页面,就说明你成功创建了一个新的应用

goagent教程

第三步: 配置wallproxy

下载地址:WallProxy

1:打开wallproxy-plus文件夹下的server文件夹,直接双击upload.bat (win7若双击无法打开,请用管理员模式)
输入“1”,并按回车,然后输入你刚才申请的App_id,再按回车。如图(点击图片可放大):

2:然后输入3,再按回车,再依次输入自己的gmail邮箱地址和密码(输入密码时将看不见任何符号,但你不用管他,输完密码后按回车就可以了)。最后程序会自动上传,看见下图红框的字样就代表上传成功。并且可关闭此窗口了。

3:双击打开wallproxy-pluslocalWallproxy.exe,屏幕右下角会出现wallproxy图标,右键点击图标,选择【设置】,并修改server_type = ‘simple2′为server_type = ‘ gaeproxy’
appid = ” 为 appid= ‘你的id’ ,最后点击确定,关闭窗口,如下图:

wallproxy教程

wallproxy教程

4:左键点击wallproxy图标,选择127.0.0.1:8086,此时图标变成蓝色状态。至此,配置完毕,翻.墙去吧,哈哈。强烈建议用谷歌的chrome浏览器。

同样,还可以用goagent翻.墙,前去探个究竟

附加步骤:导入证书,主要是方便浏览国外的https的网站

注:以下是可选功能,平时只是娱乐或看看youtube,facebook,twitter的用户不需要做。

  • 为了能浏览国外https网站,我们需要导入根证书:将_ca.crt导入为根证书,以后遇到https链接就不会弹出证书无效的警告了。首次运行程序后可以在localcert_ca.crt中找到(直接提供根证书存在严重安全隐患,所以wallproxy默认不提供,需要首次运行后自动生成),导入后最好将以前添加例外的网站全部删除以避免冲突。
  • Windows下IE等浏览器双击_ca.crt选择导入证书按提示操作即可;
  • Firefox依次操作:首选项->高级->加密->查看证书->证书机构->导入->选择_ca.crt文件->选中第一项->确定;
  • Linux下Chrome按照LinuxCertManagement操作。
  • 每次版本更新时有两个选择:
    将上次导入的根证书删除,再把新的根证书添加进去.
    直接将上一个版本的cert文件夹复制到新版本目录中去.
    使用一段时间后,cert目录下会生成很多文件,如果需要清理,可删除_ca.crt、_ca.key、_serial之外的其他文件

WallProxy官方说明:访问

关于使用及其他小提示:

若使用IE或chrome浏览器,那么翻.墙前直接打开wallproxy客户端,并选择
127.0.0.1:8086或gfwlist即可翻墙,无需对浏览器进行任何设
置。

若使用firefox浏览器的用户可以在选项->高级->网络中修改至“使用系统代
理”,然后打开wallproxy客户端即可。

3种模式简单说明:

  • 127.0.0.1:8086:所有网站进行代理,但对于打开国内普通网站略微影响速度
  • Gfwlist:简单来说,对于被墙的网站使用代理(如twitter),没被墙的不是用代
    理(如baidu)
  • 直接连接:不使用代理

chrome浏览器可使用Proxyswitchy插件, Firefox浏览器可安装autoproxy插
件。

关闭wallproxy客户端时请不要直接点击程序右上方的“叉叉”,请右键点击程序
图标选择“退出”或“显示/隐藏”中的退出,以防出错。另外在关闭前建议将模式调
成“不使用代理”,否则浏览器默认设置过代理。

若未注明,皆为猫理会会长原创,转载请注明出处:http://maolihui.com/wallproxy-detailed-version-of-the-tutorial.html