Java spring中的动态注入类
我想根据方法调用的参数动态注入一个类 我尝试使用AspectJ和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
@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中使用我的方法在运行时传递参数如果您有一组有限的值,您可以很好地使用这些条件。否则我认为你需要实施战略模式。此链接将帮助您