【发布时间】:2020-02-16 20:18:47
【问题描述】:
我想根据我的方法调用的参数动态注入一个类
我尝试过使用 AspectJ 和 Spring 的不同注解
我需要这样的解决方案:
@Component
class MyUseCase(private val spi: MySpiPort) {
fun myAction() {
spi.doSomething("myId")
}
}
interface Injectable
interface MySpiPort : Injectable {
fun doSomething(id: String)
}
class MyProxyClass {
//will intercept all Injectable
fun resolver(id: String): MySpiPort {
if(id == "myId"){
//inject MyFirstImpl
}else{
//inject MySecondImpl
}
TODO("not implemented")
}
}
@Component
class MyFirstImpl : MySpiPort {
override fun doSomething(id: String) {
TODO("not implemented")
}
}
@Component
class MySecondImpl : MySpiPort {
override fun doSomething(id: String) {
TODO("not implemented")
}
}
我希望只注入实现的通用接口,我不想在 MyUseCase 类中注入 FactoryBean 类或类似的东西。
【问题讨论】:
-
我认为您需要实施策略模式。此链接将为您提供帮助Strategy pattern with spring beans
标签: java spring spring-boot kotlin dependency-injection