提问者:小点点

在配置中将ObjectDirectoryMapper连接到LdapTemplate?


这是问题的后续:Spring ldap 2.0.1 替换已弃用的 OdmManager

下面是一个 JUnit 测试的片段:

@Autowired
private LdapTemplate ldapTemplate;

@Autowired
private ObjectDirectoryMapper objectDirectoryMapper;

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

我想避免像setUp()中所示的那样以编程方式设置objectDirectoryMapper,但是从查看

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

    <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个答案

匿名用户

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

<代码>