1. 程式人生 > >2.SpringMVC+Spring+Mybatis整合(2) 配置web.xml,spring-servlet.xml,applicationContext.xml

2.SpringMVC+Spring+Mybatis整合(2) 配置web.xml,spring-servlet.xml,applicationContext.xml

  1. web spring-servlet 在 webapp WEB-INF下
  2. applicationContext 在resource資料夾下
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>Archetype Created Web Application</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Spring beans 配置檔案所在目錄 --> <context-param
>
<param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring mvc 配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class
>
</servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> /* 攔截所有, / 不攔截 /s.jsp </servlet-mapping> <!-- Encoding --> <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> <!-- druid --> <servlet> <servlet-name>DruidStatServlet</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> <init-param> <param-name>loginUsername</param-name> <param-value>druid</param-value> </init-param> <init-param> <param-name>loginPassword</param-name> <param-value>druid</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatServlet</servlet-name> <url-pattern>/sys/druid/*</url-pattern> </servlet-mapping> <filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.css,*.jpg,*.png,*.ico,*.gif,/sys/druid/*</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>loginFilter</filter-name> <filter-class>com.mmall.filter.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>loginFilter</filter-name> <url-pattern>/sys/*</url-pattern> <url-pattern>/admin/*</url-pattern> </filter-mapping> <filter> <filter-name>aclControlFilter</filter-name> <filter-class>com.mmall.filter.AclControlFilter</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>exclusionUrls</param-name> <param-value>/sys/user/noAuth.page,/login.page</param-value> </init-param> </filter> <filter-mapping> <filter-name>aclControlFilter</filter-name> <url-pattern>/sys/*</url-pattern> <url-pattern>/admin/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
  1. listener Spring
    1. ContextLoaderListener spring上下文bean的載入
  2. context-param
    1. contextConfigLocation 指定 applicationContext.xml
  3. servlet springMVC
    1. DispatcherServlet name為spring,所以目錄下WEB-INF建立 spring-servlet.xml
  4. filter Encoding
    1. CharacterEncodingFilter
  5. welcome-file-list

spring-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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:annotation-config />--> <!-- 放到applicationContext.xml裡了 -->

    <!-- 啟動註解驅動的spring mvc 功能 -->
    <mvc:annotation-driven/>

    <mvc:interceptors>
        <bean class="com.mmall.common.HttpInterceptor" />
    </mvc:interceptors>

    <!-- 啟動包掃描功能 -->
    <context:component-scan base-package="com.mmall.controller" />
    <!--<context:component-scan base-package="com.mmall.service" />--> <!-- 放到applicationContext.xml裡了 -->

    <mvc:resources location="/js/" mapping="/js/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/bootstrap3.3.5/" mapping="/bootstrap3.3.5/**"/>
    <mvc:resources location="/assets/" mapping="/assets/**"/>
    <mvc:resources location="/ztree/" mapping="/ztree/**"/>

    <!--<bean class="com.mmall.common.ApplicationContextHelper" lazy-init="false" />--> <!-- 放到applicationContext.xml裡了 -->

    <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />-->

    <bean class="com.mmall.common.SpringExceptionResolver" />

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />

    <bean id="jsonView" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" /> jsp在view資料夾下
        <property name="suffix" value=".jsp" />
    </bean>

</beans>
  1. 請求相關的配置
    1. 上下文的註解能夠被使用
    2. 啟動註解驅動的spring mvc 功能
    3. context:component-scan 包掃描
  2. RequestMappingHandlerMapping 找到對應的對映,已經註釋了
  3. BeanNameViewResolver 選擇具體的返回,如json 或者下載
    1. MappingJackson2JsonView JSON返回
    2. InternalResourceViewResolver JSP返回

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: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.xsd http://www.alibaba.com/schema/stat http://www.alibaba.com/schema/stat.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:settings.properties</value>
            </list>
        </property>
    </bean>

    <context:annotation-config /> <!-- 從spring-servlet.xml裡轉移過來的 -->
    <context:component-scan base-package="com.mmall.service" /> <!-- 從spring-servlet.xml裡轉移過來的 -->

    <bean class="com.mmall.common.ApplicationContextHelper" lazy-init="false" /> <!-- 從spring-servlet.xml裡轉移過來的 -->

    <import resource="redis.xml" />

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${db.driverClassName}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
        <property name="initialSize" value="3" />
        <property name="minIdle" value="3" />
        <property name="maxActive" value="20" />
        <property name="maxWait" value="60000" />
        <property name="filters" value="stat" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.mmall.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>

    <!-- tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- druid -->
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <property name="slowSqlMillis" value="3000" />
        <property name="logSlowSql" value="true" />
        <property name="mergeSql" value="true" />
    </bean>
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="mysql" />
    </bean>

</beans>
  1. PropertyPlaceholderConfigurer bean載入properties檔案 忽略掉不能讀取的站位
  2. dataSource DruidDataSource
  3. SqlSessionFactoryBean
    1. configLocation
    2. dataSource
    3. mapperLocations
  4. MapperScannerConfigurer
    1. basePackage
    2. sqlSessionFactoryBeanName
  5. DataSourceTransactionManager
    1. dataSource
    2. druid 的filter stat-filter wall-filter 不用可不配

settings.properties

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
db.username=root
db.password=466420182