Skip to content

Commit 239242c

Browse files
committed
Use Published Conversion Service
This configuration class publishes an ObjectDirectoryMapper instance that is configured with the published ConversionService, including any custom converters it is configured with. Closes gh-633
1 parent a09489f commit 239242c

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies {
1616
management platform(project(":spring-ldap-dependencies"))
1717
api "org.springframework:spring-core"
1818
api "org.springframework:spring-beans"
19+
api "org.springframework:spring-context"
1920
api "org.springframework:spring-tx"
2021
api "io.micrometer:micrometer-core"
2122

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ldap.convert;
18+
19+
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
21+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22+
import org.springframework.core.convert.ConversionService;
23+
import org.springframework.core.convert.converter.ConverterRegistry;
24+
25+
/**
26+
* A {@link BeanFactoryPostProcessor} to add Spring LDAP converters to the default
27+
* {@link ConversionService}.
28+
*
29+
* <p>
30+
* Note that this post processor will ignore user-defined {@link ConversionService} beans
31+
* named {@code conversionService}.
32+
*
33+
* <p>
34+
* In that case, you can apply {@link ConverterUtils#addDefaultConverters} to your
35+
* instance instead
36+
*
37+
* @author Josh Cummings
38+
* @since 3.3
39+
*/
40+
public final class ConversionServiceBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
41+
42+
private static final String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
@Override
48+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
49+
if (hasUserDefinedConversionService(beanFactory)) {
50+
return;
51+
}
52+
ConversionService service = beanFactory.getConversionService();
53+
if (service instanceof ConverterRegistry registry) {
54+
ConverterUtils.addDefaultConverters(registry);
55+
}
56+
}
57+
58+
private boolean hasUserDefinedConversionService(ConfigurableListableBeanFactory beanFactory) {
59+
return beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME)
60+
&& beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class);
61+
}
62+
63+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ldap.odm.config;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Fallback;
22+
import org.springframework.core.convert.ConversionService;
23+
import org.springframework.ldap.convert.ConversionServiceBeanFactoryPostProcessor;
24+
import org.springframework.ldap.odm.core.ObjectDirectoryMapper;
25+
import org.springframework.ldap.odm.core.impl.DefaultObjectDirectoryMapper;
26+
27+
/**
28+
* Configuration class for {@link ObjectDirectoryMapper}
29+
*
30+
* @author Josh Cummings
31+
* @since 3.3
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
public class ObjectDirectoryMapperConfiguration {
35+
36+
@Bean
37+
ConversionServiceBeanFactoryPostProcessor conversionServiceBeanPostProcessor() {
38+
return new ConversionServiceBeanFactoryPostProcessor();
39+
}
40+
41+
@Bean
42+
@Fallback
43+
ObjectDirectoryMapper objectDirectoryMapper(ConversionService conversionService) {
44+
DefaultObjectDirectoryMapper objectDirectoryMapper = new DefaultObjectDirectoryMapper();
45+
objectDirectoryMapper.setConversionService(conversionService);
46+
return objectDirectoryMapper;
47+
}
48+
49+
}

modules/ROOT/pages/odm.adoc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,38 @@ The Spring LDAP project offers a similar ability with respect to LDAP directorie
1414
* `void update(Object entry)`
1515
* `void delete(Object entry)`
1616

17+
[[configuration]]
18+
== Configuration
19+
20+
`LdapTemplate` constructs a default `ObjectDirectoryMapper` which typically renders additional configuration unnecessary.
21+
22+
However, you can also make `ObjectDirectoryMapper` available as a `@Bean` by importing `ObjectDirectoryMapperConfiguration` like so:
23+
24+
[source,java]
25+
----
26+
@Import(ObjectDirectoryMapperConfiguration.class)
27+
@Configuration
28+
public class LdapConfig {
29+
// ...
30+
}
31+
----
32+
33+
You can then supply it to your `LdapTemplate` instance as follows:
34+
35+
[source,java]
36+
----
37+
@Bean
38+
LdapTemplate ldapTemplate(ContextSource contextSource, ObjectDirectoryMapper odm) {
39+
LdapTemplate ldap = new LdapTemplate(contextSource);
40+
ldap.setObjectDirectoryMapper(odm);
41+
return ldap;
42+
}
43+
----
44+
45+
Using this import provides the benefit that it will automatically configure `ObjectDirectoryMapper` with the `ConversionService` in the `ApplicationContext`.
46+
47+
This means that Spring LDAP will use your configured `Converter` instances.
48+
1749
[[annotations]]
1850
== Annotations
1951

0 commit comments

Comments
 (0)