Spring DI (Beans) with multiple concretes…picking one of them

后端 未结 2 730
小鲜肉
小鲜肉 2020-12-12 06:24

I have a similar question here

Guice with multiple concretes......picking one of them

with a solution for Guice.

But I have a different project usin

2条回答
  •  失恋的感觉
    2020-12-12 06:55

    Something like this should work. This uses @Autowired and not xml configuration:

    @org.springframework.stereotype.Service
    public class OrderProcessorImpl implements OrderProcessorInterface {
    
        private List shipperProviders;
    
        private Map shipperProvidersMap = new HashMap<>();
    
        @Autowired
        public void setShipperProviders(List shipperProviders) {
            this.shipperProviders= shipperProviders;
    
            this.shipperProviders.stream().forEach(p->shipperProvidersMap .put(/* your code for getting the key */, p));
        }
    

    Gradle dependency hint:

    compile group: 'org.springframework', name: 'spring-context', version: '5.1.9.RELEASE'
    

提交回复
热议问题