天天看點

spring架構DAO搭配注解配置時注意事項

如果報錯:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread

1、注意先後順序

context:annotation-config/>      
<context:component-scan base-package=""/>      
<tx:annotation-driven />      

2、注意base-package的掃描要獨立分開,不要全路徑的掃描。

     service部分

      <context:annotation-config/>           <context:component-scan base-package="com.saper.tutorial.service"/>           <context:component-scan base-package="com.saper.tutorial.dao"/>            <tx:annotation-driven transaction-manager="transactionManager"/>

     controller部分             <context:component-scan base-package="com.saper.tutorial.controller"/>

       紅色部分不能簡寫成如下:

              <context:component-scan base-package="com.saper.tutorial"/>

繼續閱讀