1.Config
Config有4个实现类
- InternamHSFConfig
- EdasConfig
- ApplicationHSFConfig
- SystemConfig
1.1 Config接口
AbstractConfig是Config的默认抽象实现
1.2 其中继承自AbstractPropertiesConfig的都是读取properties文件,
如InternalHSFConfig,重写getResourceName方法,即读取internal_hsf_config.properties文件
public class InternalHSFConfig extends AbstractFrameworkPropertiesConfig {
public InternalHSFConfig() {
}
protected String getResourceName() {
return "internal_hsf_config.properties";
}
}
1.3 SystemConfig
读取系统属性
public class SystemConfig extends AbstractConfig {
public SystemConfig() {
}
protected String findProperty(String key) {
return System.getProperty(key);
}
protected Set<String> findKeys() {
return new HashSet(System.getProperties().stringPropertyNames());
}
}
2.ConfigService
ConfigService接口非常简单,只返回一个Config实例
public interface ConfigService {
Config getConfig();
}
其内部实现了Config初始化逻辑,源码如下:
public class ConfigServiceImpl implements ConfigService, ApplicationModelAware, FrameworkModelAware {
private Config config;
public ConfigServiceImpl() {
}
public Config getConfig() {
return this.config;
}
public void setApplicationModel(ApplicationModel applicationModel) {
this.loadConfig(applicationModel.getServiceContainer());
}
public void setFrameworkModel(FrameworkModel frameworkModel) {
this.loadConfig(HSFServiceContainer.SHARED_CONTAINER);
}
private void loadConfig(AppServiceContainer appServiceContainer) {
List<Config> configList = appServiceContainer.getInstances(Config.class);
Collections.reverse(configList);
for(int i = 0; i < size - 1; ++i) {
AbstractConfig config = (AbstractConfig)configList.get(i);
config.setParent((AbstractConfig)configList.get(i + 1));
config.init();
}
this.config = (Config)configList.get(0);
}
}
分析一下上面代码
1.List<Config> configList = appServiceContainer.getInstances(Config.class);
首先看一下配置文件com.taobao.hsf.configuration.Config,有3个Config,EdasConfig则在另一个项目里面
com.taobao.hsf.configuration.impl.SystemConfig
com.taobao.hsf.configuration.impl.InternalHSFConfig
com.taobao.hsf.configuration.impl.ApplicationHSFConfig
获取Config列表,这有点spring boot的感觉,与其使用方法基本一致
2.AbstractConfig有一个parent属性,指向下一个Config,每次获取属性时,则先向父Config获取配置,最后一个Config没有调用init方法,即SystemConfig
public <T> T getProperty(Class<T> cls, String key) {
if (cls == null) {
throw new IllegalArgumentException("property class is null");
} else {
T result = null;
if (this.parent != null) {
result = this.parent.getProperty(cls, key);
}
if (result == null) {
String value = this.findProperty(key);
if (value != null) {
result = this.convertValue(cls, value);
}
}
return result;
}
}
所以ConfigService只需要返回一个Config,实际其内部是一个链表获取操作,如下堆栈
3.Env
Env是对Config中key,value的Bean化包装,更加通俗易懂些
public class EnvImpl implements Env {
private Config config = ((ConfigService)HSFServiceContainer.getInstance(ConfigService.class)).getConfig();
public EnvImpl() {
this.bindHost = this.config.getString("HsfBindHost", this.config.getString("hsf.server.ip", this.getHostIp()));
this.bindPort = this.config.getInt("hsf.server.port");
this.bindHttpPort = this.config.getInt("hsf.http.port");
//...
}
网友评论