Results 1 to 7 of 7

Thread: abstract class bean definition question

  1. #1
    Join Date
    Jul 2006
    Posts
    109

    Default abstract class bean definition question

    Hi, I was wondering if following thing is possible:

    Let A be a class mapped as abstract in app-context.xml.

    Let BlahBlahServiceImpl is class - service mapped in app-context.xml as "regular" bean. Service BlahBlahServiceImpl has property of class A lets call it aa.

    B and C are subclasses of class A, NOT managed by Spring (not in app-context.xml).

    Is it possible in BlahBlahServiceImpl to do something like this:

    Code:
    if(...)
       aa = new B();
    else
       aa = new C();
    and in in both ways to inject that inherited subobject of class A to class B and C.

    I hope my question is clear.

    Thanks in advance.
    Regards.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm not quite sure what you are trying to do. If you are creating the beans via the new operator however these aren't going to be Spring managed so I'm not quite sure where the injection bit comes into it.
    Last edited by karldmoore; Aug 29th, 2007 at 11:06 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    Jul 2006
    Posts
    109

    Default

    Ok, let simplify (or not ).

    A class has a few DAO's. A class has many subclasses which are using those DAO's. But new instances of subclasses of class A are created based on some condition.
    Code:
    if(...)
       aa = new B();
    else if(...)
       aa = new C();
    else
    aa = new D();
    I want to inject all Dao's that A have in subclass which is created, so I have thought that somehowe I can create object of class B with operator new, and somehow to force injecting of DAO's mapped as properties of class A in app-context.xml.

    Or maybe there's some better way. Maybe to explicitly get bean from app-context in code and via set method set it to object of class B.
    Thanks for reply.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Why not define all of the different instances of A as prototype beans and instead of creating them let Spring do that for you.

    Code:
    if(...)
       aa = applicationtContext.getBean("instanceB");
    else if(...)
       aa = applicationContext.getBean("instanceC");
    else
    aa = applicationContext.getBean("instanceD");
    That way I guess you have the best of both worlds.

    You could even hide this implementation behind a FactoryBean and simply inject that into your service, that way your service stays clean of the logic of deciding which bean to use and you even the possibility to reuse the logic.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2006
    Posts
    109

    Default

    That's what I need.
    Thanks mdeinum, thanks a lot.

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Your welcome. If it works please let us know your solution, always interested in nice solutions. Might be a learning experience.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  7. #7
    Join Date
    Jun 2007
    Location
    New York
    Posts
    1

    Default

    I am looking to do something similar. I have two DAOs one base class(baseDAO) and other its sub class(tmplDAO). The above solution will work. I want to do something more than this. In above solution you have to choose which instance you want to use(Hence hard coding the instance). In my case I have multiple classes which are using baseDAO. One class(say XXX) needs tmplDAO. I dont want to change the bean def on XXX and it should still get baseDAO in definition. But some other configuration(in app-ctx.xml) using inheritance in beand def or any other spring feature, that XXX should get instance of tmplDAO and other classes shud get baseDAO.
    Hope I am able to explain the problem.

Posting Permissions

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