2

我正在使用 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() { }

}

这行得通!但为什么不是第一个?

4

0 回答 0