yegor256
yegor256

Reputation: 105043

How to inject managed bean into FacesContext?

How can I replace an @ApplicationScoped managed bean with my own copy of it in FacesContext? All I have is an instance of FacesContext (I'm inside JSFUnit).

Upvotes: 3

Views: 3149

Answers (1)

BalusC
BalusC

Reputation: 1108537

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.

Upvotes: 4

Related Questions