为什么Groovy闭包不能访问注入的类成员?

时间:2016-12-01 16:14:01

标签: groovy closures guice

我们在项目中使用Groovy和Guice,我遇到了以下错误:

  

groovy.lang.MissingPropertyException:没有这样的属性:myService for class:com.me.api.services.SomeService $$ EnhancerByGuice $$ 536bdaec

有点想弄清楚,但这是因为我引用了一个私有类成员,它被注入,在一个闭包内。任何人都可以解释为什么会发生这种情况吗?

此外,还有更好的方法吗?

以下是该类的内容摘录:

import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MyService extends BaseService<Thing> {

    @Inject
    private ThingDao thingDao

    @Inject
    private OtherService<Thing> otherService

    @Override
    List<Thing> findAll() {
        List<Thing> things = this.dao.findAll()

        things.each { 
            //Note: This doesn't work!
            otherService.doSomething()
        }

        things
    }

我要么必须使用标准for循环,要么不使用注入的成员,这会导致代码重复。

0 个答案:

没有答案