MetaObject基本架构
MetaObject获取属性值流程
源码分析MetaObject获取属性值
public class MetaObjectTest {
@Test
public void test1(){
Object blog = new Blog();
Configuration configuration = new Configuration();
MetaObject metaObject = configuration.newMetaObject(blog);
User user = new User();
user.setName("xxppp");
ArrayList<Comment> comments = new ArrayList<>();
Comment comment = new Comment();
comment.setUser(user);
comments.add(comment);
metaObject.setValue("comments",comments);
metaObject.getValue("comments[0].user.name");
}
}
经过属性分词器解析
此时hasNext()为true表示存在子属性
调用getValue,此时参数和上一次已经不一样了。为comments[0],此时没有子属性,直接调用get方法获取comments[0]的值。
再将获取到的comments[0]值包装为MetaObject。
继续调用getValue(),此时参数为user.name。
此时是有子属性的,为name,会进入metaObjectForProperty()。
此时user没有子属性,调用get方法获取user值
将获取到的user值包装为MetaObject。
继续调用getValue(),此时参数是name,没有子属性,直接调用get()获取name值
对BeanWrapper【get】方法单独分析
再次出现调用getValue()
根据属性名,反射执行getXxx方法
总结:获取属性主要是利用MetaObject中的属性分词器,和反射原理
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
喜欢就支持一下吧
相关推荐
暂无评论内容