spring每次getBean(),获得的是否是同一个实例
2014-07-22 18:46
459 查看
spring 缺省:
1.spring用DefaultListableBeanFactory.preInstantiateSingletons()建立bean实例
2.缺省采用单例模式
测试基本bean:
改scope为多实例
使用spring
的时候, 你应该先问问自己,你编一个类,初衷,是否就是准备采用单例? 是否就是准备内部设静态实例,私有化构造方法,静态块初始化,静态getInstance() 方法?
我们可以借鉴一个框架的设计思想和经验,但不要让它帮我们做选择!!!
以此类推,spring 提供给hibernate的SessionFactory是否是单例?
sessionFactory bean 由spring的对应的SessionFactory类建立的bean, 按数据库类型,分为hibernate,JPA等几种,按操作类型,分为专为xml配置文件设计的的和专为标注设计的
sessionFactory bean 尽管是"工厂",但那是指它建立session的功能.它相对于spring来说,仍是一个普通的bean,通常把它作为属性注入到其他需要session的DAO bean中.
所以sessionFactory bean 自身的建立仍采用spring的缺省工厂类,自然缺省也是单例的.
DataSource bean 是否是单例?
DataSource bean通常把它作为属性注入到sessionFactory bean中
它自身仍是普通bean,采用spring的缺省工厂类,自然缺省也是单例的
apacha dbcp datasource
c3p0 datasource
1.spring用DefaultListableBeanFactory.preInstantiateSingletons()建立bean实例
2.缺省采用单例模式
测试基本bean:
xml配置文件: <bean id="dvdTypeDAO" class="com.machome.hibernate.impl.DvdTypeDAOImpl" /> |
测试代码: ctx = new ClassPathXmlApplicationContext("spring-hibernate-mysql.xml"); DvdTypeDAO tDao1 = (DvdTypeDAO)ctx.getBean("dvdTypeDAO"); DvdTypeDAO tDao2 = (DvdTypeDAO)ctx.getBean("dvdTypeDAO"); |
运行: true com.machome.hibernate.impl.DvdTypeDAOImpl@15b0333 com.machome.hibernate.impl.DvdTypeDAOImpl@15b0333 说明前后两次getBean()获得的是同一实例,说明spring缺省是单例 |
<bean id="dvdTypeDAO" class="com.machome.hibernate.impl.DvdTypeDAOImpl" scope="prototype" /> |
执行同样的测试代码 |
运行: false com.machome.hibernate.impl.DvdTypeDAOImpl@afae4a com.machome.hibernate.impl.DvdTypeDAOImpl@1db9852 说明scope="prototype"后,每次getBean()的都是不同的新实例 |
的时候, 你应该先问问自己,你编一个类,初衷,是否就是准备采用单例? 是否就是准备内部设静态实例,私有化构造方法,静态块初始化,静态getInstance() 方法?
我们可以借鉴一个框架的设计思想和经验,但不要让它帮我们做选择!!!
以此类推,spring 提供给hibernate的SessionFactory是否是单例?
sessionFactory bean 由spring的对应的SessionFactory类建立的bean, 按数据库类型,分为hibernate,JPA等几种,按操作类型,分为专为xml配置文件设计的的和专为标注设计的
sessionFactory bean 尽管是"工厂",但那是指它建立session的功能.它相对于spring来说,仍是一个普通的bean,通常把它作为属性注入到其他需要session的DAO bean中.
所以sessionFactory bean 自身的建立仍采用spring的缺省工厂类,自然缺省也是单例的.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>org/machome/hibernate/DvdType.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update </value> </property> </bean> <bean id="dvdTypeDAO" class="com.machome.hibernate.impl.DvdTypeDAOImpl"> <property name="seasonFactory" ref="seasonFactory"/> </bean> |
测试代码: ctx = new ClassPathXmlApplicationContext("spring-hibernate-mysql.xml"); SessionFactory tDao1 = (SessionFactory)ctx.getBean("sessionFactory"); SessionFactory tDao2 = (SessionFactory)ctx.getBean("sessionFactory"); System.out.println(tDao1==tDao2); System.out.println(tDao1); System.out.println(tDao2); |
运行: true org.hibernate.impl.SessionFactoryImpl@12c5431 org.hibernate.impl.SessionFactoryImpl@12c5431 |
DataSource bean通常把它作为属性注入到sessionFactory bean中
它自身仍是普通bean,采用spring的缺省工厂类,自然缺省也是单例的
apacha dbcp datasource
ctx = new ClassPathXmlApplicationContext("spring-hibernate-mysql.xml"); BasicDataSource tDao1 = (BasicDataSource)ctx.getBean("dataSource"); BasicDataSource tDao2 = (BasicDataSource)ctx.getBean("dataSource"); System.out.println(tDao1==tDao2); System.out.println(tDao1); System.out.println(tDao2); |
true org.apache.commons.dbcp.BasicDataSource@ab7165 org.apache.commons.dbcp.BasicDataSource@ab7165 |
ComboPooledDataSource tDao1 = (ComboPooledDataSource)ctx.getBean("dataSource"); ComboPooledDataSource tDao2 = (ComboPooledDataSource)ctx.getBean("dataSource"); System.out.println(tDao1==tDao2); System.out.println(tDao1); System.out.println(tDao2); |
true com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 5, ... ] com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 5, ... ] |
相关文章推荐
- spring每次getBean(),获得的是否是同一个实例
- spring每次getBean(),获得的是否是同一个实例
- spring每次getBean(),获得的是否是同一个实例
- spring每次getBean(),获得的是否是同一个实例
- Spring通过实现getBean方法获得Bean的实例
- spring学习(五)ClassPathXmlApplicationContext的getBean获取实例bean
- java_spring_bean的作用域_实例是否为单实例
- Spring在web应用中获得Bean的方法 实现getBean方法
- spring 动态获得bean实例
- Spring入门学习——调用一个实例工厂方法创建Bean
- 简单的用ApplicationContext做测试的话,获得Spring中定义的Bean实例
- 并发多线程环境下触发spring3的getBean的一个bug, bean的scope为prototype
- Spring在web应用中获得Bean的方法 实现getBean方法
- Spring ClassPathXmlApplicationContext getBean创建实例
- Spring怎样在一个bean里反复生成另一个bean的新实例
- Asp.Net MVC是否针对每次请求都重新创建一个控制器实例
- 获得一个程序的实例名柄
- ITCAST视频-Spring学习笔记(Spring的三种实例化Bean的方式)
- 一个获得计算机基本信息的类库使用的实例
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值