【转】 Struts2源代码分析(一)配置文件加载

(转自:http://blog.csdn.net/zhangzhaokun/article/details/5064843

 

       一直以来我都想找个时间好好研究一下Struts2的源代码,彻底弄清楚它的配置文件是如何加载和解析的?Action对象是如何创建的?属性等又是如何获取和注入的?拦截器和拦截器栈是如何实现的?       

       任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载。
通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于Struts 2框架而言,需要加载FilterDispatcher,只要Web应用负责加载FilterDispatcher,FilterDispatcher将会加载应用的Struts 2框架。因为Struts 2将核心控制器设计成Filter,而不是一个普通Servlet。故为了让Web应用加载FilterDispacher,只需要在web.xml文件中配置FilterDispatcher即可。

       在web.xml文件中可以如下方式配置Struts2:      

  1. <!-- 配置Struts 2框架的核心Filter -->  
  2. <filter>  
  3.      <!-- 配置Struts 2核心Filter的名字 -->  
  4.      <filter-name>struts</filter-name>  
  5.        
  6.      <!-- 配置Struts 2核心Filter的实现类 -->  
  7.      <filter-class>  
  8.            org.apache.struts2.dispatcher.FilterDispatcher  
  9.      </filter-class>  
  10.        
  11.      <init-param>  
  12.            <!-- 配置Struts 2的配置文件 -->  
  13.            <param-name>config</param-name>  
  14.            <param-value>  
  15.                 mystruts2.xml  
  16.            </param-value>  
  17.      </init-param>  
  18.        
  19.      <init-param>  
  20.            <!-- 配置Struts 2框架默认加载的Action包结构 -->  
  21.            <param-name>actionPackages</param-name>  
  22.            <param-value>  
  23.                 com.struts2.test.web.action  
  24.            </param-value>  
  25.      </init-param>  
  26.   
  27.      <!-- 配置Struts 2框架的配置提供者类 -->  
  28.      <init-param>  
  29.            <param-name>configProviders</param-name>  
  30.            <param-value>com.struts2.test.web.MyConfigurationProvider</param-value>  
  31.      </init-param>  
  32.        
  33.      <!-- 配置Struts 2框架的常量 -->  
  34.      <init-param>  
  35.            <param-name>truts.enable.DynamicMethodInvocation</param-name>  
  36.           <param-value>false</param-value>  
  37.      </init-param>  
  38.      <init-param>  
  39.            <param-name>struts.devMode</param-name>  
  40.           <param-value>false</param-value>  
  41.      </init-param>  
  42.      <init-param>  
  43.            <param-name>struts.objectFactory</param-name>  
  44.           <param-value>spring</param-value>  
  45.      </init-param>  
  46. </filter>  
  47.   
  48. <filter-mapping>  
  49.     <!-- 配置Struts 2的核心FilterDispatcher拦截所有用户请求 -->  
  50.     <filter-name>struts</filter-name>  
  51.     <url-pattern>/*</url-pattern>  
  52. </filter-mapping>  

 

      正如上面看到的,当配置Struts 2的FilterDispatcher类时,可以指定一系列的初始化参数,为该Filter配置初始化参数时,其中有3个初始化参数有特殊意义:
      config:该参数的值是一个以英文逗号(,)隔开的字符串,每个字符串都是一个XML配置文件的位置。Struts 2框架将自动加载该属性指定的系列配置文件。如果没有指定该属性则默认使用如下三个配置文件struts-default.xml,struts-plugin.xml,struts.xml
      actionPackages:该参数的值也是一个以英文逗号(,)隔开的字符串,每个字符串都是一个包空间,Struts 2框架将扫描这些包空间下的Action类。当然这些Action类的名称必须以"Action"为结尾。
      configProviders:如果用户需要实现自己的ConfigurationProvider类,用户可以提供一个或多个实现了ConfigurationProvider接口的类,然后将这些类的类名设置成该属性的值,多个类名之间以英文逗号(,)隔开。
      除此之外,还可在此处配置Struts 2常量,每个<init-param>元素配置一个Struts 2常量,其中<param-name>子元素指定了常量name,而<param-value>子元素指定了常量value。在这里配置的常量等价于在struts.properties文件中配置的属性。
      在web.xml文件中配置了该Filter,还需要配置该Filter拦截的URL。通常,我们让该Filter拦截所有的用户请求,因此使用通配符来配置该Filter拦截的URL。

下述源代码分析我所针对的版本如下:
 Struts2:struts-2.0.11.2(struts-2.0.11.2-all.zip)
 xwork:xwork-2.0.5(xwork-2.0.5-all.zip)

 从Struts2在web.xml文件中的配置可以看出,启动Struts2的关键在于类FilterDispatcher(org.apache.struts2.dispatcher.FilterDispatcher)的方法public void init(FilterConfig filterConfig) throws ServletException了。

 下面我们就来看init()方法里面有些什么东西? 

  1. /** 
  2.  * Initializes the filter by creating a default dispatcher and setting the 
  3.  * default packages for static resources. 
  4.  *  
  5.  * @param filterConfig 
  6.  *            The filter configuration 
  7.  */  
  8. public void init(FilterConfig filterConfig) throws ServletException  
  9. {  
  10.     this.filterConfig = filterConfig;  
  11.   
  12.     dispatcher = createDispatcher(filterConfig);  
  13.     dispatcher.init();  
  14.   
  15.     String param = filterConfig.getInitParameter("packages");  
  16.     String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging";  
  17.     if (param != null)  
  18.     {  
  19.         packages = param + " " + packages;  
  20.     }  
  21.     this.pathPrefixes = parse(packages);  
  22. }  

      其中dispatcher = createDispatcher(filterConfig);实际上是取得过滤器初始化参数,构造成参数名称和参数值的映射,并且
创建了一个Dispather工具类的对象,关键的代码是那一句dispatcher.init() 。

  1. public void init()  
  2.     {  
  3.   
  4.         if (configurationManager == null)  
  5.         {  
  6.             configurationManager = new ConfigurationManager(  
  7.                     BeanSelectionProvider.DEFAULT_BEAN_NAME);  
  8.         }  
  9.   
  10.         //设置org/apache/struts2/default.properties文件的配置解析提供者  
  11.         init_DefaultProperties(); // [1]  
  12.           
  13.         /** 
  14.          *  
  15.          * config 
  16.          * 该参数的值是一个以英文逗号(,)隔开的字符串, 
  17.          * 每个字符串都是一个XML配置文件的位置。 
  18.          * 默认配置文件为struts-default.xml,struts-plugin.xml,struts.xml 
  19.          * 这三个文件的配置解析提供者 
  20.          */  
  21.         init_TraditionalXmlConfigurations(); // [2]  
  22.           
  23.         /** 
  24.          * 设置struts.properties文件的配置解析提供者 
  25.          * 这个文件中的配置可以覆盖default.properties中的 
  26.          */  
  27.         init_LegacyStrutsProperties(); // [3]  
  28.           
  29.         /** 
  30.          * actionPackages 
  31.          * 该参数的值也是一个以英文逗号(,)隔开的字符串, 
  32.          * 每个字符串都是一个包空间, 
  33.          * Struts 2框架将扫描这些包空间下的Action类。 
  34.          */  
  35.         init_ZeroConfiguration(); // [4]  
  36.           
  37.         /** 
  38.          * 加载用户定制的配置管理器 
  39.          * configProviders 
  40.          * 如果用户需要实现自己的ConfigurationProvider类, 
  41.          * 用户可以提供一个或多个实现了ConfigurationProvider接口的类, 
  42.          * 然后将这些类的类名设置成该属性的值,多个类名之间以英文逗号(,)隔开 
  43.          */  
  44.         init_CustomConfigurationProviders(); // [5]  
  45.           
  46.         //该方法暂为空  
  47.         init_MethodConfigurationProvider();  
  48.           
  49.         /** 
  50.          * 此方法用来处理FilterDispatcher的配置中所定义的所有属性 
  51.          */  
  52.         init_FilterInitParameters(); // [6]  
  53.           
  54.         /** 
  55.          * 选择框架实现的关键扩展点,用于装载属性常量. 
  56.          * struts-default.xml中Struts2到底选择了那些实现类作为Struts2或者XWork内部接口默认实现类呢 
  57.          * 默认情况下struts-default.xml中定义beanname="struts"将被作为默认接口实现类被注入这些默认行为是 
  58.          * 由org.apache.struts2.config.BeanSelectionProvider所决定有兴趣读者可以参阅这个类源码  
  59.          * 选择的实现是从container builder使用的名字定义关联属性,默认名为”struts” 
  60.          */  
  61.         init_AliasStandardObjects(); // [7]  
  62.   
  63.         Container container = init_PreloadConfiguration();  
  64.         init_CheckConfigurationReloading(container);  
  65.         init_CheckWebLogicWorkaround(container);  
  66.   
  67.     }  

   dispather.init()方法,主要包括两部分,其中  

  if (configurationManager == null)
  {
      configurationManager = new ConfigurationManager(
              BeanSelectionProvider.DEFAULT_BEAN_NAME);
  }
  init_DefaultProperties(); // [1]  
  init_TraditionalXmlConfigurations(); // [2]
  init_LegacyStrutsProperties(); // [3]
  init_ZeroConfiguration(); // [4]
  init_CustomConfigurationProviders(); // [5]
  init_MethodConfigurationProvider();
  init_FilterInitParameters(); // [6]
  init_AliasStandardObjects(); // [7]  

是构造一个配置管理器实例对象,再将不同配置文件的解析类(配置提供者)和配置文件对应起来构造成一个配置提供者列表,供在后续的加载配置文件时使用。这里涉及两个概念,一个是配置管理器(com.opensymphony.xwork2.config.ConfigurationManager),它是整个Struts2的配置文件管理员;另一个是配置提供者,一个配置提供者完成对一个特定格式的配置文件的解析,在Struts中使用到的配置文件与配置提供者之间是一对一的关系。一个配置管理器实例对象包含一个配置提供者列表对象(List<ConfigurationProvider> configurationProviders)。

猜你喜欢

转载自joson-coney.iteye.com/blog/1409180