首页 > JAVA框架 > myfaces的初始化是如何进行的

myfaces的初始化是如何进行的

2009年5月1日

在看myfaces源码的时候,一直不明白javax.faces.webapp.FacesServlet中有一段

  1. public void init(ServletConfig servletConfig)
  2.             throws ServletException
  3.     {
  4.         if(log.isTraceEnabled()) log.trace("init begin");
  5.         _servletConfig = servletConfig;
  6.         _facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
  7.         //TODO: null-check for Weblogic, that tries to initialize Servlet before ContextListener
  8.  
  9.         //Javadoc says: Lifecycle instance is shared across multiple simultaneous requests, it must be implemented in a thread-safe manner.
  10.         //So we can acquire it here once:
  11.         LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
  12.         _lifecycle = lifecycleFactory.getLifecycle(getLifecycleId());
  13.         if(log.isTraceEnabled()) log.trace("init end");
  14.     }

其中的FactoryFinder.getFactory是这样一段代码

  1. public static Object getFactory(String factoryName)
  2.             throws FacesException
  3.     {
  4.         if(factoryName == null)
  5.             throw new NullPointerException("factoryName may not be null");
  6.  
  7.         ClassLoader classLoader = getClassLoader();
  8.  
  9.         //This code must be synchronized because this could cause a problem when
  10.         //using update feature each time of myfaces (org.apache.myfaces.CONFIG_REFRESH_PERIOD)
  11.         //In this moment, a concurrency problem could happen
  12.         Map factoryClassNames = null;
  13.         Map<String, Object> factoryMap = null;
  14.        
  15.         synchronized(_registeredFactoryNames)
  16.         {
  17.             factoryClassNames = _registeredFactoryNames.get(classLoader);
  18.  
  19.             if (factoryClassNames == null)
  20.             {
  21.                 throw new IllegalStateException(message);
  22.             }

注意其中的 _registeredFactoryNames.get(classLoader),说明在javax.faces.webapp.FacesServlet之前_registeredFactoryNames已经构造好了,可是我查web.xml的时候,根本找不到其它类了,到底谁去初始化了_registeredFactoryNames呢?

其实是 WEB-INF/lib/myfaces-impl-1.2.6.jar 这个引入的包起了作用,这里面有2个.tld文件,tomcat等j2ee容器会扫描所有的classpath,遇到.tld文件,也会去里面执行的。在myfaces_core.tld中,除了定义了一些f:开头标签以外,就有一段监听器代码

  1. <listener>
  2.   <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
  3. </listener>

这才是整个myfaces加载的爆炸点。以前struts的爆炸点都是在web.xml定义的servlet中的,这次放在了jar包里面,正是有点不习惯,要是仅仅想用里面的一些功能,还非得加载这个玩意了,呵呵。

JAVA框架

  1. 目前还没有任何评论.
  1. 目前还没有任何 trackbacks 和 pingbacks.