欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用@Autowired注入map

程序员文章站 2022-07-10 19:13:27
个人资源分享网站:http://xiaocaoshare.com/1.创建父接口public interface ActivityBase {}2.分别创建三个实现类@Servicepublic class ExperienceAct implements ActivityBase{}@Servicepublic class GroupAct implements ActivityBase{}@Servicepublic class InviteCardAct...

个人资源分享网站:http://xiaocaoshare.com/

1.创建父接口

public interface ActivityBase {
}

2.分别创建三个实现类

@Service
public class ExperienceAct implements  ActivityBase{
}
@Service
public class GroupAct implements  ActivityBase{
}
@Service
public class InviteCardAct implements  ActivityBase{
}

3.

@RestController
@RequestMapping("")
public class ActiveController {

    @Autowired
    private Map<String, ActivityBase> activityBaseMap;

    @GetMapping("")
    public void test(){
        System.out.println(activityBaseMap.getClass().getName());
        for(Map.Entry<String,ActivityBase> map:activityBaseMap.entrySet()){
            System.out.println(map.getKey()+"=========>"+map.getValue().getClass().getName());
        }
    }
}

4.启动程序并使用postMan调用这个接口,看控制台输出

java.util.LinkedHashMap
experienceAct=========>com.xiao.ActivityDesign.ExperienceAct
groupAct=========>com.xiao.ActivityDesign.GroupAct
inviteCardAct=========>com.xiao.ActivityDesign.InviteCardAct

本文地址:https://blog.csdn.net/xiao297328/article/details/107668320

相关标签: java springmvc