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将只有一个实例"); } }
/
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是否是同一个: