ssm框架中的配置文件

http://blog.csdn.net/yijiemamin/article/details/51156189整合修改

spring+springmvc+mybatis框架中用到了三个XML配置文件:web.xml,spring-mvc.xml,spring-mybatis.xml

项目中还会用到两个资源属性文件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">


    <!-- 以下配置的加载顺序:ServletContext >> context-param >> listener >> filter >> 
        servlet >> spring -->
    <!-- ps:创建:监听器 >> 过滤器 >>Servlet -->

    <!--******************************************************全局范围内环境参数配置****************************************************** -->

    <!-- 全局范围内环境参数初始化 -->
    <context-param>
        <!-- 参数名称 -->
        <param-name>contextConfigLocation</param-name>
        <!-- 参数值 -->
        <param-value>classpath:spring-mybatis.xml</param-value>
    </context-param>


    <!--******************************************************监听器配置****************************************************** -->

    <!-- e.g.spring监听器 -->
    <!-- 用来设定LIstener接口 -->
    <listener>
        <!--定义Listener的类名称 -->
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 防止spring内存溢出的监听器 -->
    <listener>
        <!--定义Listener的类名称 -->
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>


    <!--******************************************************过滤器配置****************************************************** -->

    <!-- e.g.编码过滤器 -->
    <!-- 用来声明filter的相关设定,过滤器可以截取和修改一个servlet或jsp页面的请求或从一个servlet或jsp页面发出的响应 -->
    <filter>
        <!-- 指定filter的名字 -->
        <filter-name>encodingFilter</filter-name>
        <!-- 定义filter的类的名称 -->
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置是否启用异步支持 -->
        <async-supported>true</async-supported>
        <!-- 用来定义参数,若在servlet可以使用下列方法来获取:String param_name = getServletContext().getInitParamter("param_name里面的值"); -->
        <init-param>
            <!-- 参数名称 -->
            <param-name>encoding</param-name>
            <!-- 参数值 -->
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>

    <!-- 用来定义filter所对应的URL -->
    <filter-mapping>
        <!-- 指定对应filter的名字 -->
        <filter-name>encodingFilter</filter-name>
        <!-- 指定filter所对应的URL 要拦截的URL -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--******************************************************servlet配置****************************************************** -->

    <!-- e.g.spring 前端控制器 -->
    <!-- 用来声明一个servlet的数据 -->
    <servlet>
        <!-- 指定servlet的名称 -->
        <servlet-name>SpringMVC</servlet-name>
        <!-- 指定servlet的类的名称,这里配置了前端控制器 -->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 用来定义参数,可有多个init-param, 在servlet类中通过getInitParamenter(String name)方法访问初始化参数 -->
        <init-param>
            <!-- 参数名称 -->
            <param-name>contextConfigLocation</param-name>
            <!-- 参数值 -->
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- 当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet -->
        <load-on-startup>1</load-on-startup>
        <!-- 设置是否启用异步支持 -->
        <async-supported>true</async-supported>
        <!-- 文件上传配置 -->
        <multipart-config>
            <!-- 文件路径 -->
            <location></location>
            <!-- 文件的最大大小,单位为字节 -->
            <max-file-size>5242880</max-file-size>
            <!-- 请求的最大大小,单位为字节 -->
            <max-request-size>10485760</max-request-size>
            <!--文件大小阈值,当大于这个阈值时将写入到磁盘,否则在内存中。默认值为0 -->
            <file-size-threshold>0</file-size-threshold>
        </multipart-config>
    </servlet>
    <!-- 用来定义servlet所对应的URL -->
    <servlet-mapping>
        <!-- 指定servlet的名称 -->
        <servlet-name>SpringMVC</servlet-name>
        <!-- 指定servlet所对应的URL -->
        <url-pattern>/*</url-pattern>
    </servlet-mapping>


    <!--******************************************************会话超时配置(单位为分钟)****************************************************** -->

    <!-- 如果某个会话在一定的时间未被访问,则服务器可以扔掉以节约资源 -->
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>


    <!--******************************************************MIME类型配置****************************************************** -->

    <!-- 设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开 -->
    <mime-mapping>
        <!-- 扩展名名称 -->
        <extension>*.ppt</extension>
        <!-- MIME格式 -->
        <mime-type>application/mspowerpoint</mime-type>
    </mime-mapping>


    <!--******************************************************欢迎页面配置****************************************************** -->

    <!-- 定义首页列单 -->
    <welcome-file-list>
        <!-- 用来指定首页文件名称可以用<welcome-file>指定几个首页,而服务器会依照顺序来找首页 -->
        <welcome-file>/index.jsp</welcome-file>
        <welcome-file>/index.html</welcome-file>
    </welcome-file-list>


    <!--******************************************************错误页面配置****************************************************** -->

    <!-- 将错误代码(Error Code)或异常(Exception)的种类对应到web应用资源路径 -->
    <error-page>
        <!-- HTTP Error code, e.g.404 500 -->
        <error-code>404</error-code>
        <!-- 用来设置发生错误或者异常时要显示的页面 -->
        <location>/error.html</location>
    </error-page>
    <error-page>
        <!-- 设置可能会发生异常的java异常类型,例如:java.lang.Exception -->
        <exception-type>java.lang.Exception</exception-type>
        <!-- 用来设置发生错误或者异常时要显示的页面 -->
        <location>/ExceptionError.html</location>
    </error-page>

</web-app>

二,spring-mvc.xml
需要实现基本功能的配置

  1. mvc :annotation-driven
  2. context:component-scan
  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.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  
        http://www.springframework.org/schema/mvc  
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">



    <!--******************************************************映射器与适配器配置****************************************************** -->
    <mvc:annotation-driven></mvc:annotation-driven>


    <!--******************************************************试图解析器配置****************************************************** -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/"></property>
        <!-- 后缀 -->
        <property name="suffix" value=".jsp"></property>
    </bean>


    <!--******************************************************试图解析器配置****************************************************** -->
    <!-- 自动扫描包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package=""></context:component-scan>


    <!--******************************************************注入事务****************************************************** -->
    <!-- 配置事务 -->
    <tx:annotation-driven transaction-manager="transactionManager" />


    <!--******************************************************json配置****************************************************** -->
    <!-- 配置使用json -->
    <bean
        class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonHttpMessageConverter" />
            </list>
        </property>
    </bean>
    <!-- 配置使用json视图解析 -->
    <bean id="jsonHttpMessageConverter"
        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>


    <!--******************************************************拦截器配置****************************************************** -->
    <!-- 拦截器可以实现多个 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 注意路径是** 参考Ant的规范 -->
            <mvc:mapping path="/**" />
            <!-- 不拦截的路径 -->
            <mvc:exclude-mapping path="" />
            <!-- 拦截器具体实现类 -->
            <bean class=""></bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

三,spring-mybatis.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: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
     ">

    <!-- 切面配置 -->
     <aop:aspectj-autoproxy />
    <!-- c3p0配置 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driver}" />
        <property name="jdbcUrl" value="${url}" />
        <property name="user" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 关键配置 -->
        <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
        <property name="initialPoolSize" value="3"></property>
        <!--连接池中保留的最小连接数。Default: 2 -->
        <property name="minPoolSize" value="2"></property>
        <!--连接池中保留的最大连接数。Default: 15 -->
        <property name="maxPoolSize" value="15"></property>
        <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
        <property name="acquireIncrement" value="3"></property>
        <!-- 性能配置 -->
        <!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 
            0 -->
        <property name="maxStatements" value="8"></property>
        <!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 
            0 -->
        <property name="maxStatementsPerConnection" value="5"></property>
        <!--最大空闲时间,1800秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
        <property name="maxIdleTime" value="1800"></property>
    </bean>

    <!-- 装配dao接口 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="mapper" />
    </bean>

    <!-- 声明事务管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

四,资源属性文件
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  

以上是常用的配置

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值