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(); } }