spring4与mongodb的集成
新项目的辅助系统,需要用到mongo系统,今天再次将其使用环境进行了操作搭建。还是遇到一些问题,毕竟之前使用的场景和现在的不同。版本也不一样了。
本次使用的环境:
mongo:3.4.4版本
OS: redhat7
java操作mongo库
1. 首先,mongo数据库的安装
直接到mongodb官网下载mongodb-linux-x86_64-rhel70-3.4.4.tgz。解压之后,运行mongod即可启动服务。当然,我这里是为了项目开发用,第一步就启用单实例。
看看我们的mongodb的配置:
#数据库数据存放路径
dbpath = /home/tkrobot/mongodb/data
#日志存放路径
logpath = /home/tkrobot/mongodb/logs/mongodb.log
logappend = true
port =
fork = true
#启动认证功能,要想连接到数据库,必须经过用户名密码认证才可以
auth = true
#禁止http访问数据库
nohttpinterface = true
#连接数据库时必须是这个ip
bind_ip = 10.90.7.10
journal = true
#最大连接数
maxConns =
这里,需要在mongod启动前,创建好/home/tkrobot/mongodb/data以及/home/tkrobot/mongodb/logs两个目录。
启动mongod的脚本:
./mongod --config /etc/mongodb.conf
这样,mongod就算启动好了。下面有其启动日志:
--02T11::03.553+ I CONTROL [initandlisten] MongoDB starting : pid= port= dbpath=/home/tkrobot/mongodb/data -bit host=localhost.localdomain
--02T11::03.553+ I CONTROL [initandlisten] db version v3.4.4
--02T11::03.553+ I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
--02T11::03.553+ I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips Feb
--02T11::03.553+ I CONTROL [initandlisten] allocator: tcmalloc
--02T11::03.553+ I CONTROL [initandlisten] modules: none
--02T11::03.553+ I CONTROL [initandlisten] build environment:
--02T11::03.553+ I CONTROL [initandlisten] distmod: rhel70
--02T11::03.553+ I CONTROL [initandlisten] distarch: x86_64
--02T11::03.553+ I CONTROL [initandlisten] target_arch: x86_64
--02T11::03.553+ I CONTROL [initandlisten] options: { config: "/etc/mongodb.conf", net: { bindIp: "10.90.7.10", http: { enabled: false }, maxIncomingConnections: , port: }, security: { authorization: "enabled" }, st
orage: { dbPath: "/home/tkrobot/mongodb/data", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/home/tkrobot/mongodb/logs/mongodb.log" } }
--02T11::03.553+ I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=15438M,session_max=,eviction=(threads_min=,threads_max=),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=jo
urnal,compressor=snappy),file_manager=(close_idle_time=),checkpoint=(wait=,log_size=2GB),statistics_log=(wait=),
--02T11::03.567+ I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
--02T11::03.567+ I CONTROL [initandlisten]
--02T11::03.567+ I CONTROL [initandlisten]
--02T11::03.568+ I CONTROL [initandlisten] ** WARNING: You are running on a NUMA machine.
--02T11::03.568+ I CONTROL [initandlisten] ** We suggest launching mongod like this to avoid performance problems:
--02T11::03.568+ I CONTROL [initandlisten] ** numactl --interleave=all mongod [other options]
--02T11::03.568+ I CONTROL [initandlisten]
--02T11::03.568+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
--02T11::03.568+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--02T11::03.568+ I CONTROL [initandlisten]
--02T11::03.568+ I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
--02T11::03.568+ I CONTROL [initandlisten] ** We suggest setting it to 'never'
--02T11::03.568+ I CONTROL [initandlisten]
--02T11::03.571+ I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/home/tkrobot/mongodb/data/diagnostic.data'
--02T11::03.575+ I INDEX [initandlisten] build index on: admin.system.version properties: { v: , key: { version: }, name: "incompatible_with_version_32", ns: "admin.system.version" }
--02T11::03.575+ I INDEX [initandlisten] building index using bulk method; build may temporarily use up to megabytes of RAM
--02T11::03.576+ I INDEX [initandlisten] build index done. scanned total records. secs
--02T11::03.576+ I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
--02T11::03.576+ I NETWORK [thread1] waiting for connections on port
2. 接下来,我们操作一下,验证数据库部署的情况是否ok。
为了方便使用mongo客户端指令,将解压后的mongo配置到环境变量里面,方便操作。
export MONGODB_PATH=/home/tkrobot/mongodb-linux-x86_64-rhel70-3.4.4
export PATH=$JAVA_HOME/bin:$MONGODB_PATH/bin:$PATH
用mongo登录操作数据库:
[root@localhost bin]# mongo
MongoDB shell version v3.4.4
connecting to: mongodb://10.90.7.10:27017
MongoDB server version: 3.4.
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> show dbs;
--02T11::08.878+ E QUERY [thread1] Error: listDatabases failed:{
"ok" : ,
"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",
"code" : ,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js::
Mongo.prototype.getDBs@src/mongo/shell/mongo.js::
shellHelper.show@src/mongo/shell/utils.js::
shellHelper@src/mongo/shell/utils.js::
@(shellhelp2)::
没有权限,因为我们在mongod.conf里面指定要auth了。最简单的办法,就是将mongod.conf里面的auth注释掉,重新启动mongod,进行权限配置。
在没有auth属性的情况下,启动mongod,然后再次mongo连接上数据库,use指定的数据库,创建一个专属的用户。
use robotkdb
指定数据库robotkdb后,创建一个用户robotassister。
db.createUser({
... user:"robotassister",
... pwd:"xxyyyxxxxx",
... roles:[{role:"dbOwner",db:"robotkdb“}]})
这样,就相当于给指定的数据库robotkdb创建了一个角色为dbOwener的用户robotassister。
最后,将mongodb.conf中的auth重新放开,启用auth,再启动mongod,并进行指定数据库和用户名的方式连接到mongodb:
mongo --host 10.90.7.10 --port -u robotassister -p xxyyyxxxx robotdb
如此,一切都ok,单实例的mongod配置带auth的启动,客户端通过credentials的方式连接到指定的库,准备工作就绪。
3. spring集成mongodb。
安装spring官方的文档介绍,这个过程是比较简单的。集成过程中需要用到的支持mongodb的jar包也不多。主要有:
mongo-java-driver-3.4..jar
spring-data-commons-1.10..RELEASE.jar
spring-data-commons-core-1.4..RELEASE.jar
spring-data-mongodb-1.7..RELEASE.jar
上述的jar是我的集成过程中用到的。
我在自己既有的spring mvc的web项目里面,添加了mongodb的支持。
首先看看mongodb的xml配置文件spring-mongo.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd"
default-lazy-init="default"> <!--credentials的配置形式是:用户名:密码@默认数据库-->
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
<mongo:client-options write-concern="SAFE"
connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}"
max-wait-time="${mongo.maxWaitTime}"
socket-timeout="${mongo.socketTimeout}"/>
</mongo:mongo-client> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
其中的参数配置文件mongo.properties:
mongo.dbname=robotkdb
mongo.host=10.90.7.10
mongo.port=
mongo.username=robotassister
mongo.password=xxyyyxxxxx
#一个线程变为可用的最大阻塞数
mongo.connectionsPerHost=
#线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
mongo.threadsAllowedToBlockForConnectionMultiplier=
#连接超时时间(毫秒)
mongo.connectTimeout=
#最大等待时间
mongo.maxWaitTime=
#自动重连
mongo.autoConnectRetry=true
#scoket保持活动
mongo.socketKeepAlive= true
#scoket超时时间
mongo.socketTimeout=
#读写分离
mongo.slaveOk=true
接下来,看看spring的配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:conf/internal.properties</value>
<value>classpath:conf/jdbc.properties</value>
<value>classpath:conf/redis.properties</value>
<value>classpath:conf/session.properties</value>
<value>classpath:conf/mongo.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configRealm"/>
</bean> <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:conf/ehcache.xml"/>
</bean> <cache:annotation-driven cache-manager="springCacheManager"/> <import resource="spring-servlet.xml"/>
<import resource="spring-cache.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-redis.xml"/>
<import resource="spring-mongo.xml"/>
</beans>
ok,到此,所有的配置工作都完毕了,我们就启动web项目,看看是否能正常启动吧!遗憾,出了下面的错误!!!!!
五月 , :: 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [spring-mongo.xml]
Offending resource: class path resource [conf/applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line in XML document from class path resource [conf/spring-mongo.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:)
at java.util.concurrent.FutureTask.run(FutureTask.java:)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
at java.lang.Thread.run(Thread.java:)
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line in XML document from class path resource [conf/spring-mongo.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:)
... more
Caused by: org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd; lineNumber: 275; columnNumber: 63; src-resolve: 无法将名称 'repository:auditing-attributes' 解析为 'attribute group' 组件。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr(XSDHandler.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaError(XSDHandler.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getGlobalDecl(XSDHandler.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAttributeGroupTraverser.traverseLocal(XSDAttributeGroupTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDAbstractTraverser.traverseAttrsAndAttrGrps(XSDAbstractTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.processComplexContent(XSDComplexTypeTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseComplexTypeDecl(XSDComplexTypeTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDComplexTypeTraverser.traverseLocal(XSDComplexTypeTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseNamedElement(XSDElementTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDElementTraverser.traverseGlobal(XSDElementTraverser.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.traverseSchemas(XSDHandler.java:)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema(XSDHandler.java:)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema(XMLSchemaLoader.java:)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.findSchemaGrammar(XMLSchemaValidator.java:)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:)
... more
这个,还真是折腾了我一段时间,最终找到原因:
上述这个错误,需要在spring-mongo.xml文件中添加下面的两行内容
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd采用基于mongo:mongo-client标签进行安全配置,mongo要求spring-mongo.xsd必须是1.6及以上的版本。我们这里选用的是1.7.
ok,再次运行看看吧。。。。。。结果如何呢。。。。。。。。。
五月 , :: 下午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [conf/spring-mongo.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory$.getObject(AbstractBeanFactory.java:)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:)
at java.util.concurrent.FutureTask.run(FutureTask.java:)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
at java.lang.Thread.run(Thread.java:)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplate]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:)
... more
Caused by: java.lang.NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd
at org.springframework.data.mongodb.core.convert.DefaultDbRefResolver.<init>(DefaultDbRefResolver.java:)
at org.springframework.data.mongodb.core.MongoTemplate.getDefaultMongoConverter(MongoTemplate.java:)
at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:)
at org.springframework.data.mongodb.core.MongoTemplate.<init>(MongoTemplate.java:)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:)
at java.lang.reflect.Constructor.newInstance(Constructor.java:)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:)
... more
Caused by: java.lang.ClassNotFoundException: org.springframework.objenesis.ObjenesisStd
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:)
... more
又出现了错误。。。看来不是很顺利啊,再研究看root cause是什么呢???依据
爆出上述错误,经过查找原因,得知,sping-data-mongo要求spring-core必须是4.x的版本,也就是说spring4才行。
源头是通过查找日志中红色部分的错误信息,得知相关的jar依赖文件,在spring-core 4.x的版本中默认含有,于是,将项目中原来3.2的配置信息,全部更新为4.2的,并相应的将工程的lib中spring的版本更新到4.2.再次启动应用。这次,一切都ok了。
最终能正常运行的applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:conf/internal.properties</value>
<value>classpath:conf/jdbc.properties</value>
<value>classpath:conf/redis.properties</value>
<value>classpath:conf/session.properties</value>
<value>classpath:conf/mongo.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configRealm"/>
</bean> <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcacheManager"/>
</bean> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:conf/ehcache.xml"/>
</bean> <cache:annotation-driven cache-manager="springCacheManager"/> <import resource="spring-servlet.xml"/>
<import resource="spring-cache.xml"/>
<import resource="spring-dao.xml"/>
<import resource="spring-redis.xml"/>
<import resource="spring-mongo.xml"/>
</beans>
最终能正常运行的spring-mongo.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.7.xsd
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd"
default-lazy-init="default"> <!--credentials的配置形式是:用户名:密码@默认数据库-->
<mongo:mongo-client id="mongoClient" host="${mongo.host}" port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
<mongo:client-options write-concern="SAFE"
connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}"
max-wait-time="${mongo.maxWaitTime}"
socket-timeout="${mongo.socketTimeout}"/>
</mongo:mongo-client> <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}" mongo-ref="mongoClient" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
4. 启用一个测试,验证一下spring和mongodb的集成是否成功吧
先构建一个javabean:
/**
* @author "shihuc"
* @date 2017年5月3日
*/
package com.roomdis.center.mongo.model; import java.io.Serializable; import org.ietf.jgss.Oid;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field; /**
* @author chengsh05
*
*/
@Document(collection="buzz_element")
public class BuzzElement implements Serializable { private static final long serialVersionUID = 1L; private Oid _id; @Field(value="item_id")
private String itemId;
@Field(value="item_name")
private String itemName;
@Field(value="price")
private String price;
@Field(value="desc")
private String desc; public Oid get_id() {
return _id;
} public void set_id(Oid _id) {
this._id = _id;
} public String getItemId() {
return itemId;
} public void setItemId(String itemId) {
this.itemId = itemId;
} public String getItemName() {
return itemName;
} public void setItemName(String itemName) {
this.itemName = itemName;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
} public String getDesc() {
return desc;
} public void setDesc(String desc) {
this.desc = desc;
} @Override
public String toString() {
return "ItemInfo [_id=" + _id + ", itemId=" + itemId + ", itemName="
+ itemName + ", price=" + price + ", desc=" + desc + "]";
}
}
再创建工具类BeanUtil:
/**
* @author "shihuc"
* @date 2017年5月3日
*/
package com.roomdis.center.mongo.util; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Date; import org.apache.commons.beanutils.BeanUtils; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; /**
* @author chengsh05
*
*/
public class BeanUtil { /**
* 把实体bean对象转换成DBObject
* @param bean
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static <T> DBObject bean2DBObject(T bean) throws IllegalArgumentException,
IllegalAccessException {
if (bean == null) {
return null;
}
DBObject dbObject = new BasicDBObject();
// 获取对象对应类中的所有属性域
Field[] fields = bean.getClass().getDeclaredFields(); // 获取所有注解
for (Field field : fields) {
org.springframework.data.mongodb.core.mapping.Field anno = field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class);
// 获取属性名
String varName = field.getName(); if("serialVersionUID".equals(varName) || "_id".equals(varName)){
continue;
} // 获取注解的值
varName = anno.value();
// 修改访问控制权限
boolean accessFlag = field.isAccessible();
if (!accessFlag) {
field.setAccessible(true);
}
Object param = field.get(bean);
if (param == null) {
continue;
} else if (param instanceof Integer) {//判断变量的类型
int value = ((Integer) param).intValue();
dbObject.put(varName, value);
} else if (param instanceof String) {
String value = (String) param;
dbObject.put(varName, value);
} else if (param instanceof Double) {
double value = ((Double) param).doubleValue();
dbObject.put(varName, value);
} else if (param instanceof Float) {
float value = ((Float) param).floatValue();
dbObject.put(varName, value);
} else if (param instanceof Long) {
long value = ((Long) param).longValue();
dbObject.put(varName, value);
} else if (param instanceof Boolean) {
boolean value = ((Boolean) param).booleanValue();
dbObject.put(varName, value);
} else if (param instanceof Date) {
Date value = (Date) param;
dbObject.put(varName, value);
}
// 恢复访问控制权限
field.setAccessible(accessFlag);
} return dbObject;
} /**
* 把DBObject转换成bean对象
* @param dbObject
* @param bean
* @return
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public static <T> T dbObject2Bean(DBObject dbObject, T bean) throws IllegalAccessException,
InvocationTargetException, NoSuchMethodException {
if (bean == null) {
return null;
}
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
String varName = field.getName();
org.springframework.data.mongodb.core.mapping.Field anno = field.getAnnotation(org.springframework.data.mongodb.core.mapping.Field.class);
if("serialVersionUID".equals(varName) || "_id".equals(varName)){
continue;
} String fieldName = anno.value();
Object object = dbObject.get(fieldName);
if (object != null) {
BeanUtils.setProperty(bean, varName, object);
}
}
return bean;
}
}
再次,创建服务,进行操作数据库,相当于dao层:
/**
* @author "shihuc"
* @date 2017年5月3日
*/
package com.roomdis.center.mongo.service; import java.util.List; import org.json.JSONObject; import com.roomdis.center.mongo.model.BuzzElement; /**
* @author chengsh05
*
*/
public interface IBuzzElementService {
// 查询
public List<BuzzElement> getItemInfo(JSONObject json) throws Exception; // 保存
public int save(BuzzElement itemInfo) throws Exception; // 更新
public void update(BuzzElement intemInfo) throws Exception;
}
/**
* @author "shihuc"
* @date 2017年5月3日
*/
package com.roomdis.center.mongo.service.impl; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service; import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.roomdis.center.mongo.model.BuzzElement;
import com.roomdis.center.mongo.service.IBuzzElementService;
import com.roomdis.center.mongo.util.BeanUtil; /**
* @author chengsh05
*
*/
@Service("buzzElementService")
public class BuzzElementServiceImpl implements IBuzzElementService { @Autowired
private MongoTemplate mongoTemplate;
private final static String COLLECTION_NAME = "buzz_element"; /* (non-Javadoc)
* @see com.roomdis.center.mongo.service.IBuzzElementService#getItemInfo(org.json.JSONObject)
*/
@Override
public List<BuzzElement> getItemInfo(JSONObject json) throws Exception {
List<BuzzElement> list = new ArrayList<BuzzElement>();
// 判断查询的json中传递过来的参数
DBObject query = new BasicDBObject(); if(json.has("item_id")){
query.put("item_id", json.getString("item_id"));
}else if(json.has("item_name")){
query.put("item_name", json.getString("item_name"));
} DBCursor results = mongoTemplate.getCollection(COLLECTION_NAME).find(query);
if(null != results){
Iterator<DBObject> iterator = results.iterator();
while(iterator.hasNext()){
BasicDBObject obj = (BasicDBObject) iterator.next();
BuzzElement itemInfo = new BuzzElement();
itemInfo = BeanUtil.dbObject2Bean(obj, itemInfo);
list.add(itemInfo);
}
} return list;
} /* (non-Javadoc)
* @see com.roomdis.center.mongo.service.IBuzzElementService#save(com.roomdis.center.mongo.model.BuzzElement)
*/
@Override
public int save(BuzzElement itemInfo) throws Exception {
DBCollection collection = this.mongoTemplate.getCollection(COLLECTION_NAME);
int result = ;
DBObject iteminfoObj = BeanUtil.bean2DBObject(itemInfo); //iteminfoObj.removeField("serialVersionUID");
//result = collection.insert(iteminfoObj).getN();
WriteResult writeResult = collection.save(iteminfoObj);
result = writeResult.getN();
return result;
} /* (non-Javadoc)
* @see com.roomdis.center.mongo.service.IBuzzElementService#update(com.roomdis.center.mongo.model.BuzzElement)
*/
@Override
public void update(BuzzElement intemInfo) throws Exception {
DBCollection collection = this.mongoTemplate.getCollection(COLLECTION_NAME);
BuzzElement queryItemInfo = new BuzzElement();
queryItemInfo.setItemId(intemInfo.getItemId());
DBObject itemInfoObj = BeanUtil.bean2DBObject(intemInfo);
DBObject query = BeanUtil.bean2DBObject(queryItemInfo);
collection.update(query, itemInfoObj);
}
}
郑重说明,上述的javabean以及BeanUtil还有服务程序类,是参照网络资源,考虑到这些东西不是本博的重点,也不是关键,所以没有花时间在这个上面。
最后,我们写一个controller来模拟一下添加数据,查询数据以及更新数据,看看工作流程是否顺畅!
/**
* @author "shihuc"
* @date 2017年5月3日
*/
package com.roomdis.center.mongo.controller; import java.util.List; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import com.roomdis.center.mongo.model.BuzzElement;
import com.roomdis.center.mongo.service.IBuzzElementService; /**
* @author chengsh05
*
*/
@Controller
@RequestMapping("/mongo")
public class MongoOpController { @Resource(name="buzzElementService")
private IBuzzElementService bes; @RequestMapping("/save")
@ResponseBody
public String doSave(HttpServletRequest req){ String itemName = req.getParameter("itemName");
String price = req.getParameter("price");
String desc = req.getParameter("desc");
BuzzElement be = new BuzzElement();
be.setItemName(itemName);
be.setDesc(desc);
be.setPrice(price);
try {
bes.save(be);
} catch (Exception e) {
e.printStackTrace();
}
return "Save OK";
} @RequestMapping("/find")
@ResponseBody
public String doFind(HttpServletRequest req){ String itemName = req.getParameter("itemName");
JSONObject json = new JSONObject();
json.putOnce("item_name", itemName);
List<BuzzElement> res = null;
try {
res = bes.getItemInfo(json);
} catch (Exception e) {
e.printStackTrace();
} return "Find OK: " + res.size() ;
} @RequestMapping("/update")
@ResponseBody
public String doUpdate(HttpServletRequest req){ String itemName = req.getParameter("itemName");
String price = req.getParameter("price");
String desc = req.getParameter("desc"); BuzzElement be = new BuzzElement();
be.setItemName(itemName);
be.setDesc(desc);
be.setPrice(price); try {
bes.update(be);
} catch (Exception e) {
e.printStackTrace();
} return "Update OK";
}
}
好了,代码都准备ok了,下面进行测试!
A.在浏览器地址栏执行:http://10.90.9.20:8080/RDcenter/mongo/save
数据库得到的数据:
> show collections
buzz_element
user
> db.buzz_element.find()
{ "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }
B. 添加一条数据。在浏览器地址栏里面输入: http://10.90.9.20:8080/RDcenter/mongo/save?itemName=iphone7&price=7000&desc=good product, everyone loves it
数据库得到的数据:
> db.buzz_element.find()
{ "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }
{ "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "", "desc" : "good product, everyone loves it" }
再次添加一条数据,在浏览器地址栏里面输入: http://10.90.9.20:8080/RDcenter/mongo/save?itemName=S8&price=8000&desc=samsung new product
数据库得到的数据:
> db.buzz_element.find()
{ "_id" : ObjectId("59092f3e0ef2eb286cd7410a") }
{ "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "", "desc" : "good product, everyone loves it" }
{ "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "", "desc" : "samsung new product" }
C.查询数据,在浏览器地址栏输入:http://10.90.9.20:8080/RDcenter/mongo/find?itemName=S8
最终,浏览器上得到的返回结果是:
D. 更新数据,在浏览器地址栏输入:http://10.90.9.20:8080/RDcenter/mongo/update?itemName=S8&price=9000&desc=hope it is nice
更新前的数据库数据:
> db.buzz_element.find()
{ "_id" : ObjectId("59092f3e0ef2eb286cd7410a"), "item_name" : "iphone7", "desc" : "not everyone will buy it" }
{ "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "", "desc" : "good product, everyone loves it" }
{ "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "", "desc" : "samsung new product" }
更新后的数据库数据:
> db.buzz_element.find()
{ "_id" : ObjectId("59092f3e0ef2eb286cd7410a"), "item_name" : "S8", "price" : "", "desc" : "hope it is nice" }
{ "_id" : ObjectId("590930480ef2eb286cd7410b"), "item_name" : "iphone7", "price" : "", "desc" : "good product, everyone loves it" }
{ "_id" : ObjectId("590933870ef2eb1bf8d04d70"), "item_name" : "S8", "price" : "", "desc" : "samsung new product" }
从上面看,数据库是更新了,但是貌似有点逻辑问题,将第一个更新了,我们希望是将既有的item_name为S8产品数据更新的,这个是业务逻辑问题,暂且不做修改了。。。
从上面的全部过程来看,基于spring+mongodb的集成工作,完美结束,也很简单。
剩下来的,重点是mongodb的增删改查相关的接口研究,是个熟悉的过程,各位朋友,建议去看官网的资料。
spring4与mongodb的集成的更多相关文章
- spring boot MongoDB的集成和使用
前言 上一章节,简单讲解了如何集成Spring-data-jpa.本章节,我们来看看如何集成NoSQL的Mongodb.mongodb是最早热门非关系数据库的之一,使用也比较普遍.最适合来存储一些非结 ...
- SpringBoot | 第三十一章:MongoDB的集成和使用
前言 上一章节,简单讲解了如何集成Spring-data-jpa.本章节,我们来看看如何集成NoSQL的Mongodb.mongodb是最早热门非关系数据库的之一,使用也比较普遍.最适合来存储一些非结 ...
- 实例演示如何在spring4.2.2中集成hibernate5.0.2并创建sessionFactory
本文地址:http://blog.csdn.net/sushengmiyan/article/details/49388209 文章作者:苏生米沿 本文目的:使用spring4.2.2集成hibern ...
- Spring4 + Quartz-2.2.0集成实例
Spring3.0不支持Quartz2.0,因为org.quartz.CronTrigger在2.0从class变成了一个interface造成IncompatibleClassChangeError ...
- Spring4 SpringMVC Hibernate4 Freemaker 集成示例
变更更正(2014-05-30 13:47:22):一些IDE在web.xml我们会报告这个错误: cvc-complex-type.2.4.a: Invalid content was found ...
- springBoot系列教程02:mongodb的集成及使用
1.安装mongodb mongdb的安装很简单,只需要下载解压后运行mongod就好了 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86 ...
- springboot~Mongodb的集成与使用
说说springboot与大叔lind.ddd的渊源 Mongodb在Lind.DDD中被二次封装过(大叔的.net和.net core),将它当成是一种仓储来使用,对于开发人员来说只公开curd几个 ...
- mongodb spring 集成
参考文档 mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?op ...
- MongoDB 项目集成 mongo-driver 3.4.2
第一次写技术!大白话讲讲.拿着用就可以了 本人是,NET的技术人员,会点JAVA所以很多不专业,见谅哈 刚刚开始使用mongo 整整搞了两天我才搞个半桶水,还是将就着用吧 随便把mongo在win的搭 ...
随机推荐
- shell 批量计算MD5值
#!/bin/sh #需要计算MD5文件列表 # list=`ls` list="file list" for file in $list do file1=`` echo &qu ...
- oracle函数创建及调用
创建函数的语法如下: CREATE [OR REPLACE] FUNCTION function_name[ (argment [ { IN | OUT | IN OUT } ] Type , arg ...
- ARM ELF函数重定位
ARM ELF的函数重定位与x86是一致的,但由于汇编指令不同,再鼓捣一遍. 示例代码: #include <stdio.h> #include <stdlib.h> int ...
- 【02_258】Add Digits
Add Digits Total Accepted: 49702 Total Submissions: 104483 Difficulty: Easy Given a non-negative int ...
- Xlua使用教程、攻略
Xlua 使用指南 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...
- div和span与块级和行内标签
<div>标签: 是一个区块容器标记,<div></div>之间是一个容器, 可以包含段落.表格.图片等各种HTML元素. <span>标签: 没有实际 ...
- java-继承的注意事项
1.子类只能继承父类所有非私有的成员(成员方法和成员变量). 2.子类不能继承父类的构造方法,但是可以通过super关键字去访问父类构造方法. 3.不要为了部分功能而去继承.
- Windows共享设置
Windows文件共享默认是开启的,任何用户都可以方便的设置共享目录.那么如何关闭并禁止呢. ====打开或关闭网上邻居的网络发现:1. 右键单击,网上邻居,弹出菜单选择:属性,打开网络和共享中心界面 ...
- hdu4300 Clairewd’s message 扩展KMP
Clairewd is a member of FBI. After several years concealing in BUPT, she intercepted some important ...
- ajax解决跨域
http://www.cnblogs.com/sunxucool/p/3433992.html 为什么会出现跨域跨域问题来源于JavaScript的同源策略,即只有 协议+主机名+端口号 (如存在)相 ...