use of com.baidu.disconf.client.config.inner.DisInnerConfigAnnotation in project disconf by knightliao.
the class DisconfAutowareConfig method autowareConfig.
/**
* 自动导入配置数据,能识别 DisconfFileItem 或 DisInnerConfigAnnotation 的标识
*
* @Description: auto ware
*/
private static void autowareConfig(final Object obj, Properties prop) throws Exception {
if (null == prop || obj == null) {
throw new Exception("cannot autowareConfig null");
}
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(DisconfFileItem.class) || field.isAnnotationPresent(DisInnerConfigAnnotation.class)) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
String name;
String value;
if (field.isAnnotationPresent(DisconfFileItem.class)) {
name = field.getName();
value = prop.getProperty(name, null);
} else {
// disconf使用的配置
DisInnerConfigAnnotation config = field.getAnnotation(DisInnerConfigAnnotation.class);
name = config.name();
String defaultValue = config.defaultValue();
value = prop.getProperty(name, defaultValue);
// using disconf as prefix to avoid env confusion
if (value.equals(defaultValue) && name != null) {
if (name.contains("disconf.")) {
String newName = name.substring(name.indexOf('.') + 1);
value = prop.getProperty(newName, defaultValue);
}
}
}
field.setAccessible(true);
if (null != value) {
try {
ClassUtils.setFieldValeByType(field, obj, value);
} catch (Exception e) {
LOGGER.error(String.format("invalid config: %s", name), e);
}
}
}
}
} catch (Exception e) {
throw new Exception("error while autowire config file", e);
}
}
use of com.baidu.disconf.client.config.inner.DisInnerConfigAnnotation in project disconf by knightliao.
the class DisconfAutowareConfig method autowareConfigWithSystemEnv.
/**
* 使用 system env 进行数据导入, 能识别 DisInnerConfigAnnotation 的标识
*
* @Description: auto ware
*/
public static void autowareConfigWithSystemEnv(final Object obj) throws Exception {
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(DisInnerConfigAnnotation.class)) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
String name;
String value;
// disconf使用的配置
DisInnerConfigAnnotation config = field.getAnnotation(DisInnerConfigAnnotation.class);
name = config.name();
// 优先使用 系统参数或命令行导入
value = System.getProperty(name);
field.setAccessible(true);
if (null != value) {
try {
ClassUtils.setFieldValeByType(field, obj, value);
} catch (Exception e) {
LOGGER.error(String.format("invalid config: %s", name), e);
}
}
}
}
} catch (Exception e) {
throw new Exception("error while autowareConfigWithSystemEnv autowire config file", e);
}
}
Aggregations