实例化Bean的Spring错误

时间:2014-11-09 15:40:48

标签: java spring

当我尝试在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

1 个答案:

答案 0 :(得分:2)

编辑:这是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项目中提交错误之外,您不能做任何事情,因为他们不会在方法中处理这种情况。