lazy-init实际上是否像宣传的那样工作?

时间:2014-06-25 10:54:36

标签: spring

根据文档http://docs.spring.io/spring-framework/docs/3.2.2.RELEASE/spring-framework-reference/html/beans.html#beans-factory-lazy-init,以下内容不应该是MyDatabase,但我可以清楚地看到调试afterPropertiesSet类的HibernateDatabase方法时就是这种情况。如果我删除MyDatabaseEntityManagerFactory,则不会发生这种情况。但是,考虑到MyDatabaseEntityManagerFactory也是懒惰的,这应该不重要 - 它不应该导致MyDatabase初始化。

要么我误解了文档中那些说引用另一个懒豆的懒惰的bean不应该导致它初始化或者Spring中存在根本问题的部分。任何人都可以对此有所了解,或者可能建议替代我想达到的目标吗?

感谢。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
    default-lazy-init="false">
    <bean id="HibernateDatabase" class="mypackage.database.HibernateDatabase"
        abstract="true" />

    <bean id="MyDatabase" parent="HibernateDatabase" lazy-init="true">
        <property name="driver" value="com.sybase.jdbc3.jdbc.SybDriver" />
        <property name="dialect" value="org.hibernate.dialect.SybaseDialect" />
        <property name="url" value="${My.dburl}" />
        <property name="user" value="${My.dbuser}" />
        <property name="password" value="${My.dbpassword}" />
    </bean>

    <bean id="EntityManagerFactory" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
        abstract="true">
        <property name="targetMethod" value="getEntityManagerFactory" />
    </bean>

    <bean id="MyDatabaseEntityManagerFactory" parent="EntityManagerFactory" lazy-init="true">
        <property name="targetObject" ref="MyDatabase" />
    </bean>
</beans>

0 个答案:

没有答案