@Transactional不适用于方法级别

nev*_*ver 5 java spring transactions

我有一个关于Spring 3.2.3 @Transactional注释的问题.我的服务类看起来像这样:

@Service @Transactional
class InventoryDisclosureBO {

@Autowired InventoryDisclosureDAO inventoryDisclosureDAO;

private static final Logger log = LoggerFactory.getLogger( InventoryDisclosureBO.class);

public void processDisclosureData(InventoryDisclosureStatus data){
  validate(data);
  persist(data);
}

@Transactional(propagation = REQUIRES_NEW)
void persist(InventoryDisclosureStatus data) {
  inventoryDisclosureDAO.setAllInvalid( data.getUnit());
  inventoryDisclosureDAO.insert( data );
}

void validate(InventoryDisclosureStatus data) {
 ...
}
}
Run Code Online (Sandbox Code Playgroud)

如果我调用persist()方法,那么一切都很完美.但是如果我在课堂级别注释掉@Transactional - 事务没有开始.有人能告诉我为什么Spring只能在甲醇水平上忽略@Transactional吗?

Evg*_*eev 5

您无法从processDisclosureData()调用persist(),因为它属于同一个类,它将绕过Spring为InventoryDisclosureBO创建的事务代理.您应该从其他bean调用它以使@Transactional注释工作.当Spring向其他bean注入InventoryDisclosureBO bean的引用时,它实际上会引入对包含事务逻辑的InventoryDisclosureBOProxy的引用,例如

    class Bean2 {

      @Autowire
      private InventoryDisclosureBO idbo;   <-- Spring will inject a proxy here

      public void persist(InventoryDisclosureStatus data) {
           idbo.persist(data);     <-- now it will work via proxy
      }
...
Run Code Online (Sandbox Code Playgroud)