org.apache.struts.config.DataSourceConfig类浅析
struts-config.xml文件中每个<data-soutce>元素都对应着一个DataSourceConfig对象
<data-sources>元素用来配置应用所需要的数据源,它包含零个、一个或多个<data-source>子元素。<data-source>元素用于配置特定的数据源,它可以包含多个<set-property>子元素
<data-soutce>元素内的type属性用来指定数据源的实现类,它在DataSourceConfig中对应的属性为String type.
<data-soutce>元素内的Key属性,是在声明多个数据源时为每个数据源分配唯一的Key值,通过该值来标识特定的数据源。
<set-property>元素是用来指定数据源的相关配置,它在DataSourceConfig中对应的属性为HashMap properties,<set-property>元素中的property属性对应着HashMap中的Key、value对应着HashMap中的value
以上属性在org.apache.struts.config.DataSourceConfig类中都有对应的get和set方法
类中方法解析
public class DataSourceConfig implements Serializable {
protected boolean configured = false;
protected String key = Globals.DATA_SOURCE_KEY;
public String getKey() {
return (this.key);
}
public void setKey(String key) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.key = key;
}
protected HashMap properties = new HashMap();
public Map getProperties() {
return (this.properties);
}
protected String type;
public String getType() {
return (this.type);
}
public void setType(String type) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
this.type = type;
}
//该方法用将<set-property>元素下的property属性和value属性保存到HashMap properties中,其中name属性对应元素中的property属性,value属性对应元素中的value属性
public void addProperty(String name, String value) {
if (configured) {
throw new IllegalStateException("Configuration is frozen");
}
properties.put(name, value);
}
public void freeze() {
configured = true;
}
public String toString() {
StringBuffer sb = new StringBuffer("DataSourceConfig[");
sb.append("key=");
sb.append(key);
sb.append(",type=");
sb.append(type);
Iterator names = properties.keySet().iterator();
while (names.hasNext()) {
String name = (String) names.next();
String value = (String) properties.get(name);
sb.append(',');
sb.append(name);
sb.append('=');
sb.append(value);
}
sb.append("]");
return (sb.toString());
}
}
备注:DataSourceConfig只有默认的构造函数,没有其他构造函数。