Results 1 to 4 of 4

Thread: Different bean reference been returned based on runtime attr

Hybrid View

  1. #1
    Join Date
    Aug 2005
    Posts
    12

    Default 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... Let´s 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();

    I´ve 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!
    Eduardo Rodrigues
    Belo Horizonte - MG - Brazil

  2. #2
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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 at 10:39 AM.

  3. #3
    Join Date
    Aug 2004
    Location
    Vancouver, Canada
    Posts
    25

    Default

    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();

  4. #4
    Join Date
    Aug 2005
    Posts
    12

    Default

    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, I´ll have to repeat the same bean with different names...

    Thank you very much!
    Eduardo Rodrigues
    Belo Horizonte - MG - Brazil

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. could not satisfy dependencies
    By springuser in forum Container
    Replies: 4
    Last Post: Apr 26th, 2005, 01:15 PM
  5. Replies: 1
    Last Post: Apr 25th, 2005, 07:37 PM

Posting Permissions

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