Announcement Announcement Module
Collapse
No announcement yet.
lazy-init issue Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • lazy-init issue

    Hi,
    I have following beanRefContext.xml with lazy-init=true.I refer this from ejb1 & ejb2,


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <bean id="test1" lazy-init="true" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
    <constructor-arg>
    <list>
    <value>Db1.xml</value>
    <value>DAO1.xml</value>
    </list>
    </constructor-arg>
    </bean>

    <bean id="test2" lazy-init="true" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
    <constructor-arg>
    <list>
    <value>DB2.xml</value>
    <value>DAO2.xml</value>
    </list>
    </constructor-arg>
    </bean>
    </beans>

    works fine with spring 1.2.6. if i refer bean reference "test1" from ejb1, since test2 is lazy-init, the db2.xml resource reference is not loaded.

    I am trying to convert it to spring 2.0 with updating the jars and moving to new schema based xml file.The same definition breaks, as test2 is eagerly loaded, which i don't want as i have not referenced it from my ejb1, when i convert the xml file from dtd based to spring 2.0 schema based as shown below, as lazy-init is not valid. Any idea how to make it work.

    Thnx,
    India

  • #2
    If you refer to a lazy init bean from one that isn't then its going to initialize. Would you happen to be doing that? Is it possible to see all of the configuration?

    One thing to understand about lazy-initialization is that even though a bean definition may be marked up as being lazy-initialized, if the lazy-initialized bean is the dependency of a singleton bean that is not lazy-initialized, when the ApplicationContext is eagerly pre-instantiating the singleton, it will (of course) have to satisfy all of the singletons dependencies, one of which will be the lazy-initialized bean! So don't be confused if the IoC container creates one of the beans that you have explicitly configured as lazy-initialized at startup; all that means is that the lazy-initialized bean probably is being injected into a non-lazy-initialized singleton bean elsewhere in your configuration.
    http://www.springframework.org/docs/...tory-lazy-init

    BTW, [ code] [ /code] tags make it sooooo much easier to read.

    Comment


    • #3
      Actually below is my other files,
      acutally db1.xml & db2.xml are the same file even though for example i showed it as different file.

      db.xml

      <bean id="db1" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryB ean">
      <property name="jndiName"><value>jdbc/db1</value></property>
      <property name="resourceRef"><value>true</value></property>
      </bean>

      <bean id="db2" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryB ean">
      <property name="jndiName"><value>jdbc/db2</value></property>
      <property name="resourceRef"><value>true</value></property>
      </bean>

      my dao1 is injected with db1 and dao2 injected with db2.
      ejb1 --> uses dao1
      ejb2 --> uses dao2

      i use ContextSingletonBeanFactoryLocator,
      ejb1 does setBeanFactoryLocatorKey("test1") -> getBean(dao1)
      ejb2 does setBeanFactoryLocatorKey("tes21") -> getBean(dao2)

      The whole thing works fine with old dtd definition as i have shown before with spring 1.2.6.
      Its only when i used schema based definition along with 2.0 it breaks as shown below. Also dtd definition with 2.0 works fine.


      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
      <beans>
      <bean id="test1" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
      <constructor-arg>
      <list>
      <value>Db1.xml</value>
      <value>DAO1.xml</value>
      </list>
      </constructor-arg>
      </bean>

      <bean id="test2" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
      <constructor-arg>
      <list>
      <value>DB2.xml</value>
      <value>DAO2.xml</value>
      </list>
      </constructor-arg>
      </bean>
      </beans>

      Comment


      • #4
        Hello,

        don't know if this may be a problem but for JndiObjectFactoryBean you should use "lookupOnStartup" and specify the "proxyInterface" or the lookup will be performed on startup (see http://static.springframework.org/sp...ocs/2.0.x/api/)

        -Patrick

        Comment


        • #5
          Code:
          <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:jee="http://www.springframework.org/schema/jee"
          xmlns:util="http://www.springframework.org/schema/util"
          xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd
          http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
          <beans>
            <bean id="test1" class="org.springframework.context.support.ClassPathXmlApplicationContext">
              <constructor-arg>
                <list>
                  <value>Db1.xml</value>
                  <value>DAO1.xml</value>
                </list>
              </constructor-arg>
            </bean>
          
            <bean id="test2" class="org.springframework.context.support.ClassPathXmlApplicationContext">
              <constructor-arg>
                <list>
                  <value>DB2.xml</value>
                  <value>DAO2.xml</value>
                </list>
              </constructor-arg>
            </bean>
          </beans>
          In your configuration there is no mention of lazy-init thus the beans get instantiated.

          Comment


          • #6
            As Marten said, the lazy init attribute isn't there. The first example contained it, the second didn't. What is it that is supposed to be lazy inited? Is it the beans test1 and test2 or the JndiObjectFactoryBeans db1 and db2?

            Comment


            • #7
              Hi,
              The lazy-init is there, it was mistake, i missed it when i sent to the forum.

              Thnx

              Comment


              • #8
                Hi,
                It works fine, if the files are as shown below for beanRefContext & db.xml.
                See the 'default-lazy-init="true"' in beanRefContext.xml.

                -----------beanRefContext.xml------------------------
                <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jee="http://www.springframework.org/schema/jee"
                xmlns:util="http://www.springframework.org/schema/util"
                xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
                default-lazy-init="true">
                <beans>
                <bean id="test1" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
                <constructor-arg>
                <list>
                <value>DB.xml</value>
                <value>DAO1.xml</value>
                </list>
                </constructor-arg>
                </bean>

                <bean id="test2" class="org.springframework.context.support.ClassPa thXmlApplicationContext">
                <constructor-arg>
                <list>
                <value>DB.xml</value>
                <value>DAO2.xml</value>
                </list>
                </constructor-arg>
                </bean>
                </beans>


                -------db.xml-------------

                <beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:jee="http://www.springframework.org/schema/jee"
                xmlns:util="http://www.springframework.org/schema/util"
                xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

                <bean id="ds1" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryB ean">
                <property name="jndiName"><value>jdbc/ds1</value></property>
                <property name="resourceRef"><value>true</value></property>
                </bean>

                <bean id="ds2" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryB ean">
                <property name="jndiName"><value>jdbc/ds2</value></property>
                <property name="resourceRef"><value>true</value></property>
                </bean>
                </beans>


                Patrick, i think u are right, even i had my inital doubt on that.
                BUT when i change the db.xml to the following as shown below, it loads all the
                jndi lookup. I known i can achieve lazy-init by giving lookupOnStartup=false.
                In that case, should i specify proxyInterface as javax.sql.DataSource.
                In that case, i am not very comfortable as i don't want specify javax.sql.DataSource in my xml file.Is there any other way to achieve lazy init
                for jndi-lookups.

                <?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:jee="http://www.springframework.org/schema/jee"
                xmlns:util="http://www.springframework.org/schema/util"
                xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.0.xsd
                http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.0.xsd
                http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

                <jee:jndi-lookup id="ds1"
                resource-ref="true"
                jndi-name="jdbc/ds1">
                </jee:jndi-lookup>

                <jee:jndi-lookup id="oem"
                resource-ref="true"
                jndi-name="jdbc/ds2">
                </jee:jndi-lookup>

                </beans>



                Thnx

                Comment


                • #9
                  Hello,

                  Patrick, i think u are right, even i had my inital doubt on that.
                  BUT when i change the db.xml to the following as shown below, it loads all the
                  jndi lookup. I known i can achieve lazy-init by giving lookupOnStartup=false.
                  In that case, should i specify proxyInterface as javax.sql.DataSource.
                  In that case, i am not very comfortable as i don't want specify javax.sql.DataSource in my xml file.Is there any other way to achieve lazy init
                  for jndi-lookups.
                  In fact it is not realy lazy-init, with the lookupOnStartup property the bean is eagerly initialized but instead of doing the jndi lookup a proxy is created that will do the lookup lazily (and that's why you have to specify the DataSource interface for Spring to know which interface the Proxy has to implement...)


                  I think if you set the lazy-init property to true, the bean should be lazy-initialized as soon as it is required (as soon as a bean requiring it is initialized...)

                  I don't know you to specify that with the jee namespace ???

                  -Patrick

                  Comment


                  • #10
                    Hi Patrick,

                    with the old dtd way, if u give like this it works fine. It does a lazy init even
                    without lookOnstartup thing or proxyInterface stuff.

                    <bean id="ds1" lazy-init="true" class="org.springframework.jndi.JndiObjectFactoryB ean">
                    <property name="jndiName"><value>jdbc/ds1</value></property>
                    <property name="resourceRef"><value>true</value></property>
                    </bean>

                    for the new schema based, if u give it as below it works fine,

                    <jee:jndi-lookup id="ds1"
                    resource-ref="true"
                    cache="true"
                    proxy-interface="javax.sql.DataSource"
                    lookup-on-startup="false"
                    jndi-name="jdbc/ds1">
                    </jee:jndi-lookup>



                    india

                    Comment

                    Working...
                    X