With spring and MONGOLDB integration, the versions of the spring and MongoDB-related jars are right, otherwise there will be problems in development; I manage these jar files with Maven, and using MAVEN, it's easy to manage these resource files without looking around for jar packs;
Spring, SPRINGMVC, and MongoDB integration, which is primarily configured with spring and SPRINGMVC and MongoDB configurations, as follows:
1, integrated the required jar file: Pom.xml
<?xml version= "1.0"?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= 2001/xmlschema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache.org/xsd/maven -4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactid >springmvc_mongodb</artifactId> <packaging>war</packaging> <version>0.0.1-snapshot </version> <name>springmvc_mongodb Maven webapp</name> <url>http://maven.apache.org</ url> <properties> <spring.version>3.2.3.RELEASE</spring.version> <slf4j.version>1.7.2& Lt;/slf4j.version> <log4j.version>1.2.16</log4j.version> <junit.version>4.11</ Junit.version> <jsonlib.version>2.4</jsonlib.version> <shiro.version>1.2.2</ shiro.version> </properties> <dependencies> <dependency> <gRoupid>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version> ${spring.version}</version> </dependency> <dependency> <groupid>org.springframework.data& Lt;/groupid> <artifactId>spring-data-mongodb</artifactId> <version>1.2.0.release</ version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifa Ctid>spring-tx</artifactid> <version>${spring.version}</version> </dependency> ;d ependency> <groupId>org.springframework</groupId> <artifactid>spring-test</artifactid > <version>${spring.version}</version> <scope>test</scope> <exclusions> < Exclusion> <groupId>commons-logging</groupId> <artifactid>commons-logging</artifactid>
; </exclusion> </EXCLUSIONS> </dependency> <!--Json--> <dependency> <groupId>org.codehaus.jackson</groupId> & Lt;artifactid>jackson-core-lgpl</artifactid> <version>1.8.1</version> </dependency> & Lt;dependency> <groupId>org.codehaus.jackson</groupId> <artifactid>jackson-mapper-lgpl</ artifactid> <version>1.8.1</version> </dependency> <dependency> <groupid>net.sf .json-lib</groupid> <artifactId>json-lib</artifactId> <version>${jsonlib.version}</ Version> <classifier>jdk15</classifier> </dependency> <!--end of Json--> <!--Lo Gging--> <dependency> <groupId>org.slf4j</groupId> <artifactid>slf4j-api</artifacti d> <version>${slf4j.version}</version> </dependency> <dependency> <groupid>org. Slf4j</groupid> <artIfactid>jcl-over-slf4j</artifactid> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> &L t;version>${slf4j.version}</version> </dependency> <dependency> <groupid>log4j</gro upid> <artifactId>log4j</artifactId> <version>${log4j.version}</version> <scope> Provided</scope> </dependency> <!--end of logging--> <!--fileupload--> <dependency&
Gt <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> < Version>1.2.2</version> </dependency> <!--end of FileUpload--> <!--io--> <depe ndency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version& Gt;2.4</version> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api< /artifactid> <version>7.0</version> <scope>provided</scope> </dependency> <d ependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${ju nit.version}</version> <scope>test</scope> </dependency> <dependency> <groupid >javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> &L T;/dependency> </dependencies> </project>
2. Configure Web.xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "> <display-name>gms</ display-name> <context-param> <param-name>log4jConfigLocation</param-name> <PARAM-VALUE&G T;classpath:log4j.properties</param-value> </context-param> <!--Spring's log4j listener ****************** --> <!--Set LOG4J profile path--> <!--Open a watchdog thread scans the configuration file for changes every 60 seconds--> <context-p
Aram> <param-name>log4jRefreshInterval</param-name> <param-value>600000</param-value> </context-param> <!--monitor start--> <listener> <listener-class>org.springframework.web.util. Log4jconfiglistener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </ Listener> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener<
/listener-class> </listener> <!--listening end--> <!--spring configuration--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-context.xml </param-value> </context-param> <!--spring MVC configuration--> <servlet> <servlet -name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet&
Lt;/servlet-class> <init-param> <param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value> <!--Specify the location of the XML file--> </init-param> ≪load-on-startup>4</load-on-startup> </servlet> <servlet-mapping> <servlet-na
Me>springmvc</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <filter> <description> Character Set filter </description> <filter-name>encodingfilter</filter- Name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <
Init-param> <description> Character Set coding </description> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> ;filter-name>encodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping > <filter> <filter-name>sitemesh</filter-name> <filter-class>org.sitemesh.config.conf Igurablesitemeshfilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pat tern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout> 15</session-timeout> </session-config> </web-app>
3. Configure Spring-mvc.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= " http://www.springframework.org/schema/p "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.0.xsd "> <!--automatically scans all classes under the controller package so that it considers the controller--> of spring MVC <context:component-scan Base-package= "Com.sy.gms.controller"/> <!--avoid IE to execute AJAX when the return JSON appears to download files--> <bean id= " Mappingjacksonhttpmessageconverter "Class=" Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name= "Supportedmediatypes" > <list> <value>text/html;charset=UTF-8</value> </ List> </property> </bean> <!--start Spring JSON conversion--> <bean class= "Org.springframework.web.s
Ervlet.mvc.annotation.AnnotationMethodHandlerAdapter "> <property name=" messageconverters "> <list> <bean class= "Org.springframework.http.converter.StringHttpMessageConverter"/> <bean class= " Org.springframework.http.converter.json.MappingJacksonHttpMessageConverter "> <property name=" Supportedmediatypes "> <list> <value>application/json;charset=UTF-8</value> </li st> </property> </bean> </list> </property> </bean> <!--static resource access-- > <!--<mvc:resources mapping= "/resources/**" location= "/resources/"/>--> <!--<mvc:reso Urces mapping= "/css/**" location= "/css/"/> <mvc:resources mapping= "/images/**" location= "/images/"/> <mvc:resources mapping= "/js/**" location= "/js/"/>--> < !--resolves the model view name by adding the prefix--> <bean class= to the Model view name Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" prefix "value="/web-inf /views "/> <property name=" suffix "value=". jsp "/> </bean> <bean id=" Multipartresolver "class=" or
G.springframework.web.multipart.commons.commonsmultipartresolver "> <property name=" defaultEncoding "> <value>UTF-8</value> </property> <property name= "Maxuploadsize" > <value>209715200< ;/value><!--upload file size limit to 31m,200*1024*1024--> </property> <property name= "Maxinmemorysize" > < value>4096</value> </property> </bean> </beans>
4. Configure Spring.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans H
Ttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <!--Open Spring's annotation support--> <context:annotation-config/> <!--setting SPRWhat bag does ing go to find annotation--> <context:component-scan base-package= "Com.sy.gms"/> <!--entity mapping--> & Lt;bean id= "Packagestoscan" class= "Org.springframework.beans.factory.config.ListFactoryBean" > <property na
Me= "SourceList" > <list> <value>com.sy.gms.model</value> </list> </property> </bean> <!--Configure MongoDB user and password--> <bean id= "USERCR Edentials "class=" org.springframework.data.authentication.UserCredentials "> <constructor-arg name=" username "Value=" Ebank/> <constructor-arg name= "password" value= "Ebank"/> </bean> <!--introduce quartz tasks --> <!--<import resource= "Quartz.xml"/>--> <!--import MongoDB configuration file--> <import resource= "MongoDB -context.xml "/> </beans>
5. Configure Mongodb.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x
Si= "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 "> <context:property-placeholder location=" classpath:mongodb.prope Rties "/> <mongo:mongo id= Mongo" replica-set= "${mongo.hostport}" > <mongo:options connections-
Per-host= "${mongo.connectionsperhost}" Threads-allowed-to-block-for-connection-multiplier= "${mongo.threadsallowedtoblockforconnectionmultiplier}" connect-timeout= "${mongo.connecttimeout}" Max-wait-time= "${mongo.maxwaittime}" Auto-connect-ret ry= "${mongo.autoconnectretry}" socket-keep-alive= "${mongo.socketkeepalive}" socket-timeout= "${mo"
Ngo.sockettimeout} "slave-ok=" ${mongo.slaveok} "write-number=" 1 "write-timeout=" 0 " Write-fsync= "true"/> </mongo:mongo> <mongo:db-factory dbname= "database" mongo-ref= "mo NGO "/> <bean id= mongotemplate" class= "Org.springframework.data.mongodb.core.MongoTemplate" > ; Constructor-arg ref= "Mongo"/> <constructor-arg name= "databaseName" value= "Ebank"/> <constru Ctor-arg ref= "Usercredentials"/> </bean> </beans>
If MongoDB enables security authentication, configure the username and password when connecting, and configure the MongoDB username and password in spring in another posting: http://blog.csdn.net/supersanya/article/details/50446069
Configure these startup Tomcat,ok without errors, the whole environment is set up.