spring5.0的scope测试

版权声明:本站所提供的文章资讯、软件资源、素材源码等内容均为本作者提供、网友推荐、互联网整理而来(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考,如有侵犯您的版权,请联系我,本作者将在三个工作日内改正。 https://blog.csdn.net/weixin_42323802/article/details/83412500

声明:使用JDK8 ,spring5.0进行测试;
scope:指定对象的作用范围。
singleton :默认值,单例的.
prototype :多例的.

创建实例bean,装配到spring中,分别使用singleton 和prototype 进行测试;
测试装配如下,首先测多例;

    <!-- 装配scopebean  单例的默认 prototype 是多例的   -->
    <bean id="scopeBean" class="bean.ScopeBean" scope="prototype"></bean>

客户端测试;

    @Test
    public  void test02() {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ScopeBean scopeBean1 = (ScopeBean) applicationContext.getBean("scopeBean");
        ScopeBean scopeBean2 = (ScopeBean) applicationContext.getBean("scopeBean");
        System.out.println(scopeBean1);
        System.out.println(scopeBean2);
    }

跑下;
s

把 scope="prototype"删除,跑下;
ceshi2可以明确的看到scope 对 bean 范围约束;

猜你喜欢

转载自blog.csdn.net/weixin_42323802/article/details/83412500