Announcement Announcement Module
Collapse
No announcement yet.
Different bean reference been returned based on runtime attr Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Different bean reference been returned based on runtime attr

    Hi guys...

    See the following code:

    Code:
    public class User{
    
        String login;
        String center;
        //gets and sets
    }
    
    public abstract class MyBean{
        abstract void doBusiness();
    }
    
    public class MyBeanToCenterX extends MyBean{
       void doBusiness(){
          //Do the business according to rules of center X 
       }
    }
    
    public class MyBeanToCenterY extends MyBean{
       void doBusiness(){
          //Do the business according to rules of center Y 
       }
    }


    Ok, I have a requirement that a determined subclass of MyBean must be used according to the value of the "center" attribute of a User instance... Lets say that the center X has a business rule and the center Y has another rule... The same runtime (deployment) will be used by users from centers X and Y, the decision must be on runtime... A custom security system will set the value of the "center" attribute, previously stored on a database....

    I would like to do somethinhlike this:

    Code:
    User user = //get the user
    MyBean bean = context.getBean("myBean",user.getCenter());
    bean.doBusiness();

    Ive already a solution using an adapter to the context ("MyContext") and naming the concrete beans as "myBeanX" and "myBeanY" on the xml... Then "MyContext" will use Spring context appending the center to the name of the bean...

    Spring has any standard feature to do this? The problem is simple as to getting a different instance based on runtime values...

    Thank you very much!

  • #2
    There have been many discussions on this forum on this topic. Spring can provide that feature, but I'm not sure there is a recommended general 'idiom' or pattern yet. ???

    One thread for example: http://forum.springframework.org/showthread.php?t=14421

    Another is: http://forum.springframework.org/showthread.php?t=12180

    ---
    J. Betancourt
    Last edited by robyn; May 14th, 2006, 11:39 AM.

    Comment


    • #3
      You should be able to use a naming convention for the beans (e.g. "myBeanX" and "myBeanY")

      Code:
      User user = //get the user 
      MyBean bean = (MyBean) context.getBean("myBean"+user.getCenter()); 
      bean.doBusiness();

      Comment


      • #4
        Unfortunately this is not possible...
        Two different center can have the same business rule... If I only append the center to the name of the bean, Ill have to repeat the same bean with different names...

        Thank you very much!

        Comment

        Working...
        X