java – Spring LDAP NullPointerException
I am using java spring version 1.3 1-RELEASE. There appears to be a problem with ldaptemplate when trying to perform any operation, and it returns NullPointerException When I instantiate ldaptemplate through XML configuration, it works normally However, when I instantiate a new ldaptemplate instance through Java code and populate the properties I put in the XML configuration, it returns a NullPointerException
xml congfig
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> <property name="url" value="ldap://localhost:10389"/> <property name="base" value="o=channel"/> <property name="userDn" value="uid=admin,ou=system"/> <property name="password" value="secret"/> </bean> <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> <constructor-arg ref="contextSource"/> </bean> <bean id="ldapService" class="com.webchannel.services.ldap.LDAPService" scope="prototype"> <!-- <property name="dao" ref="dao"></property>--> <property name="ldapTemplate" ref="ldapTemplate"></property> </bean> </beans>
When I try to save entries using ldaptemplate, it works perfectly However, when instantiating a new ldaptemplate, I get a NullPointerException:
public class LDAPService<T> { private LdapTemplate ldapTemplate; private LdapContextSource ldapContextSource; public LDAPService(DAO dao) throws Exception { ldapContextSource = new LdapContextSource(); ldapTemplate = new LdapTemplate(); ldapContextSource.setUrl(dao.findAll(LDAPDetail.class).get(0).getUrl()); ldapContextSource.setBase(dao.findAll(LDAPDetail.class).get(0).getBase()); ldapContextSource.setUserDn(dao.findAll(LDAPDetail.class).get(0).getUserDn()); ldapContextSource.setPassword(dao.findAll(LDAPDetail.class).get(0).getpassword()); ldapTemplate = new LdapTemplate(ldapContextSource); // this does not work ldapTemplate.setContextSource(ldapContextSource); // this does not work ldapTemplate.afterPropertiesSet(); } }
I need the second method because I don't want to hard code the information in the XML configuration. I need to get the information from the database at run time
When using ldaptemplate to save, delete, update or find, it returns Java lang.NullPointerException.
After comparing the ldaptemplate attribute of the two, it seems that the ldaptemplate to XML configuration contains a populated authenticationsource, while the authenticationsource is left null through the instantiation of Java code
Any ideas?
Edit:
java.lang.NullPointerException at org.springframework.ldap.core.support.AbstractContextSource.getReadWriteContext(AbstractContextSource.java:138) at org.springframework.ldap.core.LdapTemplate.executeReadWrite(LdapTemplate.java:801) at org.springframework.ldap.core.LdapTemplate.bind(LdapTemplate.java:996) at org.springframework.ldap.core.LdapTemplate.bind(LdapTemplate.java:1354) at com.webchannel.services.ldap.LDAPService.saveEntry(LDAPService.java:122) at com.webchannel.services.ldap.LDAPServiceTest.testSaveEntry(LDAPServiceTest.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:292) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Solution
Ldapcontextsource is initializingbean This means that if you create an instance manually, you must call afterpropertieset() Quoted from its Javadoc:
By the way, did you know that you can use the property placeholder ${myproperty} to keep the configuration in an external property source, such as a. Properties file? Check this blog post and the example in < context: Property placeholder > scheme 2
I recommend leaving context sources and LDAP templates in XML