在Config中将ObjectDirectoryMapper连接到LdapTemplate?

时间:2014-11-12 19:43:44

标签: spring spring-ldap

这是问题的后续问题:spring ldap 2.0.1 replacing deprecated OdmManager

以下是JUnit测试的片段:

@Autowired
private LdapTemplate ldapTemplate;

@Autowired
private ObjectDirectoryMapper objectDirectoryMapper;

@Before
public void setUp() {
  ldapTemplate.setObjectDirectoryMapper(objectDirectoryMapper);
}

我想避免以编程方式设置objectDirectoryMapper,如setUp()所示,但我不确定是否查看了< ldap:ldap-template ... />的模式。标记是否有一种方法可以在XML中而不是以编程方式注入objectDirectoryMapper属性。我可能会遗漏一些明显的东西......

这是相关的XML。正如您所看到的,我正在尝试连接目标转换器(更新:实际上主要目标是注入我的自定义转换器以供LdapRepository<User>使用:         

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">

        <context:property-placeholder location="classpath:/test/ldap.properties"
        system-properties-mode="OVERRIDE" />
        <context:annotation-config />

        <ldap:context-source id="contextSource"
                             password="${sample.ldap.password}"
                             url="${sample.ldap.url}"
                             username="${sample.ldap.userDn}"
                             base="${sample.ldap.base}" />

        <ldap:ldap-template id="ldapTemplate" context-source-ref="contextSource" />

        <ldap:repositories base-package="my.domain" />

    ...
    <bean id="conversionService"
          class="org.springframework.context.support.ConversionServiceFactoryBean">
         <property name="converters">
             <list>
                 <bean class="my.domain.StringToRealmConverter" />
                 <bean class="my.domain.RealmToStringConverter"/>
              </list>
         </property>
     </bean>
    ...
        <bean id="conversionServiceConverterManagerBean"
                class="org.springframework.ldap.odm.typeconversion.impl.ConversionServiceConverterManager">
            <constructor-arg ref="conversionService"/>
        </bean>

        <bean id="defaultObjectDirectoryMapperBean" class="org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper">
            <property name="converterManager" ref="conversionServiceConverterManagerBean" />
        </bean>
    </beans>

1 个答案:

答案 0 :(得分:0)

问题解决了。我再看了一下xsd(http://www.springframework.org/schema/ldap/spring-ldap.xsd)并使用odm-ref属性进行了如下修改:

<ldap:ldap-template id="ldapTemplate" odm-ref="defaultObjectDirectoryMapperBean" context-source-ref="contextSource" >