我正在使用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 config的LdapTemplate包含填充的authenticationSource,而通过Java代码进行的实例化将authenticationSource保留为null。

有任何想法吗?

编辑:
 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)

最佳答案

LdapContextSourceInitializingBean。这意味着,如果要手动创建实例,则必须调用afterPropertiesSet()。引用its JavaDoc:



顺便说一句,您是否知道可以使用属性占位符${myproperty}将配置保留在外部属性源(例如.properties文件)中?检查方案2中的this blog post<context:property-placeholder>示例。

我建议将上下文源和LDAP模板保留在XML内。

关于java - Spring LDAP NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19358762/

10-09 16:51