@PostContruct在服务器启动期间提供NullPointerException

时间:2018-07-03 14:13:00

标签: java spring server javabeans

在服务器启动的第一行中,我得到空指针异常

public class DefaultAreaPostalCodeService extends AbstractBusinessService implements AreaPostalCodeService {

        private Map<String,List<PostalCodeData>> suburbMap;

        @PostConstruct
        @Transactional
        public void initialize() {
            List<AreaPostalCodeModel> postalCodes = areaPostalCodeDao.getAllAreaPostalCodes();
            populateSuburbMap(postalCodes);
        }
    }

<bean id="areaPostalCodeService"
          class="za.co.testro.core.address.impl.DefaultAreaPostalCodeService" parent="abstractBusinessService">
        <property name="areaPostalCodeDao" ref="areaPostalCodeDao" />
    </bean>

我只是想在服务器启动时填充郊区地图,以便以后使用。

错误日志-

  

创建名称为“ areaPostalCodeService”的bean时出错:调用   初始化方法失败;嵌套的异常是java.lang.NullPointerException   WARN [localhost-startStop-1] [CloseAwareApplicationContext]异常   上下文初始化期间遇到-取消刷新   尝试:org.springframework.beans.factory.BeanCreationException:   创建名称为“ areaPostalCodeService”的bean时出错:调用   初始化方法失败;嵌套的异常是java.lang.NullPointerException   对JVM进行Ping操作需要10秒钟。错误   [localhost-startStop-1] [HybrisContextFactory]初始化错误   全局应用程序上下文!   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ areaPostalCodeService”的bean:init的调用   方法失败;嵌套的异常是java.lang.NullPointerException           在org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136)处   〜[spring-beans-4.3.3.REL

编辑1

将代码添加到 afterProperties()后,我仍然收到Null Pointer Exception。

  

INFO [localhost-startStop-1] [ListMergeDirectiveBeanPostProcessor]   后处理ListMergeDirective   Bean上的[promotionActionResultRaoExtractorListMergeDirective]   [cartRAOProviderExtractors]警告[localhost-startStop-1]   [CloseAwareApplicationContext]异常   encoujava.lang.NullPointerException WARN [localhost-startStop-1]   [CloseAwareApplicationContext]上下文期间遇到异常   初始化-取消刷新尝试:   org.springframework.beans.factory.BeanCreationException:错误   创建在类路径中定义的名称为“ areaPostalCodeService”的bean   资源[testcore-spring.xml]:调用init方法失败;   嵌套异常是java.lang.NullPointerException错误   [localhost-startStop-1] [HybrisContextFactory]初始化错误   全局应用程序上下文!   org.springframework.beans.factory.BeanCreationException:错误   创建在类路径中定义的名称为“ areaPostalCodeService”的bean   资源[testcore-spring.xml]:调用init方法失败; NE   sted异常是java.lang.NullPointerException           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)   〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)处   〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)   〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]在上下文期间被插入   初始化-取消刷新尝试:   org.springframework.beans.factory.BeanCreationException:错误   创建在类路径中定义的名称为“ areaPostalCodeService”的bean   资源[testcore-spring.xml]:调用init方法失败;   嵌套异常是

编辑2

使用应用程序上下文调用Bean后,仍然会得到Null指针异常。

Registry.getApplicationContext().getBean("areaPostalCodeDao", AreaPostalCodeDao.class).getAllAreaPostalCodes()

错误日志-

  

INFO [localhost-startStop-1] [ListMergeDirectiveBeanPostProcessor]   后处理ListMergeDirective   Bean上的[promotionActionResultRaoExtractorListMergeDirective]   [cartRAOProviderExtractors]警告[localhost-startStop-1]   [CloseAwareApplicationContext]上下文期间遇到异常   初始化-取消刷新尝试:   org.springframework.beans.factory.BeanCreationException:错误   创建在类路径中定义的名称为“ areaPostalCodeService”的bean   资源[testcore-spring.xml]:调用init方法失败;   嵌套异常是java.lang.NullPointerException WARN   [localhost-startStop-1] [CloseAwareApplicationContext]异常   上下文初始化期间遇到-取消刷新   尝试:org.springframework.beans.factory.BeanCreationException:   创建类中定义的名称为“ areaPostalCodeService”的bean时出错   路径资源[testcore-spring.xml]:初始化方法的调用失败;   嵌套异常是java.lang.NullPointerException错误   [localhost-startStop-1] [HybrisContextFactory]初始化错误   全局应用程序上下文!   org.springframework.beans.factory.BeanCreationException:错误   创建在类路径中定义的名称为“ areaPostalCodeService”的bean   资源[testcore-spring.xml]:调用init方法失败; NE   sted异常是java.lang.NullPointerException           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)   〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]           在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)处   〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]

     

由于:org.springframework.beans.FatalBeanException:上下文   由于以下原因,无法正确创建hybris Global Context Factory:   创建类中定义的名称为“ areaPostalCodeService”的bean时出错   路径资源[testcore-spring.xml]:初始化方法的调用失败;   嵌套的异常是java.lang.NullPointerException           在de.hybris.platform.core.HybrisContextFactory.build(HybrisContextFactory.java:314)

2 个答案:

答案 0 :(得分:2)

具有这种行为是非常正常的,因为@PostConstruct方法将在将areaPostalCodeDao bean注入到areaPostalCodeDao属性中被调用之前(此时),该方法会被调用。

有四种方法可以在bean初始化时运行某些代码,但是每种方法都可以在特定步骤中成功。这些方法是:在构造函数中添加代码,添加@PostConstruct方法,实现afterPropertiesSet()接口的InitializingBean或添加init-method

@PostConstruct将在构造后被调用,然后依次是afterPropertiesSetinit-method

因此,在您的情况下,您应该执行上述操作:

import org.springframework.beans.factory.InitializingBean;  

      public class DefaultAreaPostalCodeService extends AbstractBusinessService implements AreaPostalCodeService,InitializingBean {

           private Map<String,List<PostalCodeData>> suburbMap;

           @Override
           @Transactional
           public void afterPropertiesSet() {
               List<AreaPostalCodeModel> postalCodes = areaPostalCodeDao.getAllAreaPostalCodes();
               populateSuburbMap(postalCodes);
           }
       }

答案 1 :(得分:0)

一个可能的解决方案,也许不是专门针对这种情况,但可以帮助其他人,是从视图中调用代码,第一眼就调用它。代码类似于:

在 XHTML 中

<f:metadata>
     <f:viewAction action="#{myUI.myVoid()}"/>
</f:metadata>