首页 > 代码库 > SSM整理笔记3——配置解析

SSM整理笔记3——配置解析

github:https://github.com/lakeslove/SSM

项目的目录结构如下

技术分享

首先,配置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>SSM</display-name>
  
  <!-- session和cookie的配置 -->
  <session-config>
    <session-timeout>40</session-timeout>
    <cookie-config>
      <name>SSM</name>
      <http-only>true</http-only>
    </cookie-config>
  </session-config>
  
  <!-- 访问的首页的配置 -->
  <welcome-file-list>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>
  
  <!-- springMVC的拦截器配置,这个是重点 -->
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/spring-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 防止乱码的配置,这个配置的mapping一定要在loginFilter之前,否则loginFilte里乱码 -->
  <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>
    <servlet-name>spring-dispatcher</servlet-name>
  </filter-mapping>
  
  <!-- 自定义的登录过滤器 -->
  <filter>
    <display-name>LoginFilter</display-name>
    <filter-name>LoginFilter</filter-name>
    <filter-class>com.lx.filter.LoginFilter</filter-class>
    <init-param>
      <param-name>indexPath</param-name>
      <param-value>index.htm</param-value>
    </init-param>
    <init-param>
      <param-name>ignoreList</param-name>
      <param-value></param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
  </filter-mapping>
  
  <!-- 错误页面,平常多用静态页面,这里用了动态页面,目的是为了对异常进行动态的处理 -->
  <error-page>
    <error-code>500</error-code>
    <location>/testError.htm</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/testError.htm</location>
  </error-page>
  
  <!-- 测试用的Servlet -->
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.lx.servlets.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet.htm</url-pattern>
  </servlet-mapping>
  
  <!-- 测试用的监听器 -->
  <listener>
    <listener-class>com.lx.listeners.TestListenser</listener-class>
  </listener>
</web-app>

springMVC和spring的配置文件:spring-context.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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-4.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <context:property-placeholder location="classpath:system.properties" />
    <context:annotation-config />
    <!-- 使用注解管理事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>    
    <cache:annotation-driven cache-manager="cacheManager"/>
    <task:executor id="taskExecutor" pool-size="1-20" queue-capacity="30" keep-alive="60" />
    <task:scheduler id="scheduler" pool-size="1" />
    <task:annotation-driven executor="taskExecutor" scheduler="scheduler" />
    <!-- 需要扫描的包 start-->
    <context:component-scan base-package="com.lx.controllers" />
    <context:component-scan base-package="com.lx.services.impl" />
    <!-- 需要扫描的包 end -->
    
     <mvc:annotation-driven />
     <!-- 数据验证 start -->
    <!-- 数据验证 end -->
     <!-- viewResolver start -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="order" value="2" />
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
    <!-- tiles 配置开始 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="order" value="1" />
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles3.TilesView
            </value>
        </property>
    </bean>
    
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/classes/tilesPublic.xml</value>
            </list>
        </property>
    </bean>
    <!-- tiles 配置结束 -->
    <!-- viewResolver end -->
    
    <!--  hibernate start-->
    <!-- jndi start -->
    <!--      
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
        <property name="jndiName">  
            <value>${db.jndiname}</value>  
        </property>  
    </bean> 
    -->
    <!-- jndi end -->
    
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
        destroy-method="close">
        <property name="url" value="${db.url}"></property>
        <property name="driverClassName" value="${db.driver}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
        <property name="maxTotal" value="30" />
        <property name="maxIdle" value="20" />
        <property name="minIdle" value="5" />
        <property name="initialSize" value="5" />
        <property name="maxWaitMillis" value="-1" />
        <property name="timeBetweenEvictionRunsMillis" value="120000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="removeAbandonedOnBorrow" value="true" />
        <property name="validationQuery">
            <value>select 1 from dual</value>
        </property>
    </bean>
    
    <!-- 使用MyBatis-spring的sqlSessionFactory来代替MyBatis本身的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml" />
    </bean>

    <!-- 使用MyBatis-spring的sqlSessionTemplate来包装sqlSessionFactory -->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <!-- <constructor-arg index="1" value="http://www.mamicode.com/BATCH" /> -->
    </bean>
    
    <!-- 事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 采用自动扫描方式创建mappper bean -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com"/>
        <property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
        <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
    </bean>
    
    <!-- 国际化(多语言对应) 开始 -->
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>message_error</value>
                <value>message_generic</value>
                <value>message_validate</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false" />
        <property name="defaultEncoding" value="UTF-8" />
        <property name="cacheSeconds" value="600" />
    </bean>
    <!-- 国际化(多语言对应) 结束-->
    
    <!-- 异常处理 start -->
    <!-- <bean class="com.lx.exceptions.ExceptionHandler" /> -->
    <bean id="exceptionResolver"
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>
            </props>
        </property>
    </bean>
    <!-- 异常处理 end -->
    
    <!-- 缓存支持 start -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <bean
                    class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                    p:name="masterCache" />
            </set>
        </property>
    </bean>
    <!-- 缓存支持 end -->
    
    <!-- 文件上传限制 开始 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="maxUploadSize" value="10485760" />
        <property name="maxInMemorySize" value="1048576" />
    </bean>
    <!-- 文件上传限制 结束 -->
    
    <!-- velocityEngine start-->
    <bean id="velocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="resourceLoaderPath">
            <value>classpath:velocity</value>
        </property>
        <property name="velocityProperties">
            <props>
                <prop key="file.resource.loader.cache">false</prop>
                <prop key="file.resource.loader.modificationCheckInterval">3</prop>
                <prop key="input.encoding">UTF-8</prop>
                <prop key="output.encoding">UTF-8</prop>
            </props>
        </property>
    </bean>
    <!-- velocityEngine end -->

    <!-- sendEmail start -->
    <!-- 解除默认的单例模式,设置scope="prototype" -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" scope="prototype">
        <property name="host" value="${mail.host}" />
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
                <prop key="mail.smtp.port">${mail.smtp.port}</prop>
            </props>
        </property>
    </bean>
    <!-- sendEmail end -->
    
    <!-- IOC demo start -->
    <!-- springFrome initiate check start -->
    <!-- <bean class="com.lx.utils.CheckConfigs">
        <property name="dbUrl" value="http://www.mamicode.com/${db.url}" />
        <property name="userDao">
            <bean class="com.lx.daos.impl.UserDaoImpl" /> 
        </property>
    </bean> -->
    <!-- initiate check end -->
    
    <!-- 在过滤器中注入service start-->
    <bean id="userService" class="com.lx.services.impl.UserServiceImpl"></bean>
    <!-- 在过滤器中注入service end-->
    
    <!-- IOC demo end-->
    
    <!-- AOP demo start  -->
    <!-- 切面类 -->
    <bean id="appendLogsByAOP" class="com.lx.utils.AppendLogsByAOP"/>
    <!-- 切入点 -->
    <aop:config>
        <aop:pointcut id="afterMethodPoint" expression="execution(* com.lx.controllers.publics.*Controller.*(..))"/>
        <aop:aspect id = "appendLogs" ref="appendLogsByAOP">
            <aop:before method="logOutputBeforeMethod" pointcut="execution(* com.lx.controllers.publics.*Controller.*(..))"/>
            <aop:after-throwing method="logOutputAfterThrows" pointcut="execution(* com.lx.controllers.publics.*Controller.*(..))"/>
            <!-- 可以用 pointcut-ref来指代相同的pointcut-->
            <aop:after-returning method="logOutputAfterReturn" pointcut-ref="afterMethodPoint"/>
            <aop:after method="logOutputAfterMethod" pointcut-ref="afterMethodPoint"/>
        </aop:aspect>
    </aop:config>
    <!-- AOP demo end  -->
    
    
    
</beans>

 

SSM整理笔记3——配置解析