views:

100

answers:

2

The following is a utility class that is called by a LoginBean to add and get a User object from a session through FacesContext.

The application uses Spring. Should I use an annotation or is it an accepted practise to have static methods in this sort of class? If an annotation is advised, should I use @Component or @Service?

// Annotate as Service/Component?
public class WebUtils {

// Add user object to session
public void setUser( User user ){
    FacesContext context = FacesContext.getCurrentInstance();
    context.getExternalContext().getSessionMap().put( "user", user );
}

// Get user from session
public User getUser( FacesContext context ){
    if( context != null )
        return (User) context.getExternalContext().getSessionMap().get("user");

    return null;
}
+1  A: 

I like to have things like this as a Spring Bean so I can inject whatever I need on it.

Then again, if a static method works for you now, you can always make it a bean later when it is required.

Hans Westerbeek
+3  A: 

I would recommend you to create separate spring bean in session scope for your purposes.

You can call it UserSupport or UserController. This bean should have methods getUser (), setUser () and maybe some methods which make calls to your service layer.

Roman
Or `UserManager`. The other useful methods being `login()`, `logout()` and `isLoggedin()`.
BalusC