grails初始化bean空指针异常

时间:2013-11-25 20:43:35

标签: grails inheritance nullpointerexception javabeans initializing

我正在使用grails 2.3.1,我有两项服务。

ServiceA implements InitializingBean, SomeInterface {

    def grailsApplication
    List someList

    @Override
    void afterPropertiesSet() throws Exception {
        // initialization of someList using grailsApplication
    }

    @Override
    void methodFromSomeInterface() { }

}

和第二项服务,做的几乎相似,不同

ServiceB extends ServiceA {

    // no need for the member variables, they'll be inherited
    // def grailsApplication
    // List someList

    // should not need the afterPropertiesSet as well, it'll be inhereted
    // but that fails (same error as below), so the following change was made
    @Override
    void afterPropertiesSet() throws Exception {
        super.afterPropertiesSet();
    }

    @Override
    void methodFromSomeInterface() { }

}

现在,它抛出一个NPE

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'someController':
Initialization of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'serviceB':
Invocation of init method failed;
nested exception is java.lang.NullPointerException
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'serviceB':
Invocation of init method failed;
nested exception is java.lang.NullPointerException

我在这里做错了什么?

顺便说一下,它适用于以下代码......

ServiceA implements InitializingBean, SomeInterface {

    def grailsApplication
    List someList

    @Override
    void afterPropertiesSet() throws Exception { init() }

    protected void init() { 
        // initialization of someList using grailsApplication
    }

    @Override
    void methodFromSomeInterface() { }

}

和第二项服务

ServiceB extends ServiceA {

    // no need for the member variables, they'll be inherited
    // def grailsApplication
    // List someList

    @Override
    void afterPropertiesSet() throws Exception { init() }

    @Override
    void methodFromSomeInterface() { }

}

这个有效!但为什么不是第一个?

0 个答案:

没有答案