Mojarra 2.2.12
Here is a snippet of code that uses an instance of FacesContext :
FacesContext context = facesContextFactory.getFacesContext (servletConfig.getServletContext(), request, response, lifecycle);
The expression is perfectly clear. Upon receiving the request, we obtain global information and create an instance of FacesContext using it. Thus, an instance is created for each request. But getting the intonation of facesContextFactory seemed a lot more complicated to me.
// Acquire our FacesContextFactory instance try { facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory (FactoryFinder.FACES_CONTEXT_FACTORY); } catch (FacesException e) { //others }
Where
String javax.faces.FactoryFinder.FACES_CONTEXT_FACTORY = "javax.faces.context.FacesContextFactory"
JavaDocs for FactoryFinder describes the so-called
A standard discovery algorithm for all factory objects specified in the JavaServer Faces APIs.
That's what confused me.
Now, let's look at the actual method that creates the factory instance: javax.faces.FactoryFinderInstance # getFactory (String factoryName)
try { factoryOrList = factories.get(factoryName); if (!(factoryOrList instanceof List)) { return factoryOrList; } } finally { lock.readLock().unlock(); }
The factories field factories initialized as follows copyInjectionProviderFromFacesContext () :
private void copyInjectionProviderFromFacesContext() { InjectionProvider injectionProvider = null; FacesContext context = FacesContext.getCurrentInstance(); //USE FACES CONTEXT!!!!! if (null != context) { injectionProvider = (InjectionProvider) context.getAttributes().get("com.sun.faces.config.ConfigManager_INJECTION_PROVIDER_TASK"); } if (null != injectionProvider) { factories.put(INJECTION_PROVIDER_KEY, injectionProvider); } else { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, "Unable to obtain InjectionProvider from init time FacesContext. Does this container implement the Mojarra Injection SPI?"); } } }
So, we create an instance of FacesContext , but the factory itself is used to create FacesContext . Could you explain this cycle?