Java Spring实例化Bean时出错
当我尝试在eclipse之外运行应用程序时,出现以下错误: 创建名为“naLdap”的bean时出错:bean的实例化失败;嵌套的异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.ldap.core.LdapTemplate]:构造函数引发异常;嵌套异常是java.lang.NullPointerException 搜索所有帖子,我只找到使用Spring身份验证的示例。在本例中,我将根据区域搜索三个不同的Ldap实例。如有任何帮助/见解,将不胜感激 配置如下:Java Spring实例化Bean时出错,java,spring,Java,Spring,当我尝试在eclipse之外运行应用程序时,出现以下错误: 创建名为“naLdap”的bean时出错:bean的实例化失败;嵌套的异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.ldap.core.LdapTemplate]:构造函数引发异常;嵌套异常是java.lang.NullPointerException 搜索所有帖子,我只找到使用Spring身份验证的示例。在本
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source
url="ldap://corporate.root.corp:389"
base="dc=corporate, dc=root, dc=corp"
username="xyz"
password="xyz"
referral="follow"
id="na-context" />
<ldap:context-source
url="ldap://europe.root.corp:389"
base="DC=europe,DC=root,DC=corp"
username="xyz"
password="xyz"
referral="follow"
id="europe-context" />
<ldap:context-source
url="ldap://asia-pac.root.corp:389"
base="DC=asia-pac,DC=root,DC=corp"
username="xyz"
password="xyz"
referral="follow"
id="asia-context" />
<ldap:ldap-template id="naLdap" context-source-ref="na-context" />
<ldap:ldap-template id="asiaLdap" context-source-ref="asia-context" />
<ldap:ldap-template id="europeLdap" context-source-ref="europe-context" />
<bean id="personRepo" class="com.test.users.repo.PersonRepoImpl">
<property name="naTemplate" ref="naLdap" />
<property name="asiaTemplate" ref="asiaLdap" />
<property name="europeTemplate" ref="europeLdap" />
</bean>
</beans>
完整堆栈跟踪:
Error creating bean with name 'naLdap': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'naLdap': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.users.LoadUsersFromAD.getAllUsersToExamine(LoadUsersFromAD.java:330)
at com.test.users.LoadUsersFromAD.run(LoadUsersFromAD.java:102)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
... 15 more
Caused by: java.lang.NullPointerException
at org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper.isAtLeast30(DefaultObjectDirectoryMapper.java:80)
at org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper.<init>(DefaultObjectDirectoryMapper.java:71)
at org.springframework.ldap.core.LdapTemplate.<init>(LdapTemplate.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 17 more
创建名为“naLdap”的bean时出错:bean实例化失败;嵌套的异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.ldap.core.LdapTemplate]:构造函数引发异常;嵌套异常是java.lang.NullPointerException
org.springframework.beans.factory.BeanCreationException:创建名为“naLdap”的bean时出错:bean实例化失败;嵌套的异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.ldap.core.LdapTemplate]:构造函数引发异常;嵌套异常是java.lang.NullPointerException
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1076)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:700)
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
在com.test.users.LoadUsersFromAD.GetAllUsersToTest(LoadUsersFromAD.java:330)上
位于com.test.users.LoadUsersFromAD.run(LoadUsersFromAD.java:102)
位于java.lang.Thread.run(未知源)
原因:org.springframework.beans.BeanInstantiationException:无法实例化bean类[org.springframework.ldap.core.LdapTemplate]:构造函数引发异常;嵌套异常是java.lang.NullPointerException
位于org.springframework.beans.BeanUtils.InstanceClass(BeanUtils.java:164)
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:89)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1069)
... 还有15个
原因:java.lang.NullPointerException
位于org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper.isAtLeast30(DefaultObjectDirectoryMapper.java:80)
位于org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper。(DefaultObjectDirectoryMapper.java:71)
位于org.springframework.ldap.core.LdapTemplate(LdapTemplate.java:93)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(未知源)
位于sun.reflect.delegatingConstructor或AccessorImpl.newInstance(未知源)
位于java.lang.reflect.Constructor.newInstance(未知源)
位于org.springframework.beans.BeanUtils.InstanceClass(BeanUtils.java:148)
... 还有17个
编辑:这是SpringLDAP 2.0.0中的一个错误。确保至少使用2.0.1或2.0.2(最新版本)
以下是我的第一个解释:
显然,根据stacktrace,Spring在确定Spring版本时存在问题。让我解释一下发生了什么:
SpringLDAP正在尝试确定使用的Spring版本是否大于3.0(发生在方法isAtLeast30中)。这是通过使用SpringJAR(特别是密钥实现版本)中MANIFEST.MF的信息来完成的。如果缺少此信息(例如,因为您有一个重新打包的Spring版本),则实现版本将返回null,这将导致此NullPointerException
因此,您应该检查SpringCoreJAR是否有正确的MANIFEST.MF(在META-INF文件夹中)。也可能是这样,您拥有正确的MANIFEST.MF,但类加载器无法检索元数据。在这种情况下,除了在Spring LDAP项目中提交错误之外,您无法做任何事情,因为他们在方法中不处理这种情况。请发布NullPointerException的完整stacktrace。很抱歉,我已将完整跟踪添加到原始帖子中。感谢您的帮助。你的解决方案解决了这个问题。在我的情况下,我在做一个老项目,这个NPE ex
ApplicationContext context = new ClassPathXmlApplicationContext(
"ldap-config.xml");
PersonRepo simpleSearch = context.getBean("personRepo",
PersonRepo.class);
Error creating bean with name 'naLdap': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'naLdap': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.test.users.LoadUsersFromAD.getAllUsersToExamine(LoadUsersFromAD.java:330)
at com.test.users.LoadUsersFromAD.run(LoadUsersFromAD.java:102)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.ldap.core.LdapTemplate]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1069)
... 15 more
Caused by: java.lang.NullPointerException
at org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper.isAtLeast30(DefaultObjectDirectoryMapper.java:80)
at org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper.<init>(DefaultObjectDirectoryMapper.java:71)
at org.springframework.ldap.core.LdapTemplate.<init>(LdapTemplate.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
... 17 more