关于SpringMVC相关配置文件

来源:互联网 发布:无法连接到网络驱动器 编辑:程序博客网 时间:2024/05/16 12:39

提到Spring MVC ,是Web工程,那肯定需要web.xml,Spring的配置application.xml,Spring MVC的核心spring-mvc.xml。

有些开发喜欢把MVC的控制和Spring的配置放一起,但我自己觉得,还是分开的比较好,spring-mvc.xml就配置mvc相关东西,也就是Controller类,和视图解析。


一、Web.xml -> web项目的核心

1、配置servlet

// 配置Spring主文件

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-application.xml</param-value>

</context-param>

// 配置Spring监听

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

// 配置servlet

<servlet>

<servlet-name>springservlet</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-mvc.xml</param-value>

</init-param>

</servlet>

<servert-mapping>

<servlet-name>springservlet</servlet-name>

<url-patten>/</url-patten>

<servlet-mapping>


其他可以配置一些设置编码格式,或filter,这些在下面给出,并不是必须的


二、spring-application.xml配置

// Spring扫描配置

<context:component-scan base-package="com.spring.project"">

<context:exclude-filter type="annotaction" expression="org.springframework.stereotype.Controller"></context:excelude-filter>

// 该配置<context:exclude>表示不包含这些配置,Controller的扫描我们放到spring-mvc.xml里面,这样可以避免重复扫描

</context:component-scan>


// 读取属性文件

<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true" file-encoding="UTF-8" location="file:${configRootPath}/server.properties"
    properties-ref="localProperties">

</context:property-placeholder>

<bean id = "localProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

<property name="fileEncoding" value="utf-8"/>

<property name="locations">

<list>

<value>classpath:server.properties</value>

</list>

</property>

</bean>

// 一般只需上面<context:property-placeholder>即可,但有时会找不到对应位置的配置文件,这时候就使用properties-ref bean的相关配置

在程序里使用配置文件,可以使用@Value("${key}") ,但要注意:上面那个配置是在application配置里,只能用在除了Controller的地方,如果要在Controller的地方使用,则需要在DispatcherServlet配置中的mvc配置里,加上上面的配置,不然会出现空的情况


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<!-- Connection Info -->
    <property name="driverClassName" value="${jdbc.driver}" />
    <!-- Connection Pooling Info -->
    <!-- 连接池中最多可空闲maxIdle个连接 -->
    <property name="maxIdle" value="${dbcp.maxIdle}" />
    <property name="minIdle" value="${dbcp.minIdle}" />
    <property name="initialSize" value="${dbcp.initialSize}" />
    <!-- 连接池支持的最大连接数 -->
    <property name="maxActive" value="${dbcp.maxActive}" />
    <!-- 连接池中连接用完时,新的请求等待时间,毫秒 -->
    <property name="maxWait" value="${dbcp.maxWait}" />
    <property name="defaultAutoCommit" value="false" />
    <!-- 是否自动回收超时连接 -->
    <property name="removeAbandoned" value="true" />
    <!-- 超时时间(以秒数为单位) -->
    <property name="removeAbandonedTimeout" value="${dbcp.removeAbandonedTimeout}" />
    <!-- ,将会在回收事件后,在log中打印出回收Connection的错误信息,包括在哪个地方用了Connection却忘记关闭了,在调试的时候很有用 -->
    <property name="logAbandoned" value="true" />
    <!-- 那么在getNumActive()快要到getMaxActive()的时候,系统会进行无效的 Connection的回收,回收的Connection为removeAbandonedTimeout(默认300秒)中设置的秒数后没有使用的 
      Connection -->
    <property name="poolPreparedStatements" value="true" />


    <!-- 每timeBetweenEvictionRunsMillis毫秒检查一次连接池中空闲的连接,把空闲时间超过minEvictableIdleTimeMillis毫秒的连接断开,直到连接池中的连接数到minIdle为止 -->
    <property name="timeBetweenEvictionRunsMillis" value="${dbcp.timeBetweenEvictionRunsMillis}" />
    <!-- 连接池中连接可空闲的时间(毫秒),一定要小于mysql的wait_timeout的值 -->
    <property name="minEvictableIdleTimeMillis" value="${dbcp.minEvictableIdleTimeMillis}" />
    <!--<property name="numTestsPerEvictionRun" value="${dbcp.maxActive}" /> -->


    <!-- 取得连接时验证其有效性 -->
    <property name="testOnBorrow" value="false" />
    <!-- 返回连接时验证其有效性 -->
    <property name="testOnReturn" value="false" />
    <!-- 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除 -->
    <property name="testWhileIdle" value="true" />
    <!-- 验证sql -->
    <property name="validationQuery" value="select 1" />

</bean>


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:/mybatis/bc/Configuration.xml" />
    <property name="mapperLocations" value="classpath:/mybatis/project/*Mapper.xml" />
  </bean>  
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.project.dao.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

  </bean>


<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  
  <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />


三、Spring-mvc.xml相关配置,主要配置扫描controoler、视图解析、编码

<context:component-scan base-package="com.project" use-default-filters="false">

<context:include-filter type="annotation" expression="com.project.controller" />

</context:component-scan>

// 注意:一定要加上use-default-filters="false",否则事务会不起效。use-default-filter默认为true,表示会自动扫描标识Controller,service,component的表示。此处写false,表示不采用默认扫描,只扫描<context:include-filter>里面的注解

 <mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
    <mvc:message-converters register-defaults="true">
      <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <constructor-arg value="UTF-8" />
      </bean>
      <!-- 使用fastjson转换 -->
      <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="features">
          <list>
            <value>WriteEnumUsingToString</value>
            <value>PrettyFormat</value>
          </list>
        </property>
      </bean>
    </mvc:message-converters>
  </mvc:annotation-driven>


  <!-- REST中根据URL后缀自动判定Content-Type及相应的View -->
  <bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="ignoreAcceptHeader" value="true" />
    <property name="defaultContentType" value="application/json" />
    <property name="mediaTypes">
      <value>
        json=application/json
        xml=application/xml
      </value>
    </property>
  </bean>


  <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL -->
  <mvc:default-servlet-handler />




1 0
原创粉丝点击