Grails依赖注入问题

时间:2010-10-24 22:04:54

标签: grails groovy dependency-injection

在使用Grails中的Services进行依赖注入时遇到问题。

class ExampleService{

    def example2Service
    def example3Service

    def method1(){
       def result = example2Service.method2()
    }

}

class ExampleService{
    def example3Service

    def method2(){

       def result = example3Service.method3()
       return result 
    }

}

class Example3Service{

    def method3(){
        return true
    }

}

基本上在Example2Service中,我在Example3Service中调用method3时遇到Null Pointer Exception。

我将不胜感激,任何人都可以帮助我解决这个问题

感谢

1 个答案:

答案 0 :(得分:1)

需要初始化依赖注入。 (这同样适用于其他类型的运行时元编程,例如使用save()validate()方法扩充Domain类。)

时将初始化Grails应用程序
  • grails run-app命令
  • 运行
  • 在部署到Web服务器后运行
  • grails test-app命令运行(仅限集成测试; 单元测试不会触发初始化)。

时,初始化所涉及的类
  • 执行单个Groovy文件(即使用groovygroovyshgroovyConsole
  • 或执行单元测试时。

以下作为集成测试应该有效:

class Test2ServiceTests extends GroovyTestCase {
    def test2Service

    void testMethod2() {
        assert test2Service.method2() == true
    }
}
相关问题