Results 1 to 6 of 6

Thread: spring not able to resolve constructor

  1. #1
    Join Date
    Feb 2010
    Posts
    2

    Default spring not able to resolve constructor

    I am sure this issue would be similar to one on some other thread posted. Just point me to that thread if any.

    Query: I have a class that can be constructed by two ways-

    class MyClass {

    /*1st constructor*/
    MyClass() {} //Creates a new instance

    /*2nd constructor*/
    MyClass(ResultSet rs) {} //ResultSet object holds row from database; This way MyClass can be created from its existing Db entry

    }

    Following is my bean defination to load it.

    bean.xml :

    <bean id="myClass" class="com.package.MyClass"
    scope="prototype"/>

    and following are the two calls:

    1st call
    m_ctx.getBean("myClass", null); // intended to call 1st constructor

    2nd call
    m_ctx.getBean("myClass", new Object[] {rs}); // intended to call 2nd constructor

    // m_ctx is instance of org.springframework.context.ApplicationContext


    I can load 'myClass' instance from db by calling 2nd call or I can create a new instance of 'myClass' by making 1st call, but....

    If i create a new instance of 'myClass' and load existing 'myClass' with same ApplicationContext, m_ctx, i am not able to...

    Ill explain this..

    ...
    //somewhere in the code
    MyClass m1 = m_ctx.getBean("myClass", null); // create a new 'myClass'
    ...
    // in same code with same instance of m_ctx
    MyClass m2 = m_ctx.getBean("myClass", new Object[] {rs}); // load 'myClass' from rs

    This call is not able to resolve constructor correctly and it calls 1st constructor instead 2nd. This 1st constructor was found cached bean definition of 'myClass', and during 2nd bean call it returned the cached constructor instead of re-resolving the constructor.

    All this is happening in org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Stri ng beanName, RootBeanDefinition mbd, Object[] args) call


    If above explanation is making any sense then please let me know what is going wrong here?

  2. #2
    Join Date
    Jun 2009
    Posts
    190

    Default

    Within the same application context, getBean will return you the same instance for a prototype bean unless there is a new request.

    -Hetal

  3. #3
    Join Date
    Feb 2010
    Posts
    2

    Default

    Quote Originally Posted by Hetal B View Post
    Within the same application context, getBean will return you the same instance for a prototype bean unless there is a new request.

    -Hetal
    Is there no way to override this so that for each request it goes and resolves the constructor. Cannot re initialize application context, its part of framework that i am working and is implemented as singleton.

  4. #4
    Join Date
    Jun 2009
    Posts
    190

    Default

    For each request for a prototype bean it will resolve the construtor but as per your code mentioned in your first post:-

    //somewhere in the code
    MyClass m1 = m_ctx.getBean("myClass", null); // create a new 'myClass'
    ...
    // in same code with same instance of m_ctx
    MyClass m2 = m_ctx.getBean("myClass", new Object[] {rs}); // load 'myClass' from rs

    Also, why are you autowiring your bean?

    Autowiring will pick up byType, byName etc hence picks up the bean from the application context.

    -Hetal

  5. #5
    Join Date
    Jun 2009
    Posts
    190

    Default

    PLs post in your config file and also the code snippet which is creating a prob. Specially mention the autowiring implementation.

    -Hetal

  6. #6
    Join Date
    Jun 2009
    Posts
    190

    Default TRied sample code

    Hi,

    I tried this example , the code is as below:-

    <Code>
    Application context*************

    <bean id="SimpleBean" autowire="byType" singleton="false" class="test.SimpleBean"/>

    SimpleBean class***********
    public SimpleBean(){
    System.out.println("Const without arg");
    }

    public SimpleBean(String str){
    System.out.println("With arg.....");
    }

    TEst class*******
    ctx.getBean("SimpleBean");
    ctx.getBean("SimpleBean", new Object[]{"test1"});


    </code>

    I am using Spring 3.0.1 release and it works fine..... unless there is something that you wish to point out due to which your code is not working.

    -Hetal

Tags for this Thread

Posting Permissions

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