Announcement Announcement Module
Collapse
No announcement yet.
abstract class bean definition question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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, 11:06 AM.

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


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

          Comment


          • #6
            Your welcome. If it works please let us know your solution, always interested in nice solutions. Might be a learning experience.

            Comment


            • #7
              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.

              Comment

              Working...
              X