Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring中的动态注入类_Java_Spring_Spring Boot_Kotlin_Dependency Injection - Fatal编程技术网

Java spring中的动态注入类

Java spring中的动态注入类,java,spring,spring-boot,kotlin,dependency-injection,Java,Spring,Spring Boot,Kotlin,Dependency Injection,我想根据方法调用的参数动态注入一个类 我尝试使用AspectJ和Spring的不同注释 我需要这样的解决方案: @Component class MyUseCase(private val spi: MySpiPort) { fun myAction() { spi.doSomething("myId") } } interface Injectable interface MySpiPort : Injectable { fun doSomethi

我想根据方法调用的参数动态注入一个类

我尝试使用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")
    }
}


我希望inject只是实现的一个公共接口,我不希望在MyUseCase类中注入FactoryBean类或类似的东西。

您可以利用Spring的条件注入。请参阅

如何在@ConditionalIf中使用我的方法在运行时传递参数如果您有一组有限的值,您可以很好地使用这些条件。否则我认为你需要实施战略模式。此链接将帮助您