首先org.apache.struts2.dispatcher. FilterDispatcher.java 中的init()方法,
org.apache.struts2.dispatcher. Dispatcher.java
首先执行静态代码块:
执行构造方法
public Dispatcher(ServletContext servletContext) {
init(servletContext);
}
然后执行init()方法:
boolean reloadi18n = Boolean.valueOf(
(String) Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
上面的语句读取了properties文件。
读取properties文件的流程:
首先执行Org.apache.struts2.dispatcher.Dispatcher.java 类的方法init()中的语句
boolean reloadi18n = Boolean.valueOf((String)
Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
然后执行了org.apacher.struts2.config.Setting.java类中的方法
public static String get(String name) throws IllegalArgumentException {
String val = getInstance().getImpl(name);
return val;
}
然后执行了
public static Settings getInstance() {
return (settingsImpl == null) ? getDefaultInstance() : settingsImpl;
}
然后执行了
然后首先执行了语句defaultImpl = new DefaultSettings();
看看org.apache.struts2.config.DelegatingSettings.java下面的方法。
小结:
通过上面的代码分析,大家就可以明白为什么struts.properties中的配置项会自动覆盖default.properties中的属性,而不会配用户自定义的配置文件覆盖。
public void
init(FilterConfig filterConfig) throws ServletException { this.filterConfig =
filterConfig; String param = filterConfig.getInitParameter("packages");//由此可以看出可以增加参数packages
String packages = "org.apache.struts2.static template org.apache.struts2.interceptor.debugging"
; if (param != null
) { packages = param + " " +
packages; } this.pathPrefixes =
parse(packages); dispatcher = new
Dispatcher(filterConfig.getServletContext()); }
org.apache.struts2.dispatcher. Dispatcher.java
首先执行静态代码块:
static
{ ObjectFactory.setObjectFactory(new
StrutsObjectFactory()); ActionProxyFactory.setFactory(new
StrutsActionProxyFactory()); }
执行构造方法
public Dispatcher(ServletContext servletContext) {
init(servletContext);
}
然后执行init()方法:
boolean reloadi18n = Boolean.valueOf(
(String) Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
上面的语句读取了properties文件。
读取properties文件的流程:
首先执行Org.apache.struts2.dispatcher.Dispatcher.java 类的方法init()中的语句
boolean reloadi18n = Boolean.valueOf((String)
Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
然后执行了org.apacher.struts2.config.Setting.java类中的方法
public static String get(String name) throws IllegalArgumentException {
String val = getInstance().getImpl(name);
return val;
}
然后执行了
public static Settings getInstance() {
return (settingsImpl == null) ? getDefaultInstance() : settingsImpl;
}
然后执行了
private static
Settings getDefaultInstance() { if (defaultImpl == null
) { // Create bootstrap implementation
defaultImpl = new DefaultSettings(); //
产生了一个DefaultSettings对象 // Create default implementation
try
{ String className = get
(StrutsConstants.STRUTS_CONFIGURATION); if (!
className.equals(defaultImpl.getClass().getName())) { try
{ // singleton instances shouldn't be built accessing request or session-specific context data
defaultImpl = (Settings) ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className), null
); } catch
(Exception e) { LOG.error("Could not instantiate settings"
, e); } } } catch
(IllegalArgumentException ex) { // ignore
} } return
defaultImpl; }
然后首先执行了语句defaultImpl = new DefaultSettings();
public
DefaultSettings() { //
Create default implementations // Use default properties and struts.properties
ArrayList list = new
ArrayList(); try
{ list.add(new PropertiesSettings("struts"));//从这里加载struts.properties文件
} catch
(Exception e) { log.warn("Could not find or error in struts.properties"
, e); } try
{ list.add(new PropertiesSettings("org/apache/struts2/default")); //加载默认的defualt.properties文件
} catch
(Exception e) { log.error("Could not find org/apache/struts2/default.properties"
, e); } Settings[] configList = new Settings[list.size()];//定义了Settings对象数组
config = new
DelegatingSettings((Settings[]) list.toArray(configList)); // Add list of additional properties settingss
try
{ StringTokenizer configFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ","
); 加载了用户自己定义的properties文件。 while
(configFiles.hasMoreTokens()) { String name =
configFiles.nextToken(); try
{ list.add(new
PropertiesSettings(name)); } catch
(Exception e) { log.error("Could not find " + name + ".properties. Skipping"
); } } configList = new
Settings[list.size()]; config = new
DelegatingSettings((Settings[]) list.toArray(configList)); } catch
(IllegalArgumentException e) { //
thrown when Settings is unable to find a certain property //
eg. struts.custom.properties in default.properties which is commented // out
} // Add additional list of i18n global resource bundles
try
{ LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages"
); StringTokenizer bundleFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", "
); while
(bundleFiles.hasMoreTokens()) { String name =
bundleFiles.nextToken(); try
{ log.info("Loading global messages from " +
name); LocalizedTextUtil.addDefaultResourceBundle(name); } catch
(Exception e) { log.error("Could not find " + name + ".properties. Skipping"
); } } } catch
(IllegalArgumentException e) { // struts.custom.i18n.resources wasn't provided
} }
看看org.apache.struts2.config.DelegatingSettings.java下面的方法。
/*
* * Gets the specified property - calls getImpl(String) method on config objects in config list * until successful. * * @see #get(String) */
public
String getImpl(String name) throws IllegalArgumentException { // Delegate to the other settings
IllegalArgumentException e = null
; for (int i = 0; i < configList.length; i++
) { try
{ return
configList[i].getImpl(name); } catch
(IllegalArgumentException ex) { e =
ex; // Try next config
} } throw
e; }
小结:
通过上面的代码分析,大家就可以明白为什么struts.properties中的配置项会自动覆盖default.properties中的属性,而不会配用户自定义的配置文件覆盖。
1