pcroadkill
pcroadkill

Reputation: 107

Spring error instantiating Bean

When I try to run my application outside of eclipse, I get the following error:

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

Searching all posts, I only find examples about using Spring authentication. In this instance I am searching three different Ldap instances based on region. Any help/insight will be greatly appreciated.

The configuration is as follows:

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

The implementation:

/**
 * 
 */
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;
    }

}

Here is how I am instantiating:

ApplicationContext context = new ClassPathXmlApplicationContext(
        "ldap-config.xml");

PersonRepo simpleSearch = context.getBean("personRepo",
        PersonRepo.class);

Complete stack trace:

 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

Upvotes: 0

Views: 4033

Answers (1)

dunni
dunni

Reputation: 44515

EDIT: This is a bug in Spring LDAP 2.0.0. Make sure you use at least 2.0.1 or 2.0.2 (the latest version)

Here my first answer as explanation:

Obviously according to your stacktrace Spring has problems to determine the Spring version. Let me explain what happens:

Spring LDAP is trying to determine, if the used Spring version is larger than 3.0 (happens in the method isAtLeast30). This is done by using the information from the MANIFEST.MF from the Spring jar (specifically the key Implementation-Version). If this information is missing (e.g. because you have a repackaged Spring version) the implementation version returns null, which causes this NullPointerException.

So you should check if your Spring core jar has a correct MANIFEST.MF (in the folder META-INF). It may also be the case, that you have the correct MANIFEST.MF, but the metadata can not be retrieved by the classloader. In that case, you can't do anything besides filing a bug in the Spring LDAP project, because they don't handle that case in the method.

Upvotes: 2

Related Questions