简体   繁体   English

Groovy中Utility类的访问方法

[英]Accessing method of a Utility class in groovy

Is it possible to access a method of a utility class from a static method in a service? 是否可以从服务中的static method访问utility classstatic method I have a utility class that contains methods. 我有一个包含方法的实用程序类。 I have tested that these methods are accessible within Service and Controller . 我已经测试了这些方法可以在ServiceController中访问。

Now, I created a service that contains a static method. 现在,我创建了一个包含静态方法的服务。 Within that method I am trying to call the method of a particular utility class, but it seemed that it doesn't work. 在该方法中,我尝试调用特定实用程序类的方法,但似乎不起作用。

Is calling a method of a utility class inside a service's static method, possible? 是否可以在服务的静态方法中调用实用工具类的方法?

thanks. 谢谢。

It´s possible to call a Static method of another class from the Service layer. 可以从Service层调用另一个类的Static方法。 Maybe you are facing some other problem that has nothing to do with the main action of calling that static method from the Utils. 也许您遇到了其他问题,这些问题与从Utils调用该静态方法的主要动作无关。

You only need to import the class. 您只需要导入该类。

Here you havo some info about the service layer of Grails. 在这里,您可以了解有关Grails服务层的一些信息。

http://grails.org/doc/latest/guide/services.html http://grails.org/doc/latest/guide/services.html

I would recommend you to make this checklist: 我建议您制作此清单:

  • Check if the Utils class its well constructed. 检查Utils类是否构造正确。
  • Check if the Service layer has the import correctly writen. 检查服务层是否正确写入了导入。
  • Check the way you call to this static method of the Util class. 检查调用此Util类的静态方法的方式。
  • Check if your Service layer has this structure: 检查您的服务层是否具有以下结构:

     import org.springframework.transaction.annotation.Transactional import com.route.utils.MyUtilClass class BookService { //Example @Transactional(readOnly = true) def listBooks() { MyUtilClass.myStaticMethod() Book.list() } } 

It could be helpful to have the Grails version you are working with and a code example of the service layer where you are calling to that method. 拥有正在使用的Grails版本以及在其中调用该方法的服务层的代码示例可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM