我正在使用Java Spring版本1.3.1-RELEASE.在尝试执行任何操作时,LdapTemplate似乎存在问题,并且它返回NullPointerException.当我通过xml配置实例化LdapTemplate时,它可以正常工作.但是,当我通过Java代码实例化一个新的LdapTemplate实例并填充我放在xml配置中的属性时,它返回一个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>
当我尝试使用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属性后,似乎LdapTemplate到xml配置包含一个填充的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)
解决方法:
LdapContextSource是InitializingBean.这意味着如果您手动创建实例,则必须调用afterPropertiesSet().引自its JavaDoc:
Note: When using implementations of this class outside of a Spring Context it is necessary to call
#afterPropertiesSet()
when all properties are set, in order to finish up initialization.
顺便提一下,您是否知道可以使用属性占位符${myproperty}将配置保留在外部属性源(例如.properties文件)中?检查this blog post和< context:property-placeholder>方案2中的示例.
我建议将上下文源和LDAP模板留在XML中.