现在的位置: 首页 > 综合 > 正文

spring每次getBean(),获得的是否是同一个实例

2014年02月05日 ⁄ 综合 ⁄ 共 3496字 ⁄ 字号 评论关闭

spring 缺省:

  • 1.spring用DefaultListableBeanFactory.preInstantiateSingletons()建立bean实例
  • 2.缺省采用单例模式

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缺省是单例

 

 

 

改scope为多实例

<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()的都是不同的新实例

 

 

以此类推,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 是否是单例?
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

c3p0 datasource

        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, ... ]

 

 

使用单例的优点:只用一个实例,不需要重复new一个对象,浪费资源。
但是有没有缺点呢,有的话请说的详细一点,谢谢啦

 

 

单例的缺点,一般无法继承,需要做好同步以免多线程访问产生问题。详情请参见闫宏《Java与模式》,不再赘述。
这个怎么说呢。 看你是不是用容器管理你的JAVA BEAN了。 比如spring, 缺省情况下他自动就会为你实现单例。 也没有什么副作用。
如果不是容器管理, 那么可以写个工厂类来管理。 DAOFactory.getBean();
单例对于高性能的代码是比较重要。 我个人是不大倾向写代码单例的dao, service的。 代码不容易维护, 而且也很不雅光。
总之, 你可以管理,或者独立创建对象的单例, 但是不要把的DAO/SERVICE写成单例的。 不知道我说的, 你是不是明白了。 语言技巧不好~~~~~~~~~~~~
浪费创建时的资源,但最后会释放,总比你看见OOM异常要好吧?即使这些实例不会占很多内存。但还是有的。

 

抱歉!评论已关闭.