Spring finishing 7 - Bean scope

分类:Java 2010-05-12 来源:CodeWeblog.com 人气:177
 <!-- 

    <bean scope="singleton"/>

     -->

    <bean scope="prototype"/>

spring Bean's scope: The main property of the label by the <bean> scope decisions.

scope to value:

* Singleton: each call to getBean return the same instance of time

* Prototype: each call to getBean different instances when the return

Example code:

Bean.java:

public class Bean {

}

applicationContext.xml:

Test code:

public class ScopeTest extends TestCase {

    private BeanFactory factory;

    @Override

    protected void setUp() throws Exception {

       factory = new 

ClassPathXmlApplicationContext("applicationContext.xml"); 

    }

    public void testScope1() {

       Bean1 bean11 = (Bean1)factory.getBean("bean1");

       Bean1 bean12 = (Bean1)factory.getBean("bean1");

       if (bean11 == bean12) {

           System.out.println("bean11==bean12");

       }else {

           System.out.println("bean11!=bean12");

       }

    }

}
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.282 (s). 12 q(s)