1、接口
//一个简单的接口类
public interface Animals {
}
2、实现类1
//实现类1
//@Component注解后要加上属性值
@Component(value = "cat")
public class TestCatImpl implements Animals {
}
3、实现类2
//实现类2
@Component(value = "dog")
public class TestDogImpl implements Animals{
@Override
public void printSomeWords() {
System.out.println("wangwang");
}
}
4、调用接口
//先使用map将该接口注入进去,项目启动后,会将对应的实现类加载进去 @Autowired Map第二种写法map = new HashMap<>(); //key对应的是实现类中@Component注解中对应的属性参数 String key = "cat"; //获取的实例便是TestCatImpl实现类 Animals animals = map.get(key);
有时间再写



