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

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/227044.html
標籤:Java
上一篇:動態代理原理剖析
