2011-05-06 106 views
3

我希望爲少數託管bean創建超類,其中包含注入的@SessionScoped託管bean和@EJB。以下作品託管Bean繼承

超類:

@ManagedBean 
@ViewScoped 
public abstract class UserPageController implements Serializable { 
    private static final long serialVersionUID = -6118448960678133445L; 

    @ManagedProperty(value = "#{sessionController}") 
    protected SessionController sessionController; 

    private User user; 
    private String userId; 
    @EJB protected UserBean userBean; 

    public UserPageController() { 
    } 

    public SessionController getSessionController() { 
     return sessionController; 
    } 

    public void setSessionController(SessionController sessionController) { 
     this.sessionController = sessionController; 
    } 

    //-------------------------------- User ------------------------------------ 
    public String getUserId() { 
     return userId; 
    } 

    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    public User getUser() { 
     if (user == null) { 
      if (getUserId() != null && sessionController.getLoggedInUser().getRole().isManager()) { 
       User dbUser = userBean.findUser(Long.parseLong(userId)); 
       if (dbUser != null) { 
        setUser(dbUser); 
       } 
      } else { 
       setUser(userBean.findUser(sessionController.getLoggedInUser().getId())); 
      } 
     } 
     return user; 
    } 

    public void setUser(User user) { 
     this.user = user; 
    } 

} 

用法:

@ManagedBean 
@ViewScoped 
public class UserCompetenceController extends UserPageController implements Serializable { 
    // Stuff that uses sessionController & userBean. 
} 

有什麼辦法避免註釋抽象超類爲管理bean?它不知何故感覺不對,但沒有它,依賴注入將無法工作。

回答

0

我會說,即使你最終得到一個繼承它的基類的字段的類,注入也是爲這個類單獨完成的,這意味着爲了工作,基類也必須註明 - 但這只是一個猜測。

您是否嘗試過使用CDI的相同構造?