English 中文(简体)
圣杯:为什么这个服务课是无效的?
原标题:Grails: why would this service class be null?

鉴于这种严重简化的引渡安排:

package net.myexample.plugin

class MyExampleService {  
  Map doMunge(Map m) {
    // do stuff to  m 
    return m
  }
}

/****************************** BREAK: NEXT FILE ******************************/

package net.myexample.plugin

class MyTagLib {
  static namespace =  p 

  def myExampleService

  def tag = { attrs, body ->
    def m = doMungeAndFilter(attrs.remove( m ))

    out << g.render(template:  /template , plugin:  my-example-plugin , model: m)
  }

  Map doMungeAndFilter(def m) {
    def mm = myExampleService.doMunge(m)
    // do stuff to  m 
    return mm
  }
}

/****************************** BREAK: NEXT FILE ******************************/

package net.myexample.app

import net.myexample.plugin.MyExampleService

class MyExampleService extends net.myexample.plugin.MyExampleService {
  def doMunge(def m) {
    def mm = super.doMunge(m)
    // do more stuff to  mm 
    return mm
  }
}

/****************************** BREAK: NEXT FILE ******************************/

package net.myexample.app

import net.myexample.plugin.MyTagLib

class MyTagLib extends net.myexample.plugin.MyTagLib {
  static namespace =  a 

  def myExampleService

  def tag = { attrs, body ->
    def m = doMungeAndFilter(attrs.remove( m ))

    out << g.render(template:  /template , plugin:  my-example-plugin , model: m)
  }

  Map doMungeAndFilter(def m) {
    def mm = super.doMungeAndFilter(m)
   // do more stuff to  mm 
    return mm
  } 
}

/**
 * But we get an exception that cites that it cannot call  doMunge  on a null
 * object -- which could only be  myExampleService 
 */

为什么这个服务看起来是 null ? 当 app s taglib 上的方法调用其超级类(插件上的标签lib ), 而该方法又调用该服务上的方法时, 为什么这个服务看起来是 null ?

我所能想到的最佳理论是,这项服务实际上并没有在应用程序标签类中被即时使用,因为除了def 之外,没有明确提及它。我想,情况就是这样,因为如果我把所有服务类方法的逻辑都移到标签类方法中,它就会如预期的那样发挥作用。

(为绘制完整图片: MyExample service.doMunge 被调用到其他地方,而随后的过滤(在 MyTagLib.doMunge和Filter 中)只对标签lib 需要。 )

另选 : 如果我将 doMungeANDFilter 移动到另一个服务类别, 在插件中创建基版本并在应用程序中扩展它, 效果会很好。 我想这是一个可以接受的结论, 尽管它感觉像浮肿一样, 创建另一个服务类别来支持标签lib 。

思考 提示 错误或疏漏

最佳回答

dedef myExample Services 从子类标签中删除 。 在 Groovy 中这样的属性会编译为私有字段, 加上一个公共抓取器和设置器, 所以在超级类标签中您已经隐含了

private Object myExampleService;

public void setMyExampleService(Object svc) {
  this.myExampleService = svc;
}
// getter similar

当您再次在子类中声明 < code> myExample Service 时, 子类获得其 own 私有字段( 名称相同), 设置器被覆盖以存储在此子类字段而非超级类字段中提供的值。 Spring 呼叫设置器输入服务, 最终结果是超级类私有 < code> myExampleService 永远无法设置, 因此在试图在超级类中调用 < code> myExampleServicice.doMunge 时, 无效指针例外 。

子类可以通过继承的获取者和设置者 进入超级阶级的财产, 这样它就不需要重新宣布它。

问题回答

这只是个简单的猜测,但您是位于 / grails-app/taglib 下的标签类文件, 还是您/ src 目录中的某个位置? 我注意到我无法将服务( 自动, 至少) 注入 / grails- app 文件夹外的分类 。





相关问题
Groovy - how to exit each loop?

I m new to Grails/Groovy and am trying to find a node in a an xml file; I ve figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I ve read that ...

Eclipse Spring Builder set properties with Groovy beans

I typically use groovy to construct simple bean but the Spring IDE plugin to eclipse fails to build when I try to set a property that is generated by groovy without an explicit setter. For example, ...

How can I get this snippet to work?

I d like to port a little piece of code from Ruby to Groovy, and I m stuck at this: def given(array,closure) { closure.delegate = array closure() } given([1,2,3,4]) { findAll { it > 4} ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

热门标签