系列文章:spring的xml配置是如何对应注解配置的之配置介绍

从我们熟悉的web.xml开始…

1、web.xml:
  • 指定spring配置文件bean.xml(默认applicationContext.xml)
  • 指定servlet配置文件,默认[servletName]-servlet.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"
  version="3.0">

  <!-- 强制进行转码 -->
  <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>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

  <!-- 默认的spring配置文件是在WEB-INF下的applicationContext.xml -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:bean.xml
    </param-value>
  </context-param>

   <!--日志配置文件-->
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
  </context-param>

  <!-- springMVC的核心控制器 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--不指定配置文件位置,默认是servletName-servlet.xml-->
      <param-value>classpath:WEB-INF/spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!-- session配置 -->
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>

  <!-- 欢迎页面 ,物理页面-->
  <welcome-file-list>
    <welcome-file>/WEB-INF/jsp/index.html</welcome-file>
  </welcome-file-list>

  <!-- 错误页面 -->
  <!--<error-page>-->
    <!--<error-code>403</error-code>-->
    <!--<location>/WEB-INF/jsp/403.jsp</location>-->
  <!--</error-page>-->
  <!--<error-page>-->
    <!--<error-code>404</error-code>-->
    <!--<location>/WEB-INF/jsp/404.jsp</location>-->
  <!--</error-page>-->
  <!--<error-page>-->
    <!--<error-code>500</error-code>-->
    <!--<location>/WEB-INF/jsp/500.jsp</location>-->
  <!--</error-page>-->
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/jsp/error.jsp</location>
  </error-page>
</web-app>
2、指定bean.xml,默认applicationContext.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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    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/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 
<!--开启AOP-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- <bean id="ConfigBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
      <list>
         <value>classpath:jdbc.properties</value>
      </list>
  </property>
</bean> -->
<!-- 或者下面这种方式加载jdbc.properties配置文件 -->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<!-- 或者下面这种方式加载jdbc.properties配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="classpath:jdbc.properties"/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:configLocation="classpath:hibernate.cfg.xml"/>
<!-- 或者下面这种方式 -->  
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 <property name="configLocation">
      <value>classpath:hibernate.cfg.xml</value>
 </property>
 <property name="mappingDirectoryLocations">
      <list>
          <value>classpath*:/com/hbm</value>
      </list>
 </property>
</bean>

<!-- 配置hibernateTemplate Bean -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate" p:sessionFactory-ref="sessionFactory"/>

<!--事务管理配置-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 使用hibernate事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" p:dataSource-ref="dataSource"> 
    <!-- 或者 -->
    <!-- <property name="sessionFactory" ref="sessionFactory"></property> --> 
    <!-- <property name="dataSource" ref="dataSource"></property> --> 
</bean>
<!-- 使用普通事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" /> 
</bean>
    
<!-- 配置事务传播特性 -->
<tx:advice id="TransAdvice" transaction-manager="transactionManager"> 
    <tx:attributes> 
        <tx:method name="save*" propagation="REQUIRED" /> 
        <tx:method name="add*" propagation="REQUIRED" /> 
        <tx:method name="create*" propagation="REQUIRED" /> 
        <tx:method name="insert*" propagation="REQUIRED" /> 
        <tx:method name="update*" propagation="REQUIRED" /> 
        <tx:method name="merge*" propagation="REQUIRED" /> 
        <tx:method name="del*" propagation="REQUIRED" /> 
        <tx:method name="remove*" propagation="REQUIRED" /> 
        <tx:method name="put*" propagation="REQUIRED" /> 
        <tx:method name="use*" propagation="REQUIRED" /> 
        //hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到 
        <tx:method name="get*" propagation="REQUIRED" read-only="true" /> 
        <tx:method name="count*" propagation="REQUIRED" read-only="true" /> 
        <tx:method name="find*" propagation="REQUIRED" read-only="true" /> 
        <tx:method name="list*" propagation="REQUIRED" read-only="true" /> 
        <tx:method name="*" propagation="REQUIRED" /> 
        <tx:method name="*" timeout="30" /> 
    </tx:attributes> 
</tx:advice> 
<!-- 配置参与事务的类 --> 
<aop:config> 
    <aop:pointcut id="allTransServiceMethod" expression="execution(* com.dao.*.*(..))" /> 
    <aop:advisor pointcut-ref="allTransServiceMethod" 
        advice-ref="TransAdvice" /> 
</aop:config>
<!--导入DAO bean配置文件--> 
<!-- <import resource="classpath:spring/*-spring.xml" /> -->
<!-- c3p0 -->
<context:property-placeholder location="classpath:c3p0_config.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
     <!-- 指定连接数据库的驱动--> 
     <property name="driverClass" value="${jdbc.driverClassName}"/> 
     <!-- 指定连接数据库的URL--> 
     <property name="jdbcUrl" value="${jdbc.url}"/> 
     <!-- 指定连接数据库的用户名--> 
     <property name="user" value="${jdbc.username}"/> 
     <!-- 指定连接数据库的密码--> 
     <property name="password" value="${jdbc.password}"/> 
     <!-- 指定连接池中保留的最大连接数. Default:15--> 
     <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/> 
     <!-- 指定连接池中保留的最小连接数--> 
     <property name="minPoolSize" value="${jdbc.minPoolSize}"/> 
     <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3--> 
     <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/> 
     <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0--> 
     <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/> 
     <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3--> 
     <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/> 
     <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。 
       但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.
       如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0 --> 
     <property name="maxStatements" value="${jdbc.maxStatements}"/> 
     <!-- 每60秒检查所有连接池中的空闲连接.Default:0 --> 
     <property name="idleConnectionTestPeriod" value="${jdbc.idleConnectionTestPeriod}"/> 
     <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
      <property name="acquireRetryAttempts" value="30" />
      <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
      <!-- <property name="acquireIncrement" value="3" /> -->
      <property name="breakAfterAcquireFailure" value="true" />
      <property name="testConnectionOnCheckout" value="false" />
</bean>
</beans>
3、如果不指定spring mvc配置文件位置,默认使用WEB-INF/spring-servlet.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:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> 
 <beans>
    <!-- spring mvc 配置-->
    <mvc:annotation-driven/>
    <!-- 扫描类包以启动注解驱动的Bean -->
    <context:component-scan base-package="com.dao"></context:component-scan>
 </beans> 
4、hibernate.cfg.xml
<!-- 连接池hibernate配置c3p0数据源,需要再次配置数据源,c3p0才起作用 -->
<property name="hibernate.connection.provider_class">
   org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider    
</property>
<!-- 最小连接数 -->
<property name="c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="c3p0.max_size">20</property>
<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位秒 -->
<property name="c3p0.timeout">120</property>
<!-- 最大的PreparedStatement的数量 -->
<property name="c3p0.max_statements">50</property>
<property name="c3p0.automaicTestTable">Test</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒 -->
<property name="c3p0.idle_test_period">120</property>
<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
<property name="c3p0.idleConnectionTestPeriod">18000</property>
<property name="c3p0.maxIdleTime">25000</property>
<property name="c3p0.idle_test_period">120</property>
<!-- 每次都验证连接是否可用 -->
<property name="c3p0.validate">true</property>
<property name="preferredTestQuery">select * from db_test</property>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值