spring MVC 配置文件

1.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" id="WebApp_ID" version="3.0">
  <display-name>notes</display-name>
  <!-- 指定Spring的配置文件所在目录。默认配置在WEB-INF目录下 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:resources/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <!-- 加载Spring的Listener监听 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!--配置springMVC(是总的控制中心  被拦截的url会汇聚到该servlet) -->  
  <servlet>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--配置spring上下文  (从该目录下加载spring mvc的配置文件) --> 
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:resources/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup><!-- tomcat启动后立即加载 -->
  </servlet>
  <!--配置spring拦截的url -->  
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

2.springmvc-servlet.xml:自动扫描控制器、视图、注解

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc.xsd" >

    <!-- 加载配置文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath*:/config.properties" />

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射 -->
    <mvc:annotation-driven />
    
    <!-- 对cn.com.ssk.controller包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->  
    <context:component-scan base-package="cn.com.ssk.controller" />
    
    <!-- 定义视图解析器,在视图模型前后添加前缀后缀-->  
    <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" /> <!-- 路径前缀 -->  
        <property name="suffix" value=".jsp" /><!-- 后缀 -->  
    </bean>
    
    <!-- 文件上传 -->
    <bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"       
          p:defaultEncoding="utf-8" />
</beans>

3.applicationContext.xml,整合spring和mybatis或者hibernate,主要是自动扫描,自动注入,数据库的配置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 加载配置属性文件 -->
    <context:property-placeholder
        ignore-unresolvable="true" location="classpath*:/resources/config.properties" />

    <!-- - - - - - 扫描业务层包,支持织入DAO - - - - - -->
    <context:annotation-config />
    <context:component-scan base-package="cn.com.ssk.service" />


    <!-- 阿里 druid数据库连接池 -->
    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <!-- 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。 -->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat" />
    </bean>

    <!-- - - - - - - - sessionFactory - - - - - - - -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource" />
        <property name="configLocation" value="classpath:resources/sqlMapConfig.xml" />
        <property name="mapperLocations" value="classpath*:cn/com/ssk/**/pojo/*/*Mapper.xml" />

    </bean>
    <!-- - - - - - - spring 声明式事务 - - - - - - - -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource" />
    </bean>
    <!-- - - - - - - spring 事务属性 - - - - - - - -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="addSendUnionPay" propagation="SUPPORTS"
                rollback-for="Exception" />
            <tx:method name="save*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="create*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="insert*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="edit*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="put*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="merge*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="drop*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="remove*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="cancel*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="pay*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="reback*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="charge*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="login*" propagation="REQUIRED"
                rollback-for="Exception" />
            <tx:method name="modify*" propagation="REQUIRED"
            rollback-for="Exception" />

            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="count*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="check*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="query*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!--****** 织入,请修改成实际的业务层包名 ********* -->
    <aop:config>
        <aop:pointcut id="targetMethod"
            expression="execution(* cn.com.ssk.*.service.*.*(..))" />
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="targetMethod" />

    </aop:config>
    <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.com.ssk.dao" />
    </bean>



</beans>

 

posted @ 2017-07-28 11:12  孟夏草木长  阅读(294)  评论(0编辑  收藏  举报