配置spring+springMvc+mongodb的架构实例

1.首先导入相应的jar包,如图:


2.进行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_3_0.xsd" version="3.0">
  <display-name>Spring3MVC</display-name>
<!--配置spring-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-config.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
		<filter-name>myEncoding</filter-name>
		<filter-class>com.duola.common.MyCharacterEncodingFilter</filter-class> <!--这个是自己编写的一个编码拦截器,进行utf-8编码。-->
	</filter>
	<filter-mapping>
		<filter-name>myEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
<!-- 配置一个过滤器,对访问网站的所有页面进行检测,这句可以不要,我是后期需要监控页面才配置的-->
	<filter>
		<filter-name>MyFilter</filter-name>
		<filter-class>com.duola.loterry.MyFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>MyFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>com.duola.loterry.MyListener</listener-class>
	</listener>
	
<pre name="code" class="html"><!--配置springMvc-->
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
 
 
3.配置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:util="http://www.springframework.org/schema/util"
    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:mvc="http://www.springframework.org/schema/mvc"
    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.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
         
     <!-- 开启controller注解支持 -->
    <!-- 注:如果base-package=com.sbs 则注解事务不起作用 TODO 读源码 -->
    <context:component-scan base-package="com.duola.loterry.contrcoller"/>
     
    
         
         
    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />
	<mvc:default-servlet-handler/>
    
    <!-- 配置基于Session的处理,将提交上来的locale参数进行处理 -->  
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <!-- 该属性可以不用配置 -->
        <property name="defaultLocale" value="ja"></property>
    </bean>  
    
    <!-- 视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="contentType" value="text/html"/>
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
    </bean>
    
    
</beans>

4.配置spring的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:util="http://www.springframework.org/schema/util"
    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 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
   
   <context:annotation-config />
   
   
   
   <!-- 扫描注解Bean -->
   <context:component-scan base-package="com.duola" use-default-filters="false">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
   
   
   		<!-- 导入mongodb的配置文件 -->
       <import resource="mongodb-context.xml"/>
</beans>

5.配置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: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.xsd   
          http://www.springframework.org/schema/beans   
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
          
    <!-- 加载mongodb的属性配置文件 -->
    <context:property-placeholder location="classpath:*.properties" />
    
    <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
    <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" />
    
    <!-- 默认Mongodb类型映射 -->
	<bean id="defaultMongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
		<constructor-arg name="typeKey">
			<null /><!-- 这里设置为空,可以把 spring data mongodb 多余保存的_class字段去掉 -->
		</constructor-arg>
	</bean>
    
    
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="mongo" />
        <constructor-arg name="databaseName" value="duola" />
    </bean>

</beans>

6.配置mongodb.propers

mongo.hostport=localhost:27017
mongo.connectionsPerHost=8
mongo.threadsAllowedToBlockForConnectionMultiplier=4
#\u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4
mongo.connectTimeout=10000
#\u7B49\u5F85\u65F6\u95F4
mongo.maxWaitTime=1500
mongo.autoConnectRetry=true
mongo.socketKeepAlive=true
#Socket\u8D85\u65F6\u65F6\u95F4
mongo.socketTimeout=1500
mongo.slaveOk=true
#http://192.168.1.191:27017/
7.然后自己写一个测试类就可以测试一下了。

猜你喜欢

转载自blog.csdn.net/hbl6016/article/details/48686049