您当前的位置: 首页 >  spring

qq_34412985

暂无认证

  • 0浏览

    0关注

    1061博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

spring注入map,spring注入一个接口的多个实现类在map里

qq_34412985 发布时间:2021-01-28 22:00:04 ,浏览量:0

一个接口,两个实现类

接口:

 

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 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

 

关注
打赏
1653291990
查看更多评论
立即登录/注册

微信扫码登录

0.0433s