spring-data-mongodb icon indicating copy to clipboard operation
spring-data-mongodb copied to clipboard

Support automatic registration of converters

Open sangyongchoi opened this issue 2 years ago • 2 comments

Hi, i am spring-data-mongo user. I had some inconvenience when registered converters. Is currently manually register converters.

@Bean
fun mongoCustomConversions(): MongoCustomConversions {
    val converters = applicationContext.getBeansOfType(Converter::class.java).values
        .stream().collect(Collectors.toList())
    return MongoCustomConversions(converters)
}

Can I change to automatic registration from manually registration ? If it's not possible, can you tell me why?

sangyongchoi avatar Sep 25 '22 07:09 sangyongchoi

How is that supposed to work if a user has multiple Spring Data modules on the class path. We would register all converters in all modules without having a way to distinguish whether a Converter is intended for Spring Data usage and for which module it should be used.

mp911de avatar Sep 26 '22 13:09 mp911de

How is that supposed to work if a user has multiple Spring Data modules on the class path. We would register all converters in all modules without having a way to distinguish whether a Converter is intended for Spring Data usage and for which module it should be used.

@mp911de Thank you for reply 😄 So i hope add interface (e.g DocumentFieldConverter).

interface DocumentFieldConverter<S, T> : Converter<S, T>

Then, this is possible as follows.

if (listableBeanFactory != null) {
	converters = listableBeanFactory
			.getBeansOfType(DocumentFieldConverter.class)
			.values()
			.stream()
			.toList();
} else {
	converters = Collections.emptyList();
}

Is this possible?

sangyongchoi avatar Sep 26 '22 13:09 sangyongchoi

Thanks for your suggestion. We want to keep the existing mechanism and keep the converter registration as simple as possible. If adding an interface to your converters works in your project, then we encourage you to follow that path and apply that pattern in your project.

mp911de avatar Oct 18 '22 08:10 mp911de