【问题标题】:Dynamically inject class in spring在spring中动态注入类
【发布时间】: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 类或类似的东西。

【问题讨论】:

标签: java spring spring-boot kotlin dependency-injection


【解决方案1】:

【讨论】:

  • 如何在我的方法中获取运行时传递的参数以在@Conditional中使用
  • 如果你有一组有限的值,你可以很好地使用条件。否则不
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多