spring笔记-AttributeAccessor(转载)
原文出处:https://www.jianshu.com/p/3b338dda2437
1. 接口定义

定义了对对象元数据访问的抽象接口
2. AttributeAccessorSupport
AttributeAccessorSupport是实现AttributeAccessor的抽象类,内部由LinkedHashMap实现

测试代码:
public class AttributeAccessorSupportTests {
private static final String NAME = "foo";
private static final String VALUE = "bar";
private AttributeAccessor attributeAccessor = new SimpleAttributeAccessorSupport();
@Test
public void setAndGet() throws Exception {
this.attributeAccessor.setAttribute(NAME, VALUE);
assertEquals(VALUE, this.attributeAccessor.getAttribute(NAME));
}
@Test
public void setAndHas() throws Exception {
assertFalse(this.attributeAccessor.hasAttribute(NAME));
this.attributeAccessor.setAttribute(NAME, VALUE);
assertTrue(this.attributeAccessor.hasAttribute(NAME));
}
@Test
public void remove() throws Exception {
assertFalse(this.attributeAccessor.hasAttribute(NAME));
this.attributeAccessor.setAttribute(NAME, VALUE);
assertEquals(VALUE, this.attributeAccessor.removeAttribute(NAME));
assertFalse(this.attributeAccessor.hasAttribute(NAME));
}
@Test
public void attributeNames() throws Exception {
this.attributeAccessor.setAttribute(NAME, VALUE);
this.attributeAccessor.setAttribute("abc", "123");
String[] attributeNames = this.attributeAccessor.attributeNames();
Arrays.sort(attributeNames);
assertTrue(Arrays.binarySearch(attributeNames, NAME) > -1);
assertTrue(Arrays.binarySearch(attributeNames, "abc") > -1);
}
@SuppressWarnings("serial")
private static class SimpleAttributeAccessorSupport extends AttributeAccessorSupport {
}
}
3.BeanMetadataAttributeAccessor

BeanMetadataAttributeAccessor继承自AttributeAccessorSupport,
其重写了相关方法,对象BeanMetadataAttribute定义了key和value2个属性


4. PropertyValue
继承自BeanMetadataAttributeAccessor

PropertyValue具备Name和Value属性,继承了BeanMetadataAttributeAccessor,那么可以理解PropertyValue具备动态扩展属性的能力

5.MutablePropertyValues
对PropertyValue的容器管理
测试代码:
@Test
public void testValid() throws Exception {
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("forname", "Tony"));
pvs.addPropertyValue(new PropertyValue("surname", "Blair"));
pvs.addPropertyValue(new PropertyValue("age", "50"));
}
6. PropertyValue应用
bean的property是最常用的
<bean id="company" class="core.di.innerBeans.CompanyInfo">
<property name="cname" value="Google"/>
<property name="clocation" value="Mountain View, US"/>
<property name="cturnover" value="2000000"/>
</bean>
对应的BeanDefinition中的propertyValues属性

也可以以代码的形式手动添加PropertyValue来为对象动态设置属性
@Test
public void test1()
{
GenericBeanDefinition item=new GenericBeanDefinition();
item.setBeanClass(TestBean.class);
item.getPropertyValues().add("name","test");
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext();
applicationContext.registerBeanDefinition("item",item);
applicationContext.refresh();
TestBean bean= (TestBean) applicationContext.getBean("item");
}
参考:
http://blog.csdn.net/dfl_always/article/details/53610351
http://blog.csdn.net/u012665780/article/details/18364309
作者:兴浩
链接:https://www.jianshu.com/p/3b338dda2437
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

浙公网安备 33010602011771号