(转)spring配置数据源和hibernate的配置整合成一个文件

spring配置文件applicationContext-common.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:tx="http://www.springframework.org/schema/tx"  
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
        
     <!-- c3po连接池 -->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">      
        <property name="driverClass">      
            <value>com.mysql.jdbc.Driver</value>      
        </property>      
        <property name="jdbcUrl">                 
            <value>jdbc:mysql://127.0.0.1/sshwithdbcp</value>      
         </property>      
        <property name="user">      
            <value>root</value>      
        </property>      
        <property name="password">      
            <value>root</value>      
        </property>      
    </bean>      
        
        
     <!-- dbcp连接池 -->   
     <!--    
     <bean id="dataSource"  
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
        <property name="driverClassName"  
            value="com.mysql.jdbc.Driver">   
        </property>   
        <property name="url"  
            value="jdbc:mysql://127.0.0.1/sshwithdbcp">   
        </property>   
        <property name="username" value="root"></property>   
        <property name="password" value="root"></property>   
    </bean>   
     -->   
       
<!-- 配置sessionFactory -->   
<!--   
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
        <property name="configLocation">   
            <value>classpath:hibernate.cfg.xml</value>   
        </property>   
    </bean>      
-->   
  
<!-- session Factory -->   
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
        <property name="dataSource">   
            <ref bean="dataSource" />   
        </property>   
        <property name="hibernateProperties">   
            <props>   
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>   
                <prop key="hibernate.show_sql">true</prop>    
            </props>   
        </property>   
        <property name="mappingLocations">   
            <list>   
               <value>classpath:com/max/model/*.hbm.xml</value>   
            </list>   
        </property>   
    </bean>   
  
<!-- 配置事务管理器 -->     
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
        <property name="sessionFactory">   
            <ref local="sessionFactory"/>   
        </property>   
    </bean>   
       
    <!-- 配置事务特性 -->          
    <tx:advice id="txAdvice" transaction-manager="transactionManager">   
        <tx:attributes>   
            <tx:method name="add*" propagation="REQUIRED"/>   
            <tx:method name="del*" propagation="REQUIRED"/>   
            <tx:method name="update*" propagation="REQUIRED"/>   
            <tx:method name="deploy*" propagation="REQUIRED"/>   
            <tx:method name="submit*" propagation="REQUIRED"/>   
            <tx:method name="*" read-only="true"/>   
        </tx:attributes>   
    </tx:advice>   
       
    <!-- 配置那些类的方法进行事务管理 -->   
    <aop:config>   
        <aop:pointcut id="allManagerMethod" expression="execution (* com.max.managers.*.*(..))"/>   
        <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>   
    </aop:config>         
</beans>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>   
<web-app version="2.4"    
    xmlns="http://java.sun.com/xml/ns/j2ee"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
       
       
  
  <welcome-file-list>   
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>   
     
  <context-param>   
        <param-name>contextConfigLocation</param-name>   
        <param-value>   
            /WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml   
        </param-value>   
           
    </context-param>   
     
  <listener>   
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
   </listener>   
     
  <servlet>   
    <servlet-name>action</servlet-name>   
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   
    <init-param>   
      <param-name>config</param-name>   
      <param-value>/WEB-INF/struts-config.xml</param-value>   
    </init-param>   
    <init-param>   
      <param-name>debug</param-name>   
      <param-value>3</param-value>   
    </init-param>   
    <init-param>   
      <param-name>detail</param-name>   
      <param-value>3</param-value>   
    </init-param>   
    <load-on-startup>0</load-on-startup>   
  </servlet>   
  <servlet-mapping>   
    <servlet-name>action</servlet-name>   
    <url-pattern>*.do</url-pattern>   
  </servlet-mapping>   
    
</web-app>

相关推荐