SSM架構步驟和配置

1 web.xml步驟:

    1:Spring配置文件信息
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value->
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

    2:SpringMVC配置文件信息
        <servlet>
            <servlet-name>crm_11</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:context-dispathcer.xml</param-value>
            </init-param>
            <!-- 啓用即加載 -->
            <load-on-startup>1</load-on-startup>
        </servlet>

    3:encodeing編碼
        <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>

    4:請求後綴攔截,do結尾的請求是用於後臺管理系統,htm結尾的請求,用於前端界面請求
        <servlet-mapping>
            <servlet-name>crm_11</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>

    5:404、500
        <error-page>
            <error-code>404</error-code>
            <!-- 頁面跳轉地址 -->
            <location>/404.do</location>
        </error-page>
        <error-page>
            <error-code>500</error-code>
            <!-- 頁面跳轉地址 -->
            <location>/500.do</location>
        </error-page>

    6:session有效期
        <session-config>
            <session-timeout>120</session-timeout><!-- 默認分鐘 -->
        </session-config>
`

2 applicationContext.xml配置:

步驟:`
    1:引入jdbc配置文件
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config.properties</value>
                    <value>classpath:jdbc.properties</value>
                </list>
            </property>
        </bean>
`

    2:將Controller的註解排除掉
        <context:component-scan base-package="com.**">
            <!--將Controller的註解排除掉 -->
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        </context:component-scan>
        <task:annotation-driven/> 
    3:創建jdbc數據源
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="driver" />
          <property name="url" value="url" />
          <property name="username" value="username" />
          <property name="password" value="password" />
          <!-- 連接池啓動時的初始值 -->
          <property name="initialSize" value="30" />
          <!-- 連接池的最大值 -->
          <property name="maxActive" value="1000" />
          <!-- 最大空閒值.當經過一個高峯時間後,連接池可以慢慢將已經用不到的連接慢慢釋放一部分,一直減少到maxIdle爲止 -->
          <property name="maxIdle" value="50" />
          <!-- 最小空閒值.當空閒的連接數少於閥值時,連接池就會預申請去一些連接,以免洪峯來時來不及申請 -->
          <property name="minIdle" value="30" />
          <!--#給出一條簡單的sql語句進行驗證-->
          <property name="validationQuery" value="select 1" />
          <!--#在取出連接時進行有效驗證-->
          <property name="testOnBorrow" value="true" />
          <property name="removeAbandonedTimeout" value="120" />
          <property name="removeAbandoned" value="true" />
          <!-- #運行判斷連接超時任務的時間間隔,單位爲毫秒,默認爲-1,即不執行任務。 -->
          <property name="timeBetweenEvictionRunsMillis" value="3600000" />
          <!-- #連接的超時時間,默認爲半小時。 -->
          <property name="minEvictableIdleTimeMillis" value="3600000" />  
        </bean>

    4:事務管理
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <tx:annotation-driven transaction-manager="transactionManager" />
    5:以下兩個類爲配置表服務
        <bean id="cfgSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
            <property name="dataSource" ref="dataSource" />
        </bean>

        <bean id="cfgDataAccess" class="com.**.publics.dao.DataAccessImpl" >
            <!-- 通過構造函數注入 -->
            <constructor-arg index="0" ref="cfgSqlSessionFactory" />
        </bean>

        <bean id="springUtil" class="com.**.publics.util.ContextUtil" depends-on="cfgDataAccess">
            <property name="configPath" value="config.properties,jdbc.properties" />
        </bean>
`

3 context-dispatcher.xml配置:

步驟:
    1:使用註解的包,包括子集
        <context:component-scan base-package="com.**.controller" >  
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
        </context:component-scan>
    2:訪問國際化信息
        <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basename" value="messages" />
        </bean>
    3:視圖解析器
        <context:component-scan base-package="com.**.publics" use-default-filters="false">
            <!--只掃描控制器。  -->
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>

        <!--配置視圖解析器,方便頁面返回  -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    4:interceptors
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/**/*.do" />
                <bean class="com.**.publics.interceptorfiles.BackgroundLoginInterceptor"></bean>
            </mvc:interceptor>
        </mvc:interceptors>
    5:處理請求映射,啓用註解驅動 ,作用於我們的請求域上面
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/plain;charset=UTF-8</value>
                            <value>text/html;charset=UTF-8</value>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </mvc:message-converters>
        </mvc:annotation-driven>

4 mybatis-config.xml配置:

<settings>
    <!-- 延遲加載、懶加載,作用於屬性和對象域上面 -->
    <setting name="lazyLoadingEnabled" value="true" />
    <!-- 是否立即初始化屬性或者對象域 -->
    <setting name="aggressiveLazyLoading" value="false" />
</settings>

<plugins>
     <plugin interceptor="com.**.publics.util.BaseOperInterceptor">
        <property name="databaseType" value="mysql" />
    </plugin>
</plugins>

<mappers>
    <mapper resource="commonsqlmappings/CommonMapper.xml" />
</mappers>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章