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

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

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

问题

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

我有一个类像这样:

import org.springframework.core.convert.converter.Converter

@Component
class MyConverter : Converter<SomeResult, List<UserDto>> {
    ...
}

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

@Service
class MyService(
   private val myConverter: Converter<SomeResult, List<UserDto>>
){
...
}

但是我收到了错误消息:

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

我该如何修复它?

P.S.

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

Converter<UserDto, AnotherDto>

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

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

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

private val myConverter: MyConverter 

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

英文:

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

I have a class like this:

import org.springframework.core.convert.converter.Converter

@Component
class MyConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt; {
...
}

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

@Service
class MyService(
   private val myConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;
){
...
}

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:

Converter&lt;UserDto, AnotherDto&gt;

P.S.

My problem is I can't autowire by interface

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

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

private val myConverter : MyConverter 

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

答案1

得分: 1

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

@Configuration
class MyConfig {
    // 返回类型是接口
    @Bean fun myConverter(): Converter<SomeResult, List<UserDto>>  = MyConverter()

}

// @Component <-- 不起作用
// 在MyConfig中作为Bean提供 <-- 起作用
class MyConverter : Converter<SomeResult, List<UserDto>> {
    override fun convert(source: SomeResult): List<UserDto> {
        return listOf(UserDto())
    }
}

@Service
class MyService(
    private val myConverter : Converter<SomeResult, List<UserDto>>
){
}
英文:

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

@Configuration
class MyConfig {
    // return type is interface
	@Bean fun myConverter():Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;  = MyConverter()

}

// @Component &lt;-- does not work 
// provided as Bean in MyConfig &lt;-- works
class MyConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt; {
	override fun convert(source: SomeResult): List&lt;UserDto&gt; {
		return listOf(UserDto())
	}
}

@Service
class MyService(
	private val myConverter : Converter&lt;SomeResult, List&lt;UserDto&gt;&gt;
){
}

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:

确定