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(); } }
最后注意:光理論是不夠的。在此免費贈送5大JAVA架構項目實戰教程及大廠面試題庫,有興趣的可以進裙 783802103獲取,沒基礎勿進哦!
本文的文字及圖片來源於網絡加上自己的想法,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理