java - 在 Spring 中动态注入(inject)类

标签 java spring spring-boot kotlin dependency-injection

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

我尝试使用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)实现的通用接口(interface),我不想在 MyUseCase 类中注入(inject) FactoryBean 类或类似的东西。

最佳答案

关于java - 在 Spring 中动态注入(inject)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58474494/

相关文章:

java - (Spring boot + Java) 大于 1 GB 文件的下载 API

java - Spring + MongoDB : potential memory leak messages

java - Spring 启动: Configuration Scan missing classes due to "weird" setup

java - 以正确的顺序获取已注册的过滤器

java - Spring Security中的authorizeRequests有什么作用?

java - Json 解析错误 - MismatchedInputException 在 Spring Boot 中期望 LocalDate 的数组或字符串

java - 如何在运行多个服务时只获取一个 Redis 事件?

java - GAE/J 异步数据存储 API : how to do the equivalent of a UNIX select?

java - 在android上安装Java库[GIThub源代码]

java - 如何查看 Apache Derby 是否已安装并正在运行?