我正在使用 Java Spring 版本 1.3.1-RELEASE。尝试执行任何操作时,LdapTemplate 似乎存在问题,它返回 NullPointerException。当我通过 xml 配置实例化 LdapTemplate 时,它可以工作。但是,当我通过 Java 代码实例化一个新的 LdapTemplate 实例并填充我放在 xml 配置中的属性时,它会返回 NullPointerException。
xml配置
<?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>
当我尝试使用 LdapTemplate 保存条目时,它可以完美运行。但是,当实例化一个新的 LdapTemplate 时,我得到一个 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();
}
}
我需要第二种方法,因为我不想在 xml 配置中硬编码信息,我需要在运行时从数据库中获取信息。
当使用 ldapTemplate 保存、删除、更新或查找时,它会返回 java.lang.NullPointerException。
比较了两者的 ldapTemplate 属性后,似乎通过 xml 配置的 LdapTemplate 包含填充的 authenticationSource 并且通过 Java 代码的实例化使 authenticationSource 为空。
有任何想法吗?
编辑:
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)