繁体   English   中英

在 spring 中动态注入 class

[英]Dynamically inject class in spring

我想根据我的方法调用的参数动态注入 class

我尝试使用 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 Class 中注入 FactoryBean Class 或类似的东西。

我认为您需要实施策略模式。

此链接将帮助您使用 spring bean 的策略模式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM