@事务性注释不代理我的类



我有三个类,AccountController、AccountManager和AccountDao(它们按顺序注入,即controller<-manager<-dao)。当我在accountDao上添加@Transactional注释时,我有一个DB会话,一切都很好。当我把@Transactional放在管理器上时,它不起作用。我得到:

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

我看到accountManager由于某种原因(在调试时)没有使用代理进行包装。你知道为什么会这样吗?

在我的应用程序上下文的XML中,我有:

<context:component-scan base-package="com.mypackage" use-default-filters="true">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<tx:annotation-driven transaction-manager="hibernateTransactionManager" proxy-target-class="true"/>

在AccountManager上,我添加了以下注释:

@Repository
@Transactional
public class AccountManager  { ... }

更奇怪的是,AccountManager中注入了一个"DeviceManager",带有与AccountManager相同的注释,但DeviceManager确实被代理了!它完全是事务性的。

我使用的是org.springframework.transaction.annation.Transactional注释。

我在AccountManager 中没有任何最终方法

编辑以回应@Enigo的回答:

  1. AccountManager不是存储库,因此应使用@Service对其进行注释,而AccountDao应使用@Repository进行注释
  2. AccountManager应该实现一个接口,并且该接口应该被注入Controller类
  3. 确保您在AccountManager中调用的方法是公共的,并重写接口中的方法声明

最新更新