一个0后IT之路 2010-04-29
转自:http://tech.it168.com/j/2007-09-21/200709211302700.shtml
感谢作者wmj2003
struts2配置文件加载流程分析[收藏此页][打印]
作者:wmj20032007-09-21网友评论1条内容导航:struts2配置文件加载流程分析第1页:struts2配置文件加载流程分析
首先org.apache.struts2.dispatcher.FilterDispatcher.java中的init()方法,
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
this.filterConfig=filterConfig;
Stringparam=filterConfig.getInitParameter("packages");//由此可以看出可以增加参数packages
Stringpackages="org.apache.struts2.statictemplateorg.apache.struts2.interceptor.debugging";
if(param!=null){
packages=param+""+packages;
}
this.pathPrefixes=parse(packages);
dispatcher=newDispatcher(filterConfig.getServletContext());
}
org.apache.struts2.dispatcher.Dispatcher.java
首先执行静态代码块:
static{
ObjectFactory.setObjectFactory(newStrutsObjectFactory());
ActionProxyFactory.setFactory(newStrutsActionProxyFactory());
}
执行构造方法
publicDispatcher(ServletContextservletContext){
init(servletContext);
}
然后执行init()方法:
booleanreloadi18n=Boolean.valueOf(
(String)Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
上面的语句读取了properties文件。
读取properties文件的流程:
首先执行Org.apache.struts2.dispatcher.Dispatcher.java类的方法init()中的语句
booleanreloadi18n=Boolean.valueOf((String)
Settings.get(StrutsConstants.STRUTS_I18N_RELOAD)).booleanValue();
然后执行了org.apacher.struts2.config.Setting.java类中的方法
publicstaticStringget(Stringname)throwsIllegalArgumentException{
Stringval=getInstance().getImpl(name);
returnval;
}
然后执行了
publicstaticSettingsgetInstance(){
return(settingsImpl==null)?getDefaultInstance():settingsImpl;
}
然后执行了
privatestaticSettingsgetDefaultInstance(){
if(defaultImpl==null){
//Createbootstrapimplementation
defaultImpl=newDefaultSettings();//产生了一个DefaultSettings对象
//Createdefaultimplementation
try{
StringclassName=get(StrutsConstants.STRUTS_CONFIGURATION);
if(!className.equals(defaultImpl.getClass().getName())){
try{
//singletoninstancesshouldn'tbebuiltaccessingrequestorsession-specificcontextdata
defaultImpl=(Settings)ObjectFactory.getObjectFactory().buildBean(Thread.currentThread().getContextClassLoader().loadClass(className),null);
}catch(Exceptione){
LOG.error("Couldnotinstantiatesettings",e);
}
}
}catch(IllegalArgumentExceptionex){
//ignore
}
}
returndefaultImpl;
}
然后首先执行了语句defaultImpl=newDefaultSettings();
publicDefaultSettings(){
//Createdefaultimplementations
//Usedefaultpropertiesandstruts.properties
ArrayListlist=newArrayList();
try{
list.add(newPropertiesSettings("struts"));//从这里加载struts.properties文件
}catch(Exceptione){
log.warn("Couldnotfindorerrorinstruts.properties",e);
}
try{
list.add(newPropertiesSettings("org/apache/struts2/default"));//加载默认的defualt.properties文件
}catch(Exceptione){
log.error("Couldnotfindorg/apache/struts2/default.properties",e);
}
Settings[]configList=newSettings[list.size()];//定义了Settings对象数组
config=newDelegatingSettings((Settings[])list.toArray(configList));
//Addlistofadditionalpropertiessettingss
try{
StringTokenizerconfigFiles=newStringTokenizer((String)config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES),",");
加载了用户自己定义的properties文件。
while(configFiles.hasMoreTokens()){
Stringname=configFiles.nextToken();
try{
list.add(newPropertiesSettings(name));
}catch(Exceptione){
log.error("Couldnotfind"+name+".properties.Skipping");
}
}
configList=newSettings[list.size()];
config=newDelegatingSettings((Settings[])list.toArray(configList));
}catch(IllegalArgumentExceptione){
//thrownwhenSettingsisunabletofindacertainproperty
//eg.struts.custom.propertiesindefault.propertieswhichiscommented
//out
}
//Addadditionallistofi18nglobalresourcebundles
try{
LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
StringTokenizerbundleFiles=newStringTokenizer((String)config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES),",");
while(bundleFiles.hasMoreTokens()){
Stringname=bundleFiles.nextToken();
try{
log.info("Loadingglobalmessagesfrom"+name);
LocalizedTextUtil.addDefaultResourceBundle(name);
}catch(Exceptione){
log.error("Couldnotfind"+name+".properties.Skipping");
}
}
}catch(IllegalArgumentExceptione){
//struts.custom.i18n.resourceswasn'tprovided
}
}
看看org.apache.struts2.config.DelegatingSettings.java下面的方法。
/**
*Getsthespecifiedproperty-callsgetImpl(String)methodonconfigobjectsinconfiglist
*untilsuccessful.
*
*@see#get(String)
*/
publicStringgetImpl(Stringname)throwsIllegalArgumentException{
//Delegatetotheothersettings
IllegalArgumentExceptione=null;
for(inti=0;i<configList.length;i++){
try{
returnconfigList[i].getImpl(name);
}catch(IllegalArgumentExceptionex){
e=ex;
//Trynextconfig
}
}
throwe;
}
小结:
通过上面的代码分析,大家就可以明白为什么struts.properties中的配置项会自动覆盖default.properties中的属性,而不会配用户自定义的配置文件覆盖。