public interface TestInterface{ void test(); } @Scope("prototype") @Component("top") public class TestSub1 implements TestInterface { @Override public void test() { System.out.println("test1"); } } @Component public class TestSub2 implements TestInterface { @Override public void test() { System.out.println("test2"); } } @Component public class Test { @Autowired private ApplicationContext applicationContext; @Autowired private TestInterface testInterface; // private TestInterface testInterface; // // public Test(TestInterface 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 map = applicationContext.getBeansOfType(TestInterface.class); for (String key : map.keySet()) { System.out.println(key); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)