SSM框架初解(二)配置文件

配置文件

web.xml

在这里插入图片描述
该配置文件是创建web项目时生成的,默认路径:main/WEB-INF/web.xml

<?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"
    id="WebApp_ID" version="3.0">
  <!--项目在管理其中的显示的名字-->
  <display-name>ssm-crud</display-name>
 
  <!--项目启动时 启动的页面-->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 启动Spring容器 -->
  <!-- needed for ContextLoaderListener -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- SpringMVC的前端控制器,拦截所有请求 -->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--
        <init-param>
            <param-name>contextConfigLoction</param-name>
            <param-value>location</param-value>
        </init-param>
     -->
     <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for.. -->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
  <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceRequestEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>forceResponseEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- Rest风格的URI 将页面普通的post请求转为指定的delete或者put请求 -->
  <filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <servlet-name>/*</servlet-name>
  </filter-mapping>
  
</web-app>

标签含义:
在这里插入图片描述

aplicationContext.xml

文件一般放在resources/aplicationContext.xml。

<?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    "> <!-- 开启注解扫描 --> 
    <context:component-scan base-package="cn.dtw"></context:component-scan> 
    <!-- 读取配置文件 --> 
    <context:property-placeholder location="classpath:jdbc.properties"/> 
    <!-- 数据源 使用c3p0连接,需要jar包支持 --> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
        <property name="jdbcUrl" value="${jdbcUrl}"></property> 
        <property name="driverClass" value="${driverClass}"></property> 
        <property name="user" value="${user}"></property> 
        <property name="password" value="${password}"></property> 
    </bean> 
    <!-- 集成Mybatis(sqlSessionFactory )--> 
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"></property> 
        <property name="typeAliasesPackage" value="cn.dtw"></property> 
    </bean> 
    <!-- 映射帮助类 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> 
        <property name="basePackage" value="cn.dtw.dao"></property> 
    </bean> 
    <!-- 事务管理器 --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource"></property> 
    </bean> 
    <!-- 事务注解 --> 
    <tx:annotation-driven/> 
    <!-- mvc注解驱动 --> 
    <mvc:annotation-driven/> 
    <!-- 处理静态资源 --> 
    <mvc:default-servlet-handler/> 
    <!-- 注册异常处理 --> 
    <bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
        <property name="defaultErrorView" value="error"></property> 
        <property name="exceptionAttribute" value="exp"></property> 
        <property name="exceptionMappings"> 
            <props> 
                <prop key="cn.dtw.exception.MyException">myError</prop> 
            </props> 
        </property> 
    </bean> 
    <!-- 支持上传文件 --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <!-- 指定默认的编码格式,默认是ISO-8859-1 --> 
        <property name="defaultEncoding" value="utf-8"></property> 
        <!-- 上传文件的最大值,单位是字节 --> 
        <property name="maxUploadSize" value="10000000"></property> 
        <!-- 上传文件的临时文件夹 --> 
        <property name="uploadTempDir" value="tempDir"></property> 
    </bean> 
    <!-- 视图处理 --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/"></property> 
        <property name="suffix" value=".jsp"></property> 
    </bean> 
    <!-- 使用动态代理技术 --> <!--    
        <bean id="logAspect" class="cn.dtw.aop.LogAspect"></bean>
        <aop:config>
            切入点
            <aop:pointcut expression="execution(public java.lang.Integer addCar(cn.dtw.entity.Car))" id="pointcut"/>        
            <aop:pointcut expression="execution(* cn.dtw.service.*.*(..))" id="pointcut"/>
            切入面
            <aop:aspect ref="logAspect">
                <aop:before method="before" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config> 
    --> 
    <bean id="logAspect" class="cn.dtw.aop.LogAspect"></bean> 
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 
</beans>

spring-mvc.xml

设置响应静态文件,视图解析器,拦截器、请求编码格式,多为前端的配置

<?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

命名方式与对应的dao层的类名一致,一般情况下一个映射文件对应一个dao层的类。

<?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"/>
</pre>
</beans>

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

jdbc.properties

主要是以键值对的形式配置连接的数据库驱动、地址、用户名、访问密码,以及连接池【此配置也可忽略】的相关配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.22.2.2:3306/databases
jdbc.username=localhost
jdbc.password=localhost

c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=400
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.maxConnectionAge=600

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  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值