了解FacesContext实例化详细信息 [英] Understanding FacesContext instantiation details

查看:123
本文介绍了了解FacesContext实例化详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mojarra 2.2.12

Mojarra 2.2.12

以下是这段代码,这些代码代替了

Here is the piece of code taking over the instantiation of the FacesContext:

FacesContext context = facesContextFactory.getFacesContext
              (servletConfig.getServletContext(), request, response, lifecycle);

表达非常清楚.一旦获得请求,便从请求中获取全局信息,并使用该信息创建FacesContext实例.因此,将为每个请求创建实例.但是,在我看来,获得facesContextFactory的要领要困难得多.

The expression is perfectly clear. Once we get a request, we acquire a global information from it and create the FacesContext instance using it. So, the instance is created for each request. But obtaining the intance of facesContextFactory seemed to me much more tricky.

// Acquire our FacesContextFactory instance
try {
    facesContextFactory = (FacesContextFactory)
        FactoryFinder.getFactory
        (FactoryFinder.FACES_CONTEXT_FACTORY);
} catch (FacesException e) {
    //others
}

哪里

String javax.faces.FactoryFinder.FACES_CONTEXT_FACTORY = "javax.faces.context.FacesContextFactory"

FactoryFinder的JavaDocs描述了所谓的

JavaDocs for the FactoryFinder describes the so called

标准发现算法,用于在 JavaServer Faces API.

standard discovery algorithm for all factory objects specified in the JavaServer Faces APIs.

这就是我的困惑.

现在,让我们考虑创建工厂实例的实际方法:

Now, Let's consider the 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();
}

factories字段按以下方式初始化

The factories field is 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?");
        }
    }
}

因此,创建使用的FacesContext实例,但是工厂本身用于创建FacesContext.你不能解释那个周期吗?

So, creating used FacesContext instance, but the factory itself is used for creating the FacesContext. Couldn't you explain that cycle?

推荐答案

有一个特殊的"FacesContext左右.这种特殊的"init FacesContext"具有许多空/空/默认属性,尤其是那些依赖于HTTP Servlet请求/响应的属性,但是基于a.o的应用程序和配置相关的属性(例如通过FacesContext#getApplication()可用的属性)已经可用. faces-config.xml.

There's a special "init FacesContext" which is available during servlet container initialization, ensuring that there's at least "a" FacesContext around during JSF initialization. This special "init FacesContext" has a lot of empty/null/default properties, particularly the ones depending on HTTP servlet request/response, but application and configuration related properties such as ones available via FacesContext#getApplication() are already available based on a.o. faces-config.xml.

对于Mojarra,此"init FacesContext"为 com.sun.faces.config.FacesInitializer ,一个运行的 ServletContainerInitializer 实现在webapp启动期间.在那一刻,创建了工厂.

In case of Mojarra, this "init FacesContext" is created in a.o. the com.sun.faces.config.FacesInitializer, a ServletContainerInitializer implementation which runs during webapp startup. During that moment, factories are created.

这篇关于了解FacesContext实例化详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆