spring开发_spring中Bean的作用域_singleton_prototype

spring开发_spring中Bean的作用域_singleton_prototype,第1张

概述java spring bean singleton prototype spring的Bean的作用

href="http://www.cnblogs.com/hongten/gallery/image/112385.HTML">http://www.cnblogs.com/hongten/gallery/image/112385.HTML

这里需要设置环境:

添加如下jar包

commons-logging.jar

spring.jar

com.b510.bean.dao; PrototypeBeanDao { prototype(); }

com.b510.bean.dao; SingletonBeanDao { singleton(); }

com.b510.bean; com.b510.bean.dao.PrototypeBeanDao; PrototypeBean PrototypeBeanDao { prototype() { System.out .println("原型模式,每次通过容器的getBean方法获取prototype定义的Bean,都将产生一个新的Bean实例"); } }

com.b510.bean; com.b510.bean.dao.SingletonBeanDao; SingletonBean SingletonBeanDao { singleton() { System.out.println("单例模式,在整个spring IoC容器中,使用singleton定义Bean将只有一个实例"); } }

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http: ="com.b510.bean.SingletonBean"> ="com.b510.bean.PrototypeBean" scope="prototype">

/

com.b510.test; org.springframework.context.ApplicationContext; org.springframework.context.support.ClasspathXmlApplicationContext; com.b510.bean.dao.PrototypeBeanDao; com.b510.bean.dao.SingletonBeanDao; BeanTest { 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是否是同一个:

我们看到:

总结

以上是内存溢出为你收集整理的spring开发_spring中Bean的作用域_singleton_prototype全部内容,希望文章能够帮你解决spring开发_spring中Bean的作用域_singleton_prototype所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1270040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存