SSM框架主要配置文件及加载顺序总结

本文详细介绍了SSM框架的组成及应用,包括Spring、SpringMVC和MyBatis三个开源框架,用于web项目的开发。深入探讨了项目文件结构、配置文件编写,以及服务器加载流程,从web.xml到springMVC配置的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值