SSM项目整合,配置文件的加载流程及原理

1.tomcat启动,先加载web.xml文件的配置

服务器启动,要加载spring框架容器,springmvc框架容器,mybatis框架由spring整合,所以这里不用加载该框架容器

web.xml


<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_2_5.xsd" id="WebApp_ID" version="2.5">




	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:applicationContext.xmlparam-value>
	context-param>

<listener>
	<listener-class>org.springframework.web.context.ontextLoaderListenerlistener-class>
listener>




<servlet>
	<servlet-name>DispatcherServletservlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
	
	
	<init-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:spring-mvc.xmlparam-value>
    init-param>
    
	
	<load-on-startup>1load-on-startup>
servlet>


<servlet-mapping>
        <servlet-name>DispatcherServletservlet-name>
        
        <url-pattern>/url-pattern>
servlet-mapping>


	


    <filter>
        <filter-name>CharacterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

2.spring的配置文件加载

在web.xml中配置了加载spring的配置文件 applicationContext.xml 会被加载,spring整合mybatis框架,可以将mybatis框架中的所有相关配置全部放到spring配置文件中来加载.(也可以单独给mybatis写配置文件)

applicationContext.xml


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">







<context:properties-placeholder  location="classpath:jdbc.properties">


<bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
	 <property name="driverClass" value="${jdbc.driver}">property>
     <property name="jdbcUrl" value="${jdbc.url}">property>
     <property name="user" value="${jdbc.username}">property>
     <property name="password" value="${jdbc.password}">property>
bean>




<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>




<tx:annotaion-driver />






<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	
	<property name="dataSource" ref="dataSource">property>
	 <property name="typeAliasesPackage" value="实体类所在包,用来起别名"/>
    <property name="configuration">
     <bean class="org.apache.ibatis.session.Configuration">
            
            <property name="mapUnderscoreToCamelCase" value="true"/>
        bean>
    property>
    
 <property name="plugins">
        <array>
         <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                 <props>
                        <prop key="helperDialect">数据库方言类型(mysql|oracle)prop>
                 props>
                property>
            bean>
        array>
 property>
bean>






    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.mapper">property>
    bean>



3.spring-mvc的配置文件加载


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.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">


<context:component-scan base-package="controller包名"/>


    <mvc:annotation-driven>mvc:annotation-driven>


    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/">property>
        <property name="suffix" value=".jsp">property>
    bean>



<mvc:default-servlet-handler>mvc:default-servlet-handler>

你可能感兴趣的:(java)