springMVC+MongoDB配置

举报
扶醉 发表于 2020/12/31 00:47:32 2020/12/31
3.9k+ 0 0
【摘要】 一、配置文件路径 二、pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http:...

一、配置文件路径

二、pom.xml配置


      <project xmlns="http://maven.apache.org/POM/4.0.0"
      xmlns:xsi="http://www.w3.org/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>mongod</groupId>
      <artifactId>mongod</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <name>mongod</name>
      <description>mongod web</description>
      <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
      <dependencies>
      <!--spring测试需要的包-->
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.0.0.RELEASE</version>
      <scope>provided</scope>
      </dependency>
      <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
      </dependency>
      <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
      </dependency>
      <dependency>
      <groupId>org.apache.openejb</groupId>
      <artifactId>javaee-api</artifactId>
      <version>5.0-1</version>
      <scope>provided</scope>
      </dependency>
      <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>provided</scope>
      </dependency>
      <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
      </dependency>
      <!-- aop -->
      <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.7.1</version>
      </dependency>
      <!-- mvc -->
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.0.0.RELEASE</version>
      </dependency>
      <!-- jstl -->
      <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      </dependency>
      <!-- mongodb -->
      <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-mongodb</artifactId>
      <version>1.5.5.RELEASE</version>
      </dependency>
      <!--JSONObject-->
      <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
      <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.0.4</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
      <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.3</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
      <dependency>
      <groupId>commons-collections</groupId>
      <artifactId>commons-collections</artifactId>
      <version>3.2</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
      <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.7.0</version>
      </dependency>
      <!--json-lib -->
      <dependency>
      <groupId>com.hynnet</groupId>
      <artifactId>json-lib</artifactId>
      <version>2.4</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
      <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.54</version>
      </dependency>
      <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
      <dependency>
      <groupId>net.sf.ezmorph</groupId>
      <artifactId>ezmorph</artifactId>
      <version>1.0.4</version>
      </dependency>
      </dependencies>
      <build>
      <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
      <source>1.8</source>
      <target>1.8</target>
      </configuration>
      </plugin>
      </plugins>
      </build>
      </project>
  
 

三、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:aop="http://www.springframework.org/schema/aop"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      	xmlns:p="http://www.springframework.org/schema/p"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:cache="http://www.springframework.org/schema/cache"
      	xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      	<!-- 注解支持 -->
      	<context:annotation-config></context:annotation-config>
      	<!-- 组件扫描,排除controller,该组件由springmvc配置文件扫描 -->
      	<context:component-scan base-package="net"></context:component-scan>
      	<!-- 配置文件 -->
      	<bean id="annotationPropertyConfigurer"
     		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      		<property name="locations">
      			<list>
       <value>classpath:config/properties/mongodb.properties</value>
      			</list>
      		</property>
      	</bean>
      	<!-- 实体映射 -->
      	<bean id="packagesToScan"
     		class="org.springframework.beans.factory.config.ListFactoryBean">
      		<property name="sourceList">
      			<list>
       <value>net.fuzui.pojo</value>
      			</list>
      		</property>
      	</bean>
      	<!-- 注解式事务 -->
      	<tx:annotation-driven
      		transaction-manager="transactionManager" />
      	<bean id="multipartResolver"
     		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
      		<property name="defaultEncoding" value="utf-8"></property>
      	</bean>
      	<import resource="classpath:config/mongodb/mongodb.xml"/>
      </beans>
  
 

四、springMVC.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:mvc="http://www.springframework.org/schema/mvc"
      	xmlns:p="http://www.springframework.org/schema/p"
      	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.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
      <!-- 支持注解 -->
      	<mvc:annotation-driven />
      	<!-- 自动装配handlermapping和hangleradapter -->
      	<mvc:default-servlet-handler/>
      	<!-- 扫描路由控制器 -->
      	<context:component-scan base-package="net.fuzui" />
      	<!-- 设置静态资源访问 -->
      	<mvc:resources location="/" mapping="/**"/>
      	<!-- 视图解析器 -->
      	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      		<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
      		<property name="prefix" value="/WEB-INF/views"></property>
      		<property name="suffix" value=".jsp"></property>
      	</bean>
      </beans>
  
 

五、mongodb.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:aop="http://www.springframework.org/schema/aop"
      	xmlns:context="http://www.springframework.org/schema/context"
      	xmlns:p="http://www.springframework.org/schema/p"
      	xmlns:tx="http://www.springframework.org/schema/tx"
      	xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
      	<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-retry="${mongo.autoConnectRetry}"
       socket-keep-alive="${mongo.socketKeepAlive}"
       socket-timeout="${mongo.socketTimeout}"
       slave-ok="${mongo.slaveOk}"
       write-number="1"
       write-timeout="0"
       write-fsync="true" />
      	</mongo:mongo>
      	<mongo:db-factory dbname="database" mongo-ref="mongo" />
      	<bean id="mongoTemplate"
     		class="org.springframework.data.mongodb.core.MongoTemplate">
      		<constructor-arg name="mongo" ref="mongo" />
      		<constructor-arg name="databaseName" value="test" />
      	</bean>
      </beans>
  
 

六、mongodb.properties配置文件


      mongo.hostport=127.0.0.1:27017
      mongo.connectionsPerHost=8
      mongo.threadsAllowedToBlockForConnectionMultiplier=4
      #连接超时时间
      mongo.connectTimeout=1000
      #等待时间
      mongo.maxWaitTime=1500
      mongo.autoConnectRetry=true
      mongo.socketKeepAlive=true
      #Socket超时时间
      mongo.socketTimeout=1500
      mongo.slaveOk=true
  
 

七、web.xml配置


      <?xml version="1.0" encoding="UTF-8"?>
      <!--
        - This is the Cocoon web-app configurations file
        -
        - $Id$
        -->
      <web-app version="2.4"
       xmlns="http://java.sun.com/xml/ns/j2ee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <!-- 加载spring ioc容器 -->
        <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:config/spring/applicationContext.xml</param-value>
        </context-param>
        <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
       <servlet-name>springMVC</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <init-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:config/spring/springMVC.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
       </servlet>
       <!-- Map all requests to the DispatcherServlet for handling -->
       <servlet-mapping>
       <servlet-name>springMVC</servlet-name>
       <url-pattern>/</url-pattern>
       </servlet-mapping>
       <filter>
       <filter-name>encodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
       </init-param>
       <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
       </init-param>
       </filter>
       <filter-mapping>
       <filter-name>encodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
       </filter-mapping>
       <listener>
       <listener-class>
       org.springframework.web.context.ContextLoaderListener
       </listener-class>
       </listener>
       <!--实现rest风格架构-->
       <filter>
       <filter-name>HiddenHttpMethodFilter</filter-name>
       <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
       </filter>
       <filter-mapping>
       <filter-name>HiddenHttpMethodFilter</filter-name>
       <url-pattern>/*</url-pattern>
       </filter-mapping>
      </web-app>
  
 

文章来源: fuzui.blog.csdn.net,作者:极客时代,版权归原作者所有,如需转载,请联系作者。

原文链接:fuzui.blog.csdn.net/article/details/88043618

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。