spring注入实验

public interface TestInterface<T> {
    void test();
}
@Scope("prototype")
@Component("top")
public class TestSub1 implements TestInterface<TestSub1>{
    @Override
    public void test() {
        System.out.println("test1");
    }
}
@Component
public class TestSub2 implements TestInterface<TestSub2>{
    @Override
    public void test() {
        System.out.println("test2");
    }
}

@Component
public class Test {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 以下两种注入皆可
     */
    @Autowired
    private TestInterface<TestSub1> testInterface;

//    private TestInterface<TestSub1> testInterface;
//
//    public Test(TestInterface<TestSub1> testInterface) {
//        this.testInterface = testInterface;
//    }

    @PostConstruct
    public void init() {
        testInterface.test();
        for (int i = 0; i < 2; i++) {
            TestInterface bean = applicationContext.getBean(TestSub1.class);
            System.out.println(bean);
        }
        for (int i = 0; i < 2; i++) {
            TestInterface bean = applicationContext.getBean(TestSub2.class);
            System.out.println(bean);
        }

        for (int i = 0; i < 2; i++) {
            TestInterface bean = applicationContext.getBean("top", TestInterface.class);
            System.out.println(bean);
        }
        Map<String, TestInterface> map = applicationContext.getBeansOfType(TestInterface.class);
        for (String key : map.keySet()) {
            System.out.println(key);
        }
    }
}