spring5属性及内嵌属性解析
所属分类 spring
浏览量 1363
application.properties
hellomsg=hello,${name}
name=tiger
age=9
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(Config.class);
ctx.refresh();
// hellomsg=hello,${name}
// name=tiger
// age=9
Environment env = ctx.getEnvironment();
String name = env.getProperty("name");
String hellomsg = env.getProperty("hellomsg");
int age = env.getProperty("age", Integer.class);
System.out.println("name="+name);
// PropertySourcesPropertyResolver
System.out.println("hellomsg="+hellomsg);
System.out.println("age="+age);
// 使用反射获取 AbstractEnvironment 的 propertyResolver 字段
PropertySourcesPropertyResolver propertyResolver = getPropertySourcesPropertyResolver(env);
System.out.println(propertyResolver);
// 使用反射调用 PropertySourcesPropertyResolver 里的protected方法 getPropertyAsRawString
String hellomsgRaw = getPropertyAsRawString("hellomsg",propertyResolver);
System.out.println("hellomsgRaw="+hellomsgRaw);
输出结果
name=tiger
hellomsg=hello,tiger
age=9
hellomsgRaw=hello,${name}
PropertySourcesPropertyResolver
protected String getPropertyAsRawString(String key) {
return getProperty(key, String.class, false);
}
protected T getProperty(String key, Class targetValueType, boolean resolveNestedPlaceholders)
resolveNestedPlaceholders 是否解析 内嵌属性
hellomsgRaw=hello,${name}
Object value = propertySource.getProperty(key);
if (value != null) {
if (resolveNestedPlaceholders && value instanceof String) {
value = resolveNestedPlaceholders((String) value);
}
}
一些异常情况测试
try{
propertyResolver.getRequiredProperty("propNotExist");
}catch(Throwable e){
// java.lang.IllegalStateException: Required key 'propNotExist' not found
System.out.println(e);
}
try{
propertyResolver.getProperty("name", Integer.class);
}catch(Throwable e){
// org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value 'tiger'; nested exception is java.lang.NumberFormatException: For input string: "tiger"
System.out.println(e);
}
属性循环引用测试
msg1=msg1-hello,${msg2}
msg2=msg2-hello,${msg1}
String msg1 = env.getProperty("msg1");
java.lang.IllegalArgumentException: Circular placeholder reference 'msg2' in property definitions
private static PropertySourcesPropertyResolver getPropertySourcesPropertyResolver(Environment env)throws Exception{
// 获取 public 字段 (包含父类)
// Field field = env.getClass().getField("propertyResolver");
// 获取本类的所有字段 (不包含父类的字段)
// Field field = env.getClass().getDeclaredField("propertyResolver");
// env StandardEnvironment
// AbstractEnvironment 包含 propertyResolver 字段
Field field = env.getClass().getSuperclass().getDeclaredField("propertyResolver");
field.setAccessible(true);
return (PropertySourcesPropertyResolver)field.get(env);
}
private static String getPropertyAsRawString(String key,PropertySourcesPropertyResolver propertyResolver)throws Exception{
Method method = PropertySourcesPropertyResolver.class.getDeclaredMethod("getPropertyAsRawString", new Class[]{String.class});
method.setAccessible(true);
return (String)method.invoke(propertyResolver, key);
}
上一篇
下一篇
spring5自动装配实例
eclipse异常断点设置
eclipse设置条件断点和异常断点
反射获取字段注意点
这样的人你敢要吗
我们怎么了