Spring 3 - Dynamic Autowiring at runtime based on another object attribute

后端 未结 4 457
北荒
北荒 2020-12-08 11:27

I am working on a Spring 3.1 MVC application and for one of my scenarios, I had to write two implementations of a DAO. I would like to know how to autowire this in a service

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 12:00

    Please one note: previous solution is great and simple, but spring is creating each instance of each bean.

    So my solution is to create abstract class VehicleService instead of interface

    then in appContext

    @Bean
    @Primary
    VehicleService paymentProviderService()
    {
        return ...new (your requested instance by your own)...
    }
    

提交回复
热议问题