How to inject managed bean into FacesContext?
How can I replace an @ApplicationScoped
managed开发者_Python百科 bean with my own copy of it in FacesContext
? All I have is an instance of FacesContext
(I'm inside JSFUnit).
Application scoped beans are stored in the application map with the managed bean name as key.
So, this should do:
FacesContext.getCurrentInstance().getExternalContext()
.getApplicationMap().put("managedBeanName", new Bean());
By the way, deeper under the JSF covers up to the Servlet API, the application map is just a mapping of ServletContext
attributes. Useful to know when it happens that you've only the ServletContext
at your hands. And in the same line, the session map maps to HttpSession
attributes and the request map to HttpServletRequest
attributes. Use them for session and request scoped beans respectively.
精彩评论