【问题标题】:What are the implications of using types for grails services?对 Grails 服务使用类型有什么影响?
【发布时间】:2012-12-10 12:25:31
【问题描述】:

我发现自己在使用 groovy 编程时避免使用 def 关键字,因为我喜欢使用类型的明确性。我想知道使用类型与使用 def 注入服务有什么含义?我想它对模拟注入的可测试性有潜在的影响。还要别的吗?

我看到the documentation 中提到的两种方法。

人们现在认为最佳做法是什么?

【问题讨论】:

    标签: java grails groovy


    【解决方案1】:

    当使用显式类型声明时(对于 2.0 之前的 grails),曾经存在重新加载注入其他服务的服务的问题。我在 grails jira 中没有看到任何关于此的最新信息,因此这可能已在更新的版本中得到修复。

    【讨论】:

    • 这实际上似乎仍然是一个问题。我正在使用 grails 2.1,我注意到从一开始就定义它们时重新加载 spring 注入服务会更好。
    【解决方案2】:

    def 是类型名称的替代品。在变量定义中,它用于表示您不关心类型。 Is there any difference between declaring a variable with 'def' and declaring it with a known type?

    def num = 1
    Integer number = 2
    
    assert num.class == Integer
    assert number.class == Integer
    

    我更喜欢使用 def 进行依赖注入,我认为这是 Grails 中最常见的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      相关资源
      最近更新 更多