BeanCreationException,如何排查?

问题描述 投票:0回答:1

StackOverflow 社区您好,

我在 Spring Boot 应用程序的

NullPointerException
类中遇到了
ShopPromotionScheduler
问题。

我对在代码中实现调度程序还比较陌生,并且遇到了无法诊断的问题。具体来说,我收到 NullPointerException,但我不确定它的来源。

以下是相关代码片段:

@Component
public class ShopPromotionScheduler {

    @Resource
    private IShopPromotionService shopPromotionService;

    @Resource
    private IShopItemBossBusinessService shopItemBossBusinessService;

    @Resource
    private IShopAutoSaleBossBusinessService shopAutoSaleBossBusinessService;

    private Logger bizLogger = LoggerFactory.getLogger(Constants.BIZ_LOGGER);

    @PostConstruct
    public void init(){
        startScheduler();
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void checkAndUpdateShopPromo() {
        ShopPromotionQuery param = new ShopPromotionQuery();
        param.setStatus(1);
        List<ShopPromotionVo> shopPromotionList = shopPromotionService.getShopPromotionList(param).getModel();

        bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromotionList:{}", JSON.toJSONString(shopPromotionList));
        shopPromotionList.forEach(shopPromo -> {
            bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromo:{}", JSON.toJSONString(shopPromo));
            if (checkPastTime(shopPromo.getEndTime())){
                shopPromo.getShopPromotionItemVoList().forEach(itemPromo -> {
                    bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler itemPromo:{}", JSON.toJSONString(itemPromo));
                    ShopItemQuery shopItemQuery = new ShopItemQuery();
                    shopItemQuery.setEntityId(itemPromo.getEntityId());
                    shopItemQuery.setId(itemPromo.getItemId());
                    ShopItemDetailsVo shopItemDetailsVo = new ShopItemDetailsVo();
                    try {
                        shopItemDetailsVo = shopItemBossBusinessService.getShopItemDetails(shopItemQuery).getModel();
                        bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopItemDetailsVo:{}", JSON.toJSONString(shopItemDetailsVo));
                        if (shopItemDetailsVo == null)
                            throw new RuntimeException();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    ShopItemSetParam shopItemSetParam = itemSetParam(shopItemDetailsVo);
                    shopItemSetParam.setIsPromo(0);
                    ShopPromotionQuery shopPromotionParam = new ShopPromotionQuery();
                    shopPromotionParam.setId(itemPromo.getShopPromotionId());
                    bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromotionParam:{}", JSON.toJSONString(shopPromotionParam));
                    List<ShopPromotionVo> shopPromotion = shopPromotionService.getShopPromotionList(shopPromotionParam).getModel();
                    bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromotion:{}", JSON.toJSONString(shopPromotion));
                    boolean isUpdate = true;
                    for (ShopPromotionVo shopPromotionVo : shopPromotion) {
                        bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromotionVo:{}", JSON.toJSONString(shopPromotionVo));
                        if (!(Objects.equals(shopPromotionVo.getId(), itemPromo.getShopPromotionId()))) {
                            if (shopPromotionVo.getStatus() == 1){
                                isUpdate = false;
                            }
                        }
                    }
                    if (isUpdate) {
                        bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler isUpdate:{}", JSON.toJSONString(isUpdate));
                        Result<String> updateResult = shopItemBossBusinessService.updateShopItem(shopItemSetParam);
                        bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler updateResult:{}", JSON.toJSONString(updateResult));
                    }

                });
                ShopPromotionParam shopPromotionParam = new ShopPromotionParam();
                shopPromotionParam.setEntityId(shopPromotionParam.getEntityId());
                shopPromotionParam.setId(shopPromotionParam.getId());
                shopPromotionParam.setStatus(2);
                bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler shopPromotionParam:{}", JSON.toJSONString(shopPromotionParam));
                Result<Integer> result = shopPromotionService.updateShopPromotion(shopPromotionParam);
                bizLogger.info("checkAndUpdateShopPromo shopPromoScheduler result:{}", JSON.toJSONString(result));
            };
        });
    }

    private void startScheduler(){
        checkAndUpdateShopPromo();
    }

    private boolean checkPastTime(Long timestamp){
        Duration duration = Duration.between(Instant.now(), Instant.ofEpochMilli(timestamp));
        return duration.isNegative() || duration.isZero();
    }

    private ShopItemSetParam itemSetParam(ShopItemDetailsVo shopItemDetailsVo) throws Exception {
        ShopAutoOnOffSaleQuery shopAutoOnOffSaleQuery = new ShopAutoOnOffSaleQuery();
        shopAutoOnOffSaleQuery.setEntityId(shopItemDetailsVo.getEntityId());
        shopAutoOnOffSaleQuery.setItemId(shopItemDetailsVo.getId());
        ShopItemSetParam shopItemSetParam = new ShopItemSetParam();
        ShopAutoOnOffSaleVo shopAutoOnOffSaleVo = new ShopAutoOnOffSaleVo();
        try {
            shopAutoSaleBossBusinessService.getShopAutoOnOffSale(shopAutoOnOffSaleQuery).getModel();
        } catch (Exception e){
            throw new Exception();
        }
        ShopAutoOnOffSaleParam shopAutoOnOffSaleParam = new ShopAutoOnOffSaleParam();
        shopAutoOnOffSaleParam.setId(shopAutoOnOffSaleVo.getId());
        shopAutoOnOffSaleParam.setEntityId(shopAutoOnOffSaleVo.getEntityId());
        shopAutoOnOffSaleParam.setIsAutoSale(shopAutoOnOffSaleVo.getIsAutoSale());
        shopAutoOnOffSaleParam.setIsWeekday(shopAutoOnOffSaleVo.getIsWeekday());
        shopAutoOnOffSaleParam.setItemId(shopAutoOnOffSaleVo.getItemId());

        shopItemSetParam.setId(shopItemDetailsVo.getId());
        shopItemSetParam.setOpUserId(shopItemDetailsVo.getOpUserId());
        shopItemSetParam.setEntityId(shopItemDetailsVo.getEntityId());
        shopItemSetParam.setShopAutoOnOffSaleParamList(shopAutoOnOffSaleParam);
        shopItemSetParam.setItemType(shopItemDetailsVo.getItemType());
        shopItemSetParam.setSubType(shopItemDetailsVo.getSubType());
        shopItemSetParam.setCategoryId(shopItemDetailsVo.getCategoryId());
        shopItemSetParam.setItemName(shopItemDetailsVo.getItemName());
        shopItemSetParam.setItemSubName(shopItemDetailsVo.getItemSubName());
        shopItemSetParam.setItemCode(shopItemDetailsVo.getItemCode());
        shopItemSetParam.setStartNum(shopItemDetailsVo.getStartNum());
        shopItemSetParam.setIsShelves(shopItemDetailsVo.getIsShelves());
        shopItemSetParam.setIsTakeout(shopItemDetailsVo.getIsTakeout());
        shopItemSetParam.setIsDiscount(shopItemDetailsVo.getIsDiscount());
        shopItemSetParam.setIsRecommend(shopItemDetailsVo.getIsRecommend());
        shopItemSetParam.setIsSku(shopItemDetailsVo.getIsSku());
        shopItemSetParam.setDetails(shopItemDetailsVo.getDetails());
        shopItemSetParam.setDualUnit(shopItemDetailsVo.getDualUnit());
        shopItemSetParam.setBarCode(shopItemDetailsVo.getBarCode());
        shopItemSetParam.setSortCode(shopItemDetailsVo.getSortCode());
        shopItemSetParam.setMemo(shopItemDetailsVo.getMemo());

        return shopItemSetParam;
    }
}

这是日志错误;

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2024-04-01 17:47:50.361 ERROR o.s.boot.SpringApplication.reportFailure:771 Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopPromotionScheduler': Invocation of init method failed; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:407)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1623)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
        at com.platform.merchant.StartSpringBootApplication.main(StartSpringBootApplication.java:34)
Caused by: java.lang.NullPointerException: null
        at com.platform.merchant.component.ShopPromotionScheduler.itemSetParam(ShopPromotionScheduler.java:122)
        at com.platform.merchant.component.ShopPromotionScheduler.lambda$null$0(ShopPromotionScheduler.java:72)
        at java.util.ArrayList.forEach(ArrayList.java:1259)
        at com.platform.merchant.component.ShopPromotionScheduler.lambda$checkAndUpdateShopPromo$1(ShopPromotionScheduler.java:58)
        at java.util.ArrayList.forEach(ArrayList.java:1259)
        at com.platform.merchant.component.ShopPromotionScheduler.checkAndUpdateShopPromo(ShopPromotionScheduler.java:55)
        at com.platform.merchant.component.ShopPromotionScheduler.startScheduler(ShopPromotionScheduler.java:107)
        at com.platform.merchant.component.ShopPromotionScheduler.init(ShopPromotionScheduler.java:45)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
        at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)

我正在尝试实现一种使用调度程序创建代码的方法。然而,当我实现它时,出现了这样的错误。我不太确定我哪里出了问题,因为我尝试了几种不同的调度程序方法,但没有一个起作用

PS:已经解决了,忘了加上@EnableScheduling 哈哈

java spring-boot microservices javabeans
1个回答
0
投票

我不认为你的问题与 Spring 调度程序有关。堆栈跟踪表明访问 ArrayList 元素时出现问题。

Caused by: java.lang.NullPointerException: null
        at com.platform.merchant.component.ShopPromotionScheduler.itemSetParam(ShopPromotionScheduler.java:122)
        at com.platform.merchant.component.ShopPromotionScheduler.lambda$null$0(ShopPromotionScheduler.java:72)
        at java.util.ArrayList.forEach(ArrayList.java:1259) 

这可能是由于 ShopPromotionScheduler 类中的 checkAndUpdateShopPromo 方法中的 forEach 循环中访问空对象引起的。您可以检查 shopPromotionService 是否返回一个初始化的对象。

List<ShopPromotionVo> shopPromotionList = shopPromotionService.getShopPromotionList(param).getModel();
© www.soinside.com 2019 - 2024. All rights reserved.