java - 关于spring中整合Hibernate为什么加入aop事务就可以被管理,aop做了什么事情
大家讲道理
大家讲道理 2017-04-18 10:47:37
0
0
557

在没有加入aop之前的配置如下

<context:component-scan base-package="org.csp.elec"></context:component-scan>
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.pass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolsize}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolsize}"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:org/csp/elec/domain/*.hbm.xml"></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

     
     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
    <tx:attributes>    
        <tx:method name="add*" propagation="REQUIRED" />    
        <tx:method name="save*" propagation="REQUIRED" />    
        <tx:method name="delete*" propagation="REQUIRED" />    
        <tx:method name="modify*" propagation="REQUIRED" />    
        <tx:method name="update*" propagation="REQUIRED" />    
    </tx:attributes>    
    </tx:advice>  

这么配置,只要使用hibernateTemplete 操作数据库,那么就会抛出 org.springframework.dao.InvalidDataAccessApiUsageException 大概就是说 你只能读,不能写。

在使用断点跟踪进入到hibernateTemplete时,

好了,我的问题是,当加入aop的时候,到底发生了什么,为什么就可以从当前线程获取到session了

下面是加入aop

<context:component-scan base-package="org.csp.elec"></context:component-scan>
    <context:property-placeholder location="classpath:db.properties"/>
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.pass}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolsize}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolsize}"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:org/csp/elec/domain/*.hbm.xml"></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

     
     <tx:advice id="txAdvice" transaction-manager="transactionManager">    
    <tx:attributes>    
        <tx:method name="add*" propagation="REQUIRED" />    
        <tx:method name="save*" propagation="REQUIRED" />    
        <tx:method name="delete*" propagation="REQUIRED" />    
        <tx:method name="modify*" propagation="REQUIRED" />    
        <tx:method name="update*" propagation="REQUIRED" />    
    </tx:attributes>    
    </tx:advice>   

     <aop:config>
         <aop:pointcut expression="execution(* org.csp.elec.dao.*.*(..))" id="pointcut"/>
         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
     </aop:config>
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!