Announcement Announcement Module
Collapse
No announcement yet.
Injecting dynamic dependencies unspecifiable in the config Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting dynamic dependencies unspecifiable in the config

    Two part newbie question:

    I have a set of business logic beans dependent upon a Principal-like object stored in the HttpSession. When I'm retrieving the beans using Spring, I'll need to pass this object into Spring's BeanFactory somehow so it will return a bean applicable to my user's session context.

    1) Can this be done using AutowireCapableBeanFactory's applyBeanPropertyValues?

    2) If so, when I apply a Prinicpal-like object's properties, will I need to synchronize this process so if two different users request a business logic bean at the same time, the second user will have to wait until the first user's Principal object is applied and a business logic logic bean is returned to him before applying the second user's Principal?

  • #2
    Possible Soluion

    I think I may have found a way to handle this while avoiding synchronization issues. If all of this is obvious to a non-noob, feel free to ignore me.

    For all of my business logic beans dependent upon a principal-like User object, I had them implement an interface

    Code:
    interface UserSpecificBean {
      setUser(User user);
    }
    Then, I created a BeanPostProcessor

    Code:
    class SessionAwarePostProcessor {
       public SessionAwarePostProcessor(HttpSession session) {
         ... get the User object from the session
       }
       public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof UserSpecificBean) {
          ((UserSpecificBean)bean).setUser(user);
        }    
        return bean;
      }
    }
    and registered it when obtaining my WebApplicationContext

    Code:
    ConfigurableWebApplicationContext context = 
          (ConfigurableWebApplicationContext)WebApplicationContextUtils
          .getRequiredWebApplicationContext(session.getServletContext());
    ConfigurableBeanFactory factory = 
            (ConfigurableBeanFactory)context.getBeanFactory();    
    factory.addBeanPostProcessor(new SessionAwarePostProcessor (session));
    This way, I'm injecting the User-dependency into each bean when I retrieve it instead of into the entire BeanFactory.

    Comment

    Working...
    X