Skip to content

Commit f82cff9

Browse files
119 Improvement: Customize class name and bean name for ConverterRegistrationConfiguration
Added documentation Fixed generated import element to have simple name instead of full name(uncovered by unit test)
1 parent 66ef19d commit f82cff9

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

docs/src/docs/asciidoc/chapter-3-mapper-as-converter.asciidoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,70 @@ public class ConversionServiceAdapterIntegrationTest {
170170
----
171171
====
172172

173+
[[converterRegistrationConfigurationClassName]]
174+
=== Modifying the name for the generated converter registration configuration class
175+
176+
By default, the converter registration configuration class will have name `ConverterRegistrationConfiguration`.
177+
If you wish to change this, you can do so by setting the property `converterRegistrationConfigurationClassName`:
178+
179+
====
180+
[source,java,linenums]
181+
[subs="verbatim,attributes"]
182+
----
183+
@MapperConfig(componentModel = "spring")
184+
@SpringMapperConfig(
185+
converterRegistrationConfigurationClassName = "MyConfiguration",
186+
generateConverterScan = true)
187+
public interface MapstructConfig {}
188+
----
189+
====
190+
191+
This changes the generated class name to be the property's value:
192+
193+
====
194+
[source,java,linenums]
195+
[subs="verbatim,attributes"]
196+
----
197+
@Configuration
198+
class MyConfiguration {
199+
private final ConfigurableConversionService conversionService;
200+
201+
private final List<Converter<?, ?>> converters;
202+
203+
MyConfiguration(
204+
@Qualifier("conversionService") final ConfigurableConversionService conversionService,
205+
final List<Converter<?, ?>> converters) {
206+
this.conversionService = conversionService;
207+
this.converters = converters;
208+
}
209+
210+
@PostConstruct
211+
void registerConverters() {
212+
converters.forEach(conversionService::addConverter);
213+
}
214+
}
215+
----
216+
====
217+
218+
Also this changes reference to converter registration configuration class from generated ConverterScan class:
219+
220+
====
221+
[source,java,linenums]
222+
[subs="verbatim,attributes"]
223+
----
224+
@ComponentScan
225+
@Target(TYPE)
226+
@Import(MyConfiguration.class)
227+
@Documented
228+
@Retention(RUNTIME)
229+
@Repeatable(ConverterScans.class)
230+
public @interface ConverterScan {
231+
...
232+
}
233+
----
234+
====
235+
236+
173237
[[adapterMethodName]]
174238
=== Modifying the name for the generated adapter method
175239

examples/custom-conversion-service-bean/src/main/java/org/mapstruct/extensions/spring/example/custombean/MapperSpringConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
import org.mapstruct.extensions.spring.SpringMapperConfig;
55

66
@MapperConfig(componentModel = "spring", uses = ConversionServiceAdapter.class)
7-
@SpringMapperConfig(conversionServiceBeanName = "myConversionService")
7+
@SpringMapperConfig(conversionServiceBeanName = "myConversionService", generateConverterScan = true)
88
public interface MapperSpringConfig {
99
}

extensions/src/main/java/org/mapstruct/extensions/spring/converter/ConversionServiceAdapterDescriptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public ConversionServiceAdapterDescriptor configurationClassName(
6969
return this;
7070
}
7171

72+
public String getConfigurationClassName() {
73+
return configurationClassName;
74+
}
75+
7276
public boolean isGenerateConverterScan() {
7377
return generateConverterScan;
7478
}

extensions/src/main/java/org/mapstruct/extensions/spring/converter/ConverterScanGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected TypeSpec createMainTypeSpec(final ConversionServiceAdapterDescriptor d
4141
TypeSpec.annotationBuilder(descriptor.getConverterScanClassName()).addModifiers(PUBLIC);
4242
final var importAnnotationSpec =
4343
AnnotationSpec.builder(IMPORT_CLASS_NAME)
44-
.addMember("value", "$L", descriptor.getConverterRegistrationConfigurationClassName() + ".class")
44+
.addMember("value", "$L", descriptor.getConfigurationClassName() + ".class")
4545
.build();
4646
Optional.ofNullable(buildGeneratedAnnotationSpec())
4747
.ifPresent(converterScanClassTypeSpecBuilder::addAnnotation);

0 commit comments

Comments
 (0)