spring注入实验

spring注入实验,第1张

spring注入实验
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);
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5687037.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存