原文:https://blog.csdn.net/yijiemamin/article/details/51156189#

这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿出来一起分享一下,希望有不足的地方大家批评指正~~~

首先   这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结.

spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml(也常常叫做applicationContext.xml),第一个不用说,每个web项目都会有的也是关联整个项目的配置.第二个文件spring-mvc.xml是springmvc的一些相关配置,第三个是mybatis的相关配置.

项目中还会用到两个资源属性文件jdbc.properties和log4j.properties.一个是关于jdbc的配置,提取出来方便以后的修改.另一个是日志文件的配置.

以上是我这篇文章中所要讲的内容,比较简单,也很容易懂.希望大牛不要鄙视~~接下来进入正题:

一  web.xml

关于这个配置文件我以前一直都是朦朦胧胧的状态,刚好借着这次整合框架的机会将它了解清楚.在下面的代码中我对每一个标签都进行了详细的注释,大家一看就懂,主要理解<servlet>中的配置,因为其中配置了前端控制器,在SSM框架中,前端控制器起着最主要的作用.下面贴上代码

<?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"> <context-param> <!--全局范围内环境参数初始化-->
<param-name>contextConfigLocation</param-name> <!--参数名称-->
<param-value>classpath:spring-mybatis.xml</param-value> <!--参数取值-->
</context-param> <!--以下配置的加载顺序:先 ServletContext >> context-param >> listener >> filter >> servlet >> spring--> <!---------------------------------------------------过滤器配置------------------------------------------------------>
<!--例:编码过滤器-->
<filter> <!-- 用来声明filter的相关设定,过滤器可以截取和修改一个Servlet或JSP页面的请求或从一个Servlet或JSP页面发出的响应-->
<filter-name>encodingFilter</filter-name> <!--指定filter的名字-->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!--定义filter的类的名称-->
<async-supported>true</async-supported> <!--设置是否启用异步支持-->
<init-param><!--用来定义参数,若在Servlet可以使用下列方法来获得:String param_name=getServletContext().getInitParamter("param-name里面的值");-->
<param-name>encoding</param-name> <!--参数名称-->
<param-value>UTF-8</param-value> <!--参数值-->
</init-param>
</filter>
<filter-mapping><!--用来定义filter所对应的URL-->
<filter-name>encodingFilter</filter-name> <!--指定对应filter的名字-->
<url-pattern>/*</url-pattern> <!--指定filter所对应的URL-->
</filter-mapping> <!---------------------------------------------------监听器配置------------------------------------------------------>
<!--例:spring监听器-->
<listener> <!--用来设定Listener接口-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!--定义Listener的类名称-->
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!---------------------------------------------------servlet配置------------------------------------------------------>
<servlet> <!--用来声明一个servlet的数据 -->
<servlet-name>SpringMVC</servlet-name> <!--指定servlet的名称-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--指定servlet的类名称,这里配置了前端控制器-->
<init-param><!--用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数 -->
<param-name>contextConfigLocation</param-name> <!--参数名称-->
<param-value>classpath:spring-mvc.xml</param-value> <!--参数值-->
</init-param>
<load-on-startup>1</load-on-startup><!--当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.-->
<async-supported>true</async-supported><!--设置是否启用异步支持-->
</servlet>
<servlet-mapping><!--用来定义servlet所对应的URL-->
<servlet-name>SpringMVC</servlet-name> <!--指定servlet的名称-->
<url-pattern>/</url-pattern> <!--指定servlet所对应的URL-->
</servlet-mapping> <!-----------------------------------------------会话超时配置(单位为分钟)------------------------------------------------->
<session-config><!--如果某个会话在一定时间未被访问,则服务器可以扔掉以节约内存-->
<session-timeout>120</session-timeout>
</session-config>
<!---------------------------------------------------MIME类型配置 ------------------------------------------------------>
<mime-mapping><!--设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开-->
<extension>*.ppt</extension> <!--扩展名名称-->
<mime-type>application/mspowerpoint</mime-type> <!--MIME格式-->
</mime-mapping>
<!---------------------------------------------------欢迎页面配置 ------------------------------------------------------>
<welcome-file-list><!--定义首页列单.-->
<welcome-file>/index.jsp</welcome-file> <!--用来指定首页文件名称.我们可以用<welcome-file>指定几个首页,而服务器会依照设定的顺序来找首页.-->
<welcome-file>/index.html</welcome-file>
</welcome-file-list>
<!---------------------------------------------------配置错误页面------------------------------------------------------>
<error-page> <!--将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径.-->
<error-code>404</error-code> <!--HTTP Error code,例如: 404、403-->
<location>error.html</location> <!--用来设置发生错误或异常时要显示的页面-->
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type><!--设置可能会发生的java异常类型,例如:java.lang.Exception-->
<location>ExceptionError.html</location> <!--用来设置发生错误或异常时要显示的页面-->
</error-page>
</web-app>

二  spring-mvc.xml

需要实现基本功能的配置 
1 配置 <mvc:annotation-driven/>
2 配置 <context:component-scan base-package="com.springmvc.controller"/> //配置controller的注入
3 配置视图解析器

<mvc:annotation-driven/>   相当于注册了DefaultAnnotationHandlerMapping(映射器)和AnnotationMethodHandlerAdapter(适配器)两个bean.即解决了@Controller注解的使用前提配置。

context:component-scan  对指定的包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。

<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1、配置映射器与适配器 -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 2、视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<span style="white-space:pre"> </span><!-- 前缀和后缀 -->
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 3、自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器  -->
<context:component-scan base-package="com.rhzh.controller"/>
</beans>

三  spring-mybatis.xml(也常常叫做applicationContext.xml)

需要实现基本功能的配置 
1 配置 <context:component-scan base-package="com.rhzh"/> //自动扫描,将标注Spring注解的类自动转化Bean,同时完成Bean的注入
2 加载数据资源属性文件
3 配置数据源  三种数据源的配置方式 http://blog.csdn.net/yangyz_love/article/details/8199207
4 配置sessionfactory
5 装配Dao接口
6 声明式事务管理 
7 注解事务切面

<?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--1 自动扫描 将标注Spring注解的类自动转化Bean-->
<context:component-scan base-package="com.rhzh" />
<!--2 加载数据资源属性文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<span style="white-space:pre"><!-- 3 配置数据源 --></span>
<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="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 4   配置sessionfactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/rhzh/mapping/*.xml"></property>
</bean>
<!-- 5 装配dao接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.rhzh.dao" /> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 6、声明式事务管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
 <!-- 7、注解事务切面 --><span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"> <tx:annotation-driven transaction-manager="transactionManager"/>

下面是需要引入的两个资源属性文件:

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/ecdatabase?characterEncoding=utf-8
username=root
password=admin
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

log4j.properties

#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n #文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n

SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释(转)的更多相关文章

  1. 0927-转载:SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这篇文章暂时只对框架中所要用到的配置文件进行解释说明,而且是针对注解形式的,框架运转的具体流程过两天再进行总结. spring+springmvc+mybatis框架中用到了三个XML配置文件:web ...

  2. SSM:spring+springmvc+mybatis框架中的XML配置文件功能详细解释

    这几天一直在整合SSM框架,虽然网上有很多已经整合好的,但是对于里面的配置文件并没有进行过多的说明,很多人知其然不知其所以然,经过几天的搜索和整理,今天总算对其中的XML配置文件有了一定的了解,所以拿 ...

  3. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  4. SSM(Spring +SpringMVC + Mybatis)框架搭建

    SSM(Spring +SpringMVC + Mybatis)框架的搭建 最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作 参考 ...

  5. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee.com/niceyoo/jeenotes-ssm 2. 概述 在写代码之前我们先了解一下 ...

  6. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  7. java web,从零开始,一步一步配置ssm(Spring+SpringMVC+MyBatis)框架

    1.安装JDK: 安装之后要配置环境变量,在系统变量里: 新建变量名JAVA_HOME,变量值C:\Program Files\Java\jdk1.8.0_77: 新建变量名CLASSPATH,变量值 ...

  8. 使用intellij idea搭建MAVEN+SSM(Spring+SpringMVC+MyBatis)框架

    基本概念 使用SSM(Spring,SpringMVC和Mybatis) 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod  ...

  9. SSM Spring SpringMVC Mybatis框架整合Java配置完整版

    以前用着SSH都是老师给配好的,自己直接改就可以.但是公司主流还是SSM,就自己研究了一下Java版本的配置.网上大多是基于xnl的配置,但是越往后越新的项目都开始基于JavaConfig配置了,这也 ...

随机推荐

  1. [转]CENTOS 使用RSYNC+INOTIFY实现文件实时自动同步

    FROM : http://www.qiansw.com/centos-rsync-inotify-file-sync.html 生产环境中的两台web服务器,有个目录需要完全一样.使用rsync和i ...

  2. Understanding Linux CPU stats

    Your Linux server is running slow, so you follow standard procedure and run top. You see the CPU met ...

  3. 关于MAX30100心率的编程

    MAX30100是能够读取心率.血氧的传感器,通信方式是通过IIC进行通信. 其工作原理是通过红外led灯照射,能够得到心率的ADC值. 图为MAX30100的寄存器. 可以分为五类,状态寄存器.FI ...

  4. JS中三目运算符和if else的区别,你弄得明白吗

    今天写了一个图片轮播的小demo,用到了判断   先试了一下if else,代码如下:   if(n >= count-1){ n =0; }else{ n ++; } 随后代码写完了,准备优化 ...

  5. 必须记住的 30 类 CSS 选择器

    大概大家读知道`id`,`class`以及`descendant`选择器,并且整体都在使用它们,那么你正在错误拥有更大级别的灵活性的选择方式.这篇文章里面提到的大部分选择器都是在CSS3标准下的,所以 ...

  6. 不用IDE写C#的Hello World

    用Visual Studio等IDE写C#的Hello World非常简单,但脱离了IDE你能不能打印出Hello World呢?这不是说工作时脱离IDE,而是学习一下CLR的执行模型. Hello ...

  7. 添加PMD插件扫描潜在的bug

    上一节使用checkstyle来规范你的项目主要解决了代码编码规范问题,比如缩进换行等.这次继续代码健康工具类PMD. 什么是PMD PMD真的不像checkstyle这样的东西所见即所得啊,去官网找 ...

  8. Log4net的不能产生Log文件的问题

    [问题] 用如下的步骤应用了Log4Net: 建立了一个公用的项目, 在里面引入了Log4net的Nuget package. 在公用的项目中建立了一个类,加上了Log4net的attribute. ...

  9. Redis实战总结-配置、持久化、复制

    Redis的配置主要放置在redis.conf,可以通过修改配置文件实现Redis许多特性,比如复制,持久化,集群等. redis.conf部分配置详解 # 启动redis,显示加载配置redis.c ...

  10. POI的一些配置

    引用:http://apps.hi.baidu.com/share/detail/17249059 POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWork ...