hibernate3和spring整合的一些方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 引入prperties配置文件 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <!-- 引入sessionFactory LocalSessionFactoryBean 既满足了hibernate的特点,也满足了spring容器的特点 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>

    <bean id="sessionFactory2"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingDirectoryLocations">
            <list>
                <!-- 
                    spring容器会去该包及子包下搜索所有的映射文件
                 -->
                <value>com/itheima11/spring/hibernate/domain</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
    </bean>
    
    <!-- 
        引入dao和service层
     -->
    <bean id="classesDao" class="com.itheima11.spring.hibernate.dao.impl.ClassesDaoImpl">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    <bean id="classesService" class="com.itheima11.spring.hibernate.service.impl.ClassesServiceImpl">
        <property name="classesDao">
            <ref bean="classesDao"/>
        </property>
    </bean>
    
    <!-- 
        事务管理器
     -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 
        进行aop的配置
     -->
     <aop:config>
         <aop:pointcut 
             expression="execution(* com.itheima11.spring.hibernate.service.impl.*.*(..))" 
             id="perform"/>
         <aop:advisor advice-ref="tx" pointcut-ref="perform"/>
     </aop:config>
     
     <import resource="applicationContext-callback.xml"/>
</beans>

 

posted on 2015-09-20 14:21  freedom's_blog  阅读(376)  评论(0编辑  收藏  举报

导航