SSM框架主要配置文件及加載順序總結

        SSM框架是由Spring、SpringMVC、MyBatis三個開源框架組成的,在本人當下的理解中主要用於開發web項目。要使用SSM框架首先需要導入相應的包,接着開始編寫配置文件。

/**
	項目文件結構
	-src
	  -main
	    -java
		  -com
			-leo
			  {-entity,-mapper,-service,-controller,-test,-utils}
		-resources
		  -mvc
		    SpringMvc.xml
		  -mybatis
			-mapper
			-properties
			  db.properties
			  sqlMapConfig.xml
			-spring
			  applicationContext.xml
		-webapp
		  -resources
		    {-css,-fontsm,-i,-img,-js,-jsp}
		  -WEB_INF
		    web.xml
		  index.jsp
**/

       編寫配置文件的過程稍微有點複雜,但是配置好之後以後就可以重複利用。主要的配置文件有:web.xml(自動生成,在WEB-INF下),applicationContext.xml(spring配置文件,文件名可能不同,以自己的文件名爲準),springMVC.xml,Mapper.xml文件(操作數據庫的配置文件)。

執行一個使用SSM框架搭建的javaWEB工程時,服務器加載順序如下:

1.讀取web.xml配置文件,web.xml配置文件中主要的配置有:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="4.0" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee">
<!-- 根據指定路徑加載spring配置文件 -->
<!-- 1.關聯到spring主配置文件的路徑地址 classpath根目錄-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
<!--注意:此時服務器會去加載spring配置文件,暫停本段配置之後的代碼執行;-->

2.加載spring配置文件,applicationContext.xml中的主要配置如下:

<!--只要導入了相應的包,這一段配置不變-->
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd
">
    <!-- 1.開啓spring註解驅動 -->
    <context:component-scan base-package="com.leo"/>
    
    <!-- 2.讀取數據庫properties -->
    <context:property-placeholder location="classpath:mybatis/properties/db.properties"/>
    
    <!--3. 配置數據庫連接池 c3p0 -->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <property value="${jdbc.driver}" name="driverClass"/>
        <!-- 配置Jdbc的Url -->
        <property value="${jdbc.url}" name="jdbcUrl"/>
        <!-- 配置用戶名 -->
        <property value="${jdbc.username}" name="user"/>
        <!-- 密碼 -->
        <property value="${jdbc.password}" name="password"/>
    </bean>
    
    <!-- 4.配置事務管理器 -->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <!-- 事務管理數據庫連接池 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
​
    <!--5. 開啓事務的註解驅動 @Transactional -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <!-- 6.spring管理mybatis配置文件 -->
    <!--生成mapper接口的代理類(簡而言之就是,dao層中只定義接口,spring使用了這個配置之後就會自動根據mapper.xml文件中的語句生成dao層的實現類區操作數據庫-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!--dataSource屬性指定要用的連接池 ref映射的是id=dataSource的bean標籤,讀取連接數據庫信息-->
        <property name="dataSource" ref="dataSource"/>
        <!--configLocation屬性指定mybatis的核心配置文件,管理mybatis配置文件-->
        <property value="classpath:mybatis/sqlMapConfig.xml" name="configLocation"/>
        <!-- 所有配置的mapper文件,mybatis的實體類配置文件所有的sql映射文件 -->
        <property value="classpath*:mybatis/mapper/*.xml" name="mapperLocations"/>
    </bean>
    
    <!-- 7.spring管理mybatis映射接口和sql映射文件之間關聯關係 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property value="com.leo.mapper" name="basePackage"/>
        <property value="sqlSessionFactory" name="sqlSessionFactoryBeanName"/>
    </bean>
    
    <!-- 8.AOP配置,執行日誌文件等操作-->
    <aop:config>
        <aop:pointcut expression="execution(* com.leo.service.*.*(..))" id="tp" />
        <aop:advisor advice-ref="ta" pointcut-ref="tp" />
    </aop:config>
</beans>
    <!--至此spring配置文件加載完成,服務器回到web.xml文件中進行後續的執行:-->

3.繼續讀取web.xml配置文件(從上到下加載):

        加載監聽器;加載過濾器;加載前端控制器(即servlet或者springMVC),前端控制器中的1配置決定了前端控制器是否在容器啓動時就加載,若其值大於等於0則在容器啓動時加載,小於零或不設置時則不在容器啓動時加載。若要在容器(服務器Tomcat)啓動時就加載前端控制器,則此時暫停web.xml的加載,先去加載springMVC。

 <!-- 2.配置監聽器加載spring的配置文件 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
​
    <!-- 3.配置過濾器,過濾編碼格式 爲utf-8,只支持post提交 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--異步支持 -->
        <async-supported>true</async-supported>
        <!-- 初始化 編碼格式爲UTF-8,只適用於post提交-->
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
​
    <!-- 4.加載前端控制器(servlet或者springmvc) -->
    <!-- 配置DispatcherServlet,來加載springmvc的配置 -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mvc/SpringMvc.xml</param-value>
        </init-param>
        <!-- 啓動優先加載 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4.加載springMVC配置文件。

        在web.xml配置文件加載前端控制器時根據其中的相關配置加載springMVC配置。加載控制器(第二步中未加載),自動實例化相關類以便之後直接使用不用new(controller和servse),加載視圖解析器,完成springMVC配置文件的加載。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 1.開啓controller層中spring註解驅動  打開 Component Controller Service Reposity -->
    <context:component-scan base-package="com.leo" />
​
    <!-- 2.開啓springmvc特有的註解驅動  打開 @RequestMapping  @RequestParam  -->
    <mvc:annotation-driven>
        <mvc:message-converters><!--response.getWriter().Write()  @responsebody -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" /><!--解決@responsebody響應中文亂碼-->
            </bean>
            <bean                     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
​
    <!--3.放行靜態文件-->
    <mvc:default-servlet-handler/>
    <!--DispatcherServlet-->
    <!-- 靜態資源路徑配置  location表示路徑地址 -->
    <mvc:resources mapping="/resources/css/**" location="/resources/css/"/>
    <mvc:resources mapping="/resources/js/**" location="/resources/js/"/>
    <mvc:resources mapping="/resources/jsp/**" location="/resources/jsp/"/>
​
    <!-- 4.加載視圖解析器(處理jsp頁面所在的前綴和後綴) -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前綴,確定當前訪問的頁面路徑地址 -->
        <property name="prefix" value="/resources/jsp/">
        </property>
        <!-- 後綴,確定要訪問的文件類型 -->
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章