MongoTemplate在执行第二次时返回NullPoint异常

时间:2017-06-08 15:27:41

标签: java spring mongodb

我想从mongo获取数据并且我使用spring数据为mongo创建了一个bean,但问题是它运行并且仅在第一次成功提供数据但是当我调用相同的模板时我第二次得到 NullPointException

这是我的代码::

try{
            mongoTemplate = (MongoTemplate) ApplicationContextProvider.getBean("mongoTemplate");
            UsageMapping usageMapping = mongoTemplate.findById(salesOrg, UsageMapping.class);
            usagemapAsObject = usageMapping.getUsages();
            String usageMappingJSON = JsonUtil.getJsonFromObject(usagemapAsObject); //mapper.writeValueAsString(usagemapAsObject);
            if(InsightUtil.isEmpty(usageMappingJSON)){
                LOGGER.warn("No value found for "
                        + EnvironmentConstants.USAGE_SETUP_MAPPING);
                return new HashMap();
            }
            usageMap = JsonUtil.getObjectFromJson(usageMappingJSON, Map.class);

        }catch (Exception e){
            LOGGER.warn("Exception in Mongo:: "+ EnvironmentConstants.USAGE_SETUP_MAPPING);
        }

这是我的ApplicationContextProvider Class ::

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext appContext;

    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException {
        appContext = context;
    }

    public static Object getBean(String name) {
        return  appContext.getBean(name);
    }
}

我在这里遗漏了什么。 提前谢谢你:)

0 个答案:

没有答案