Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: bean reinstanciation upon request

  1. #11
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by al0 View Post
    Just small but complete test to prove my disbelief
    and now context (myContext.xml)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="B1"  class="B1" scope="prototype">
            <property name="bean2" ref="B2"/>
        </bean>
    
        <bean id="B2"  class="B2" scope="prototype">
            <property name="bean3" ref="B3"/>
        </bean>
    
        <bean id="B3"  class="B3" scope="prototype"/>
        
    </beans>
    and, just for completness, log4j.properties
    My applicationContext.xml has a different signature that yours.

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

  2. #12
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Can you try sample that I have posted in the previous mail?
    If it works as well for you, then check once more your configuration.

    And concerning final local variable - it has nothing to do with it. I (wild) guess that you do more then one change per try, so it may be not clear which change caused this ot that effect.

    Regards,

    Oleksandr

    Quote Originally Posted by DJViking View Post
    I have discovered my error. Because an earlier version of my program containted a final variable.

    final SomeService someService;
    ...
    someService = applicationContext.getBean("someService");

    But when I removed the final statement the output was not the same for two getBean instances.

    I don't completely understand it. Declaring my local variable as final should not give any trouble. Or?

    Edit: I rejoiced to soon. Problem sill ocour

  3. #13
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by DJViking View Post
    My applicationContext.xml has a different signature that yours.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    It is highly recommended to switch from DTD-based context to schema-based (look into the Spring reference), but, if for some reasons you can not do it, then some advanced capabilities of the Spring are not availbale, but it has absolutely no impact on discussed behavior.

    I just changed signature to your here is new output:
    Code:
    2007-07-25 16:30:15,916 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - <Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dd5b: display name [org.springframework.context.support.ClassPathXmlApplicationContext@dd5b]; startup date [Wed Jul 25 16:30:15 CEST 2007]; root of context hierarchy>
    2007-07-25 16:30:16,057 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - <Loading XML bean definitions from class path resource [myContext.xml]>
    2007-07-25 16:30:16,277 INFO [org.springframework.context.support.ClassPathXmlApplicationContext] - <Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@dd5b]: org.springframework.beans.factory.support.DefaultListableBeanFactory@337d0f>
    2007-07-25 16:30:16,308 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@337d0f: defining beans [B1,B2,B3]; root of factory hierarchy>
    Bean3 [2544881]
    Bean2 [23473608]
    First fetch of B1 [B1@147c5fc]
    Bean3 [18303751]
    Bean2 [4115088]
    Second fetch of B1 [B1@64dc11]
    As you can see nothing is changed - all bean instances are still different.

    Regards,
    Oleksandr

  4. #14
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    I just created a simple example with one class and defined it in application context as prototype. Output was different for each time..


    However. I can not see any difference from my other application in the definition of my prototype bean.

  5. #15
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by DJViking View Post
    My code goes something like this.

    Code:
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
        SomeService someService = (SomeService) applicationContext.getBean("someService");
        SomeService someService2 = (SomeService) applicationContext.getBean("someService");
        
        System.out.println("Equal?: " + (someService == someService2));
        System.out.println(System.identityHashCode("Bean1 Hash: " + someService));
        System.out.println(System.identityHashCode("Bean2 Hash: " + someService2));
    Red lines are incorrect, they should be
    Code:
    System.out.println("Bean1 Hash: " + System.identityHashCode(someService));
        System.out.println("Bean2 Hash: " + System.identityHashCode(someService2));
    Code:
          <bean id="someService" class="SomeServiceImpl" scope="prototype">
                <property name="someUtilities" ref="someUtilities" />
          </bean>
    
          <bean id="someUtilities" class="SomeUtilities" scope="prototype">
                <property name="someUtilities" ref="someUtilities" />
          </bean>
    
          <bean id="someTypeUtilities" class="SomeTypeUtilities" scope="prototype">
                <constructor-arg ref="jdbcTemplate" />
          </bean>
    And the output is the same for both instances.
    And here someUtilities refrers to itself (circular reference, which is not allowed).

  6. #16
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by DJViking View Post
    I just created a simple example with one class and defined it in application context as prototype. Output was different for each time..


    However. I can not see any difference from my other application in the definition of my prototype bean.
    So ask somebody to look over your complete configuration and code

  7. #17
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by al0 View Post
    And here someUtilities refrers to itself (circular reference, which is not allowed).
    Those errors was simply a type error
    Code:
    System.out.println(System.identityHashCode("Bean1 Hash: " + someService));
        System.out.println(System.identityHashCode("Bean2 Hash: " + someService2));
    Should be
    Code:
    System.out.println("Bean1 Hash: " + System.identityHashCode(someService));
        System.out.println("Bean2 Hash: " + System.identityHashCode(someService2));
    While
    Code:
          <bean id="someUtilities" class="SomeUtilities" scope="prototype">
                <property name="someUtilities" ref="someUtilities" />
          </bean>
    Should be
    Code:
          <bean id="someUtilities" class="SomeUtilities" scope="prototype">
                <property name="someUtilities" ref="someTypeUtilities" />
          </bean>
    I have used pseudonyms for my actual code and by doing so I got some type errors.
    Last edited by DJViking; Jul 25th, 2007 at 12:41 PM.

  8. #18
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by DJViking View Post
    Those errors was simply a type error
    ...
    I have used pseudonyms for my actual code and by doing so I got some type errors.
    I have thought so, but it is hard to give some definite suggestion when instead a real code some mess is present. It is absolutely Ok not to reveal a real code, but coud you prepare some working example that exhibits the claimed behavior? It may be posted as an attachment to the message.

    BTW, I'm quite sure that ifyou would try to prepare such example by "stripping out" your code and configuration step-by-step, then in this process you will find cause of the problem

    Regards,
    Oleksandr

  9. #19
    Join Date
    Jun 2007
    Location
    Oslo, Norway
    Posts
    153

    Default

    Quote Originally Posted by al0 View Post
    I have thought so, but it is hard to give some definite suggestion when instead a real code some mess is present. It is absolutely Ok not to reveal a real code, but coud you prepare some working example that exhibits the claimed behavior? It may be posted as an attachment to the message.

    BTW, I'm quite sure that ifyou would try to prepare such example by "stripping out" your code and configuration step-by-step, then in this process you will find cause of the problem

    Regards,
    Oleksandr
    This is frustrating. I took and copied my prototype beans over in a test application with only 5 beans. The code was identical and it worked.

    My testapplication was a simple runtime application, while the other is a web application. This shouldn't have any bearing on the result.

    Edit: I increased the context xml in the small test application to include the complete context xml I use in the web application. And still the prototype bean work as it should. So I can see no fault with the Spring configuration.

    Do I need to use a WebApplicationContext, even if I don't use Spring MVC?
    Last edited by DJViking; Jul 26th, 2007 at 03:10 AM.

  10. #20
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote Originally Posted by DJViking View Post
    This is frustrating. I took and copied my prototype beans over in a test application with only 5 beans. The code was identical and it worked.

    My testapplication was a simple runtime application, while the other is a web application. This shouldn't have any bearing on the result.
    What is frustrating - my advice or the whole situation

    Concerning test application and real application - they, very likely , differs in a way how they load context. It is quite possible that in web application you load not that context that you mean to load (very typical error if you would look in the threads in this forum), especially if you have complicated context structure (e. g. multiply context files).

    Try to make copy of your real application and strip it out step-by-step till you find a problem or obtain example that you may reveal.

    Regards,
    Oleksandr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •