【问题标题】:Spring error instantiating BeanSpring错误实例化Bean
【发布时间】:2014-11-10 03:40:06
【问题描述】:

当我尝试在 Eclipse 之外运行我的应用程序时,我收到以下错误:

创建名为“naLdap”的 bean 时出错:bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [org.springframework.ldap.core.LdapTemplate]:构造函数抛出异常;嵌套异常是 java.lang.NullPointerException

搜索所有帖子,我只找到有关使用 Spring 身份验证的示例。在本例中,我根据区域搜索三个不同的 Ldap 实例。任何帮助/见解将不胜感激。

配置如下:

<?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>

实现:

/**
 * 
 */
package com.test.users.repo;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

import java.util.ArrayList;
import java.util.List;

import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;

import com.test.users.beans.Person;
import com.test.users.ldap.mappers.PersonAttributeMapper;
import com.test.users.utils.LdapRegionsEnum;

/**
 * @author
 * 
 */
public class PersonRepoImpl implements PersonRepo {

    private LdapTemplate    naTemplate;
    private LdapTemplate    asiaTemplate;
    private LdapTemplate    europeTemplate;
    private String          networkId;

    /**
     * 
     */
    public PersonRepoImpl() {
        super();
        // TODO Auto-generated constructor stub
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.valspar.users.repo.PersonRepo#getAllPersons(com.valspar.users.utils
     * .LdapRegionsEnum)
     */
    @Override
    public List<Person> getAllPersons(LdapRegionsEnum region) {
        PersonAttributeMapper mapper = new PersonAttributeMapper();
        AndFilter filter = new AndFilter();
        filter.and(new EqualsFilter("objectClass", "person"));
        LdapTemplate template = null;
        switch (region) {
            case ASIA:
                template = this.getAsiaTemplate();
                break;
            case EUROPE:
                template = this.getEuropeTemplate();
                break;
            default:
                template = this.getNaTemplate();
        }
        return template.search(
                query().where("objectclass").is("person").and("samAccountName")
                        .like(networkId), mapper);
    }

    @SuppressWarnings("unchecked")
    public List<Person> getAllPersons() {

        List<Person> people = new ArrayList<Person>();
        for (LdapRegionsEnum region : LdapRegionsEnum.values()) {
            people.addAll(getAllPersons(region));
        }
        return people;
    }

    /**
     * @return the networkId
     */
    public String getNetworkId() {
        return networkId;
    }

    /**
     * @param networkId
     *            the networkId to set
     */
    public void setNetworkId(String networkId) {
        this.networkId = networkId;
    }

    /**
     * @return the naTemplate
     */
    public LdapTemplate getNaTemplate() {
        return naTemplate;
    }

    /**
     * @param naTemplate
     *            the naTemplate to set
     */
    public void setNaTemplate(LdapTemplate naTemplate) {
        this.naTemplate = naTemplate;
    }

    /**
     * @return the asiaTemplate
     */
    public LdapTemplate getAsiaTemplate() {
        return asiaTemplate;
    }

    /**
     * @param asiaTemplate
     *            the asiaTemplate to set
     */
    public void setAsiaTemplate(LdapTemplate asiaTemplate) {
        this.asiaTemplate = asiaTemplate;
    }

    /**
     * @return the europeTemplate
     */
    public LdapTemplate getEuropeTemplate() {
        return europeTemplate;
    }

    /**
     * @param europeTemplate
     *            the europeTemplate to set
     */
    public void setEuropeTemplate(LdapTemplate europeTemplate) {
        this.europeTemplate = europeTemplate;
    }

}

这是我的实例化方式:

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

【问题讨论】:

  • 请发布 NullPointerException 的完整堆栈跟踪。
  • 对不起,我在原帖中添加了完整的跟踪。

标签: java spring


【解决方案1】:

编辑:这是 Spring LDAP 2.0.0 中的一个错误。确保您至少使用 2.0.1 或 2.0.2(最新版本)

这是我的第一个答案作为解释:

显然根据你的stacktrace Spring判断Spring版本有问题。让我解释一下会发生什么:

Spring LDAP 正在尝试确定使用的 Spring 版本是否大于 3.0(发生在方法 isAtLeast30 中)。这是通过使用来自 Spring jar 的 MANIFEST.MF 的信息(特别是关键的 Implementation-Version)来完成的。如果缺少此信息(例如,因为您有重新打包的 Spring 版本),则实现版本返回 null,这会导致此 NullPointerException。

所以您应该检查您的 Spring 核心 jar 是否有正确的 MANIFEST.MF(在 META-INF 文件夹中)。也可能是您拥有正确的 MANIFEST.MF,但类加载器无法检索元数据。在这种情况下,除了在 Spring LDAP 项目中提交错误之外,您什么也做不了,因为他们没有在方法中处理这种情况。

【讨论】:

  • 感谢您的帮助。您的解决方案已解决。
  • 就我而言,我正在处理一个旧项目并且弹出了这个 NPE 异常。不知道结果如何,但我的 Spring LDAP Core 2.0.0.RELEASE jar 在某些情况下选择了错误的清单文件。我将我的 War 清单中的 Implementation-Version 指定为 2.0.0.RELEASE 并且 NPE 消失了。在我升级依赖项时,这将是一个有用的权宜之计。谢谢!
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 2017-04-23
  • 2016-06-08
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-10-15
相关资源
最近更新 更多