gallery/image/112385.html">http://www.cnblogs.com/hongten/gallery/image/112385.html
这里需要设置环境:
commons-logging.jar
spring.jar
com.b510.bean.dao; PrototypeBeanDao { prototype(); }
etonBeanDao.java
com.b510.bean; com.b510.bean.dao.PrototypeBeanDao; PrototypeBean PrototypeBeanDao { prototype() { System.out .println("原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例"); } }
etonBean.java
com.b510.bean; com.b510.bean.dao.SingletonBeanDao; eton SingletonBean SingletonBeanDao { etonBeanDao#singleton() singleton() { System.out.println("单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例"); } }
/
com.b510.test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.b510.bean.dao.PrototypeBeanDao; com.b510.bean.dao.SingletonBeanDao; eton(单例模式)和prototype(原型模式) 默认指定Bean的作用域为singleton(单例模式),java在创建java实例 垃圾回收,而这些工作都会导致系统开销的增加。 eton(单例模式)作用域的Bean实例一次就可以 eton(单例模式),除非有必要有prototype(原型模式)。 BeanTest { aram main(String[] args) { Beantest().instanceContext(); } instanceContext() { ApplicationContext ctx = ClasspathXmlApplicationContext("beans.xml"); SingletonBeanDao singletonBeanDao = (SingletonBeanDao) ctx .getBean("single"); singletonBeanDao.singleton(); SingletonBeanDao singletonBeanDao1 = (SingletonBeanDao) ctx .getBean("single"); singletonBeanDao1.singleton(); System.out.print("singletonBeanDao与singletonBeanDao1是否是同一个:"); System.out.println(singletonBeanDao1==singletonBeanDao); PrototypeBeanDao prototypeBeanDao = (PrototypeBeanDao) ctx .getBean("proto"); prototypeBeanDao.prototype(); PrototypeBeanDao prototypeBeanDao1 = (PrototypeBeanDao) ctx .getBean("proto"); prototypeBeanDao1.prototype(); System.out.print("prototypeBeanDao与prototypeBeanDao1是否是同一个:"); System.out.println(prototypeBeanDao==prototypeBeanDao1); } }
效果:
2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: 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 2012-3-6 18:19:34 org.springframework.beans.factory.xml.XmlBeanDeFinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [beans.xml] 2012-3-6 18:19:34 org.springframework.context.support.AbstractApplicationContext obtainFreshbeanfactory 信息: Bean factory application context [org.springframework.context.support.ClasspathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListablebeanfactory@1507fb2 2012-3-6 18:19:34 org.springframework.beans.factory.support.DefaultListablebeanfactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListablebeanfactory@1507fb2: defining beans [single,proto]; root of factory hierarchy 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例 单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例 singletonBeanDao与singletonBeanDao1是否是同一个: 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例 原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例 prototypeBeanDao与prototypeBeanDao1是否是同一个:
我们看到:
eton的时候,singletonBeanDao与singletonBeanDao1是同一个对象;而使用prototype的时候,prototypeBeanDao与prototypeBeanDao1不是同一个对象;他是
调用的时候,才new出来的。而singleton是一次创建,多次使用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。