如何在Kotlin中注入返回泛型类型列表的转换器?

huangapple go评论114阅读模式
英文:

How to inject Converter which returns list of generic type in Kotlin?

问题

我的应用程序是用Kotlin实现的,我使用Spring Boot 3。

我有一个类像这样:

  1. import org.springframework.core.convert.converter.Converter
  2. @Component
  3. class MyConverter : Converter<SomeResult, List<UserDto>> {
  4. ...
  5. }

我想要将它(使用接口!!!)注入到另一个组件中:

  1. @Service
  2. class MyService(
  3. private val myConverter: Converter<SomeResult, List<UserDto>>
  4. ){
  5. ...
  6. }

但是我收到了错误消息:

  1. Parameter 1 of constructor in ******.MyService required a bean of type
  2. 'org.springframework.core.convert.converter.Converter' that could not
  3. be found.

我该如何修复它?

P.S.

这个技巧对于没有泛型的转换器完美适用。例如:

  1. Converter<UserDto, AnotherDto>

P.S.
我的问题是我不能按接口自动装配

  1. private val myConverter: Converter<SomeResult, List<UserDto>>

作为一种解决方法,我可以按类型自动装配(它可以工作)

  1. private val myConverter: MyConverter

但从我看来,这不是完美的。

英文:

My application is implemented on Kotlin and I use spring boot 3.

I have a class like this:

  1. import org.springframework.core.convert.converter.Converter
  2. @Component
  3. class MyConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt; {
  4. ...
  5. }

I want to inject it(using interface!!!) to another component:

  1. @Service
  2. class MyService(
  3. private val myConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;
  4. ){
  5. ...
  6. }

But I receive the error:

> Parameter 1 of constructor in ******.MyService required a bean of type
> 'org.springframework.core.convert.converter.Converter' that could not
> be found.

How can I fix it ?

P.S.

This trick perfectly works for converters without generics. For example:

  1. Converter&lt;UserDto, AnotherDto&gt;

P.S.

My problem is I can't autowire by interface

  1. private val myConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;

As a workaround I can autowire by type(and it works)

  1. private val myConverter : MyConverter

But it doesn't look perfect from my point of view

答案1

得分: 1

如果您将您的转换器作为@Bean提供,您应该能够注入这个转换器(甚至作为一个接口)。

  1. @Configuration
  2. class MyConfig {
  3. // 返回类型是接口
  4. @Bean fun myConverter(): Converter<SomeResult, List<UserDto>> = MyConverter()
  5. }
  6. // @Component <-- 不起作用
  7. // 在MyConfig中作为Bean提供 <-- 起作用
  8. class MyConverter : Converter<SomeResult, List<UserDto>> {
  9. override fun convert(source: SomeResult): List<UserDto> {
  10. return listOf(UserDto())
  11. }
  12. }
  13. @Service
  14. class MyService(
  15. private val myConverter : Converter<SomeResult, List<UserDto>>
  16. ){
  17. }
英文:

If you provide your converter as a @Bean, you should be able to inject this converter (even as an interface)

  1. @Configuration
  2. class MyConfig {
  3. // return type is interface
  4. @Bean fun myConverter():Converter&lt;SomeResult, List&lt;UserDto&gt;&gt; = MyConverter()
  5. }
  6. // @Component &lt;-- does not work
  7. // provided as Bean in MyConfig &lt;-- works
  8. class MyConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt; {
  9. override fun convert(source: SomeResult): List&lt;UserDto&gt; {
  10. return listOf(UserDto())
  11. }
  12. }
  13. @Service
  14. class MyService(
  15. private val myConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;
  16. ){
  17. }

huangapple
  • 本文由 发表于 2023年7月13日 23:31:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定