前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring开发_spring中Bean的作用域_singleton_prototype

spring开发_spring中Bean的作用域_singleton_prototype

作者头像
Hongten
发布2018-09-13 16:54:26
4490
发布2018-09-13 16:54:26
举报
文章被收录于专栏:HongtenHongten

项目结构:

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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
 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

代码语言:javascript
复制
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

代码语言:javascript
复制
 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 }

运行效果:

代码语言:javascript
复制
 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是一次创建,多次使用。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-03-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档