Caused by: java.lang.NullPointerException at org.springframework.beans.factory.support.DefaultLis

在搭建完Spring环境之后运行第一个demo的时候 就碰到这个问题,折腾了一天了,到现在才解决,记录一下自己的失误。
解决办法就是在添加user Library的时候 不能勾选System Library,否则后面运行demo的时候会提示空指针异常,这个真的是很郁闷。
这里写图片描述

下面内容为转载的,就是看了别人的博客才发现自己的失误的,学习大神的分析思路:

Caused by: java.lang.NullPointerException
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:106)
   ... 7 more

一看源代码,是这样的:

static{  
    ClassLoader cl =DefaultListableBeanFactory.class.getClassLoader();  
    try{  
        javaxInjectProviderClass = cl.loadClass("javax.inject.Provider");//Line 106  
    }  
    catch(ClassNotFoundException ex){  
        // JSR-330 API not available - Provider interface simply not supported then.  
    }  
}  

源代码的问题在于:

ClassLoader cl =DefaultListableBeanFactory.class.getClassLoader();返回空!

找了找网上:发现别人也遇到过:

http://stackoverflow.com/questions/2272354/why-do-i-get-a-nullpointerexception-when-initializing-spring

再了解了一下getClassLoader() 的原理:

http://blog.chenlb.com/2009/06/java-classloader-architecture.html

原来是这个DefaultListableBeanFactory通过BootStrap直接加载,getClassLoader()的时候返回null

JDK上也有说明:

“public ClassLoader ‍getClassLoader()返回该类的类加载器。有些实现可能使用 null 来表示引导类加载器。如果该类由引导类加载器加载,则此方法在这类实现中将返回 null。 “

细细想来,原来是这里错了:勾选了System library(added to the boot class path),DefaultListableBeanFactory被当成系统Jar通过BootStrap直接加载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值