spring注入map,spring注入一個接口的多個實現類在map里


spring注入map,spring注入多個實現類在map里

一個接口,兩個實現類

接口:

 

public interface TestService {
    void test();
}

 

兩個實現類

@Component("testOService")
public class TestOService implements TestService {
    @Override
    public void test() {
        System.out.println("testOService");
    }
}

 

@Component("testTwoService")
public class TestTwoService implements TestService {
    @Override
    public void test() {
        System.out.println("testTwoService");
    }
}

 

查看:

@Service
public class UserInfoService {
    
    @Autowired
    private Map<String ,TestService> testServiceMap;
    @PostConstruct
    public void init(){
        testServiceMap.get("testOService").test();
        testServiceMap.get("testTwoService").test();
    }
}

 

 

源碼解析

對應spring源碼位置 org.springframework.beans.factory.support.DefaultListableBeanFactory

方法 : org.springframework.beans.factory.support.DefaultListableBeanFactory#resolveMultipleBeans

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM