Q: JSF : How to update one session bean base on another session bean? I am currently modifying some jsf application. I have two beans.


*

*connectionBean

*UIBean


When I set my connection parameters in connectionBean the first time, the UIBean is able to read my connectionBean information and display the correct UI Tree.
However when I try to set the connection parameters in the same session. My UIBean will still use the previous connectionBean information.
It only will use after I invalidate the whole httpSession.
Is there anyway I can make one session bean update another session bean?

A: Sounds to me like it's some kind of problem with UIBean referencing an out-of-date version of ConnectionBean. This is one problem with JSF - if you re-create a bean, JSF will not update the references in all your other beans.
You could try getting a 'fresh' copy of the ConnectionBean each time. The following method will retrieve a backing bean by name:
protected Object getBackingBean( String name )
{
    FacesContext context = FacesContext.getCurrentInstance();

    return context
            .getApplication().createValueBinding( String.format( "#{%s}", name ) ).getValue( context );
}

Without knowing the specifics of your code and how you're using the beans it's difficult to be more specific!

A: @Phill Sacre 
getApplication().createValueBinding is now deprecated.  Use this function instead for JSF 1.2. To get a fresh copy of the bean.
protected Object getBackingBean( String name )
{
    FacesContext context = FacesContext.getCurrentInstance();

    Application app = context.getApplication();

    ValueExpression expression = app.getExpressionFactory().createValueExpression(context.getELContext(),
            String.format("#{%s}", name), Object.class);

    return expression.getValue(context.getELContext());
}


A: Define constant and static method in first session bean:
public class FirstBean {

public static final String MANAGED_BEAN_NAME="firstBean";

/**
 * @return current managed bean instance
 */
public static FirstBean getCurrentInstance()
{
  FacesContext context = FacesContext.getCurrentInstance();
  return (FirstBean) context.getApplication().evaluateExpressionGet(context, "#{" + FirstBean.MANAGED_BEAN_NAME + "}", TreeBean.class);
}  
...

than use in second session bean like this:
...  
FirstBean firstBean = FirstBean.getCurrentInstance();  
...

Better approach would be to use some Dependency Injection framework like JSF 2 or Spring.
