spring开发_spring中Bean的作用域_singleton_prototype

这里需要设置环境:

添加如下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的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...