实施Grails服务时存在继承问题 [英] Issues with inheritance when implementing Grails services

查看:100
本文介绍了实施Grails服务时存在继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,在实施Grails服务时不推荐使用继承方法吗?我经历了一种简单的服务专业化模式认识,即所有人都能以透明的方式工作,但我开始在spring / grails引擎盖下针对事务管理陷入困境。问题发生在专用类的方法调用继承类(两个具体服务本身)的方法时:

  @Transactional 
public class MammalService {

public mammalMethod(){
}

}

@Transactional
public class DogService扩展MammalService {

public dogMethod(){
mammmalMethod()
}

}

当从特定的方法调用继承的方法时, org.springframework.transaction.support.GrailsTransactionTemplate() code>构造函数被一个null transactionManager 参数触发(通过spring / grails事务AOP),这会导致一些 NullPointerException 另外。

有没有人在服务中使用过这种方法?我错过了什么?



PS:奇怪的是,我试着改变 @ grails.transaction.Transactional 注释 @ org.springframework.transaction.annotation.Transactional NullPointerException 停止发生。 (尽管如此,它并没有指出一个很好的解决方案,因为其他副作用开始发生在我的池管理中)。



UPDATE1:在调试时,我可以看到TWO在我的专门服务中有同名TransactionManager的变量(检查具体超类时不会发生的事情)。



p>

我在在Grails服务中重复的transactionManager属性

解决方案


通过从@Transaction中移除专门的服务类和
方法,只保留它们继承的服务类和方法。




  @Transactional 
public class MammalService {

@Transactional(readonly = true)
public mammalMethod(){
}

}

//不用@Transactional注释 - 依赖于超类声明
public class DogService扩展MammalService {

//不用@Transactional注释 - 依靠超类事务声明
public dogMethod(){
mammmalMethod()
}

}

看来,对于专门的服务类,事务例程尝试重新注入 transactionManager 属性,导致两个具有相同名称并且其中一个为空的属性。另外,注释覆盖方法会引发StackOverflowException。


So, is it not recommended using an inheritance-approach when implementing Grails services? I went through a simple service specialization pattern understanding that all would work in a transparent way, but I started to go into trouble regarding transaction management under the spring/grails hood. Issues happen when a method from the specialized class calls a method from the inherited class (both concrete services themselves):

@Transactional
public class MammalService {

  public mammalMethod() {
  }

}

@Transactional
public class DogService extends MammalService {

   public dogMethod() {
       mammmalMethod()
   }

}

It comes that when the inherited method is called from the specialized one, org.springframework.transaction.support.GrailsTransactionTemplate() constructor is fired (by the spring/grails transaction AOP) with a null transactionManager argument, which causes some NullPointerException moreover.

Has anyone used such approach with services? Am I missing something?

PS: Curiously, I tried changing the @grails.transaction.Transactional annotation by the @org.springframework.transaction.annotation.Transactional and the NullPointerException ceased from happening. (Nevertheless, it didn't point to a nice solution, since other side effects started to happen with my pool management).

UPDATE1: While debugging, I can see TWO variables with the same name transactionManager inside my specialized service (something that doesn't happen when inspecting the concrete superclass).

I'm opening a more specific issue at Duplicated transactionManager property in Grails service

解决方案

Solved by removing @Transaction from specialized service classes and methods, keeping them only on the inherited service class and its methods.

@Transactional
public class MammalService {

  @Transactional(readonly=true)
  public mammalMethod() {
  }

}

//Don't annotate with @Transactional - rely on the super class declaration
public class DogService extends MammalService {

   //Don't annotate with @Transactional - rely on the super class transactional declaration
   public dogMethod() {
       mammmalMethod()
   }

}

It seems that, for specialized service classes, the transaction routines try to re-inject the transactionManager attribute, resulting in two attributes with the same name and one of them null. Also, annotating overrided methods raises an StackOverflowException.

这篇关于实施Grails服务时存在继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆