Spring ioc-BeanWrapper

来源:互联网 发布:思科路由端口转换 编辑:程序博客网 时间:2024/06/05 19:43

BeanWrapper

BeanWrapper是Spring IOC内部操作bean对象的api。

BeanWrapper是对bean的包装。

其接口中所定义的功能:

1.    设置获取被包装的对象

2.    获取被包装bean的属性描述器

3.    由于BeanWrapper接口是PropertyAccessor的子接口,因此其也可以设置以及访问被包装对象的属性值

 

BeanWrapperImpl

BeanWrapperImpl类是对BeanWrapper接口的默认实现,它包装了一个bean对象缓存了bean的内省结果,并可以访问bean的属性设置bean的属性值。BeanWrapperImpl类提供了许多默认属性编辑器,支持多种不同类型的类型转换,可以将数组、集合类型的属性转换成指定特殊类型的数组或集合。用户也可以注册自定义的属性编辑器在BeanWrapperImpl中。

 

Aop Demo:

public classUser{// 用户类       private String name;       public String getName{       return name;}public voidsetName(String name){       this.name = name;}}


 

//这是一个数据库插入前的aop操作

publicvoid beforeInsert(JoinPoint jp) throws Throwable {    Object[] args = jp.getArgs();    if (!ArrayUtils.isEmpty(args)) {        Object record = args[0];        BeanWrapper beanWrapper = new BeanWrapperImpl(record);        setProp(beanWrapper, “name”, “菜鸟要飞”);//设置name属性值        }}privatevoid setProp(BeanWrapper beanWrapper, String propName, Object value) {//内部通过属性描述器判断字段名是否存在,设置属性也是用属性描述器    if (beanWrapper.isWritableProperty(propName)&& value != null) {       beanWrapper.setPropertyValue(propName, value);    }}

源码分析:http://blog.csdn.net/zhiweianran/article/details/7919129

原创粉丝点击