spring开发_spring中Bean的作用域_singleton_prototype

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112385.html

这里需要设置环境:

添加如下jar包

commons-logging.jar

spring.jar

/spring_0003_bean的作用域/src/com/b510/bean/dao/PrototypeBeanDao.java

 1 package com.b510.bean.dao;
2
3 /**
4 * 原型模式dao
5 *
6 * @author Hongten
7 *
8 */
9 public interface PrototypeBeanDao {
10
11 /**
12 * 原型模式
13 */
14 public abstract void prototype();
15
16 }

/spring_0003_bean的作用域/src/com/b510/bean/dao/SingletonBeanDao.java

 1 package com.b510.bean.dao;
2
3 /**
4 * 单例模式dao
5 *
6 * @author Hongten
7 *
8 */
9 public interface SingletonBeanDao {
10
11 /**
12 * 单例模式
13 */
14 public abstract void singleton();
15
16 }

/spring_0003_bean的作用域/src/com/b510/bean/PrototypeBean.java

 1 package com.b510.bean;
2
3 import com.b510.bean.dao.PrototypeBeanDao;
4
5 /**
6 * 原型模式prototype
7 *
8 * @author Hongten
9 *
10 */
11 public class PrototypeBean implements PrototypeBeanDao {
12
13 /* (non-Javadoc)
14 * @see com.b510.bean.PrototypeBeanDao#prototype()
15 */
16 public void prototype() {
17 System.out
18 .println("原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例");
19 }
20 }

/spring_0003_bean的作用域/src/com/b510/bean/SingletonBean.java

 1 package com.b510.bean;
2
3 import com.b510.bean.dao.SingletonBeanDao;
4
5 /**
6 * 单例模式singleton
7 *
8 * @author Hongten
9 *
10 */
11 public class SingletonBean implements SingletonBeanDao {
12
13 /* (non-Javadoc)
14 * @see com.b510.bean.SingletonBeanDao#singleton()
15 */
16 public void singleton() {
17 System.out.println("单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例");
18 }
19 }

/spring_0003_bean的作用域/src/beans.xml

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6 <bean id="single" class="com.b510.bean.SingletonBean"></bean>
7 <bean id="proto" class="com.b510.bean.PrototypeBean" scope="prototype"></bean>
8 </beans>

/spring_0003_bean的作用域/src/com/b510/test/BeanTest.java

 1 package com.b510.test;
2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6 import com.b510.bean.dao.PrototypeBeanDao;
7 import com.b510.bean.dao.SingletonBeanDao;
8 /**
9 * 测试类,在此类中,我们主要是测试singleton(单例模式)和prototype(原型模式)
10 * 如果不指定Bean的作用域,spring会默认指定Bean的作用域为singleton(单例模式),java在创建java实例
11 * 的时候,需要进行内存申请;销毁实例的时候,需要完成垃圾回收,而这些工作都会导致系统开销的增加。
12 * prototype(原型模式)作用域的创建,销毁代价比较大;singleton(单例模式)作用域的Bean实例一次就可以
13 * 重复利用,因此,我们尽量用singleton(单例模式),除非有必要有prototype(原型模式)。
14 *
15 * @author Hongten
16 *
17 */
18 public class BeanTest {
19
20 /**
21 * @param args
22 */
23 public static void main(String[] args) {
24 new BeanTest().instanceContext();
25 }
26
27 public void instanceContext() {
28 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
29 SingletonBeanDao singletonBeanDao = (SingletonBeanDao) ctx
30 .getBean("single");
31 singletonBeanDao.singleton();
32 SingletonBeanDao singletonBeanDao1 = (SingletonBeanDao) ctx
33 .getBean("single");
34 singletonBeanDao1.singleton();
35 System.out.print("singletonBeanDao与singletonBeanDao1是否是同一个:");
36 System.out.println(singletonBeanDao1==singletonBeanDao);
37 PrototypeBeanDao prototypeBeanDao = (PrototypeBeanDao) ctx
38 .getBean("proto");
39 prototypeBeanDao.prototype();
40 PrototypeBeanDao prototypeBeanDao1 = (PrototypeBeanDao) ctx
41 .getBean("proto");
42 prototypeBeanDao1.prototype();
43 System.out.print("prototypeBeanDao与prototypeBeanDao1是否是同一个:");
44 System.out.println(prototypeBeanDao==prototypeBeanDao1);
45
46 }
47
48 }

运行效果:

 1 2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Tue Mar 06 18:19:34 CST 2012]; root of context hierarchy
3 2012-3-6 18:19:34 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
6 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2
7 2012-3-6 18:19:34 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
8 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2: defining beans [single,proto]; root of factory hierarchy
9 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例
10 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例
11 singletonBeanDao与singletonBeanDao1是否是同一个:true
12 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例
13 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例
14 prototypeBeanDao与prototypeBeanDao1是否是同一个:false

我们看到:

使用singleton的时候,singletonBeanDao与singletonBeanDao1是同一个对象;而使用prototype的时候,prototypeBeanDao与prototypeBeanDao1不是同一个对象;他是

在系统调用的时候,才new出来的。而singleton是一次创建,多次使用。


 


 

原文地址:https://www.cnblogs.com/hongten/p/java_spring_bean_singleton_prototype.html