死磕源码系列【springboot之DataObjectBinder、DataObject。。。
DataObjectBinder是⼀个内部策略被Binder⽤来绑定数据对象,数据对象是由递归绑定的属性组成的对象;其实现类有
JavaBeanBinder、ValueObjectBinder;JavaBeanBinder实现类是通过getter/tter绑定,ValueObjectBinder实现类是通过构造函数绑定;
DataObjectBinder接⼝源码:
interface DataObjectBinder {
/**
* 返回⼀个绑定的实例,如果DataObjectBinder不⽀持指定的Bindable,则返回null
*/
<T> T bind(ConfigurationPropertyName name, Bindable<T> target, Context context,
DataObjectPropertyBinder propertyBinder);
/**
* 返回⼀个新创建的实例,如果DataObjectBinder不⽀持指定的Bindable,则返回null
*/
<T> T create(Bindable<T> target, Context context);
}
JavaBeanBinder类⽤于不可变的Java Beans,源码如下:
//JavaBeanBinder的空实例对象
茶叶蛋有营养吗static final JavaBeanBinder INSTANCE =new JavaBeanBinder();
//绑定指定的值到指定的属性
@Override
public<T> T bind(ConfigurationPropertyName name, Bindable<T> target, Context context,
哈蜜瓜
DataObjectPropertyBinder propertyBinder){叮咛
boolean hasKnownBindableProperties = Value()!= null &&hasKnownBindableProperties(name, context);
//获取javabean的属性字段对应的绑定结果集
Bean<T> bean = (target, hasKnownBindableProperties);
if(bean == null){
return null;
}
BeanSupplier<T> beanSupplier = Supplier(target);
//绑定字段值,并返回是否绑定成功
boolean bound =bind(propertyBinder, bean, beanSupplier, context);
return(bound ? (): null);
}
//创建属性字段值的实例
@Override
@SuppressWarnings("unchecked")
public<T> T create(Bindable<T> target, Context context){
//获取属性值的class实例
Class<T> type =(Class<T>) Type().resolve();
//实例化属性字段类型
return(type != null)? BeanUtils.instantiateClass(type): null;
}
private boolean hasKnownBindableProperties(ConfigurationPropertyName name, Context context){
for(ConfigurationPropertySource source : Sources()){
ainsDescendantOf(name)== ConfigurationPropertyState.PRESENT){
return true;
}
}
return fal;
}
//将javabean字段属性值绑定为指定的配置属性
private<T>boolean bind(DataObjectPropertyBinder propertyBinder, Bean<T> bean, BeanSupplier<T> beanSupplier,
Context context){
boolean bound =fal;
boolean bound =fal;
for(BeanProperty beanProperty : Properties().values()){
bound |=bind(beanSupplier, propertyBinder, beanProperty);
context.clearConfigurationProperty();
}
return bound;
}
private<T>boolean bind(BeanSupplier<T> beanSupplier, DataObjectPropertyBinder propertyBinder, BeanProperty property){
//获取属性字段名
String propertyName = Name();
//获取字段数据类型
ResolvableType type = Type();
//获取属性字段值的Supplier包装对象
Supplier<Object> value = Value(beanSupplier);
//获取属性字段上标注的注解
Annotation[] annotations = Annotations();
//获取属性字段的绑定值
Object bound = propertyBinder.bindProperty(propertyName,
Bindable.of(type).withSuppliedValue(value).withAnnotations(annotations));
if(bound == null){
return fal;
}
李达康是谁演的
/
/设置属性字段的值
if(property.isSettable()){
property.tValue(beanSupplier, bound);
}
el if(value == null ||!bound.())){
throw new IllegalStateException("No tter found for property: "+ Name());
}
return true;
}
BeanProperty类代表正在绑定的bean属性
static class BeanProperty {
/
/属性名
private final String name;
//属性配置源对应的java bean类类型实例对象
private final ResolvableType declaringClassType;
//属性对应的getter⽅法Method对象
private Method getter;
//属性对应的tter⽅法Method对象
private Method tter;
//属性字段的Field对象
private Field field;
//创建属性的BeanProperty实例对象
BeanProperty(String name, ResolvableType declaringClassType){
this.name = DashedForm(name);
this.declaringClassType = declaringClassType;
}
//初始化getter对象
void addGetter(Method getter){
== null){
< = getter;
}
}
//初始化tter对象
void addSetter(Method tter){
== null ||isBetterSetter(tter)){
< = tter;
}
}
//tter⽅法初始化条件判定
private boolean isBetterSetter(Method tter){
private boolean isBetterSetter(Method tter){
!= null &&ReturnType().ParameterTypes()[0]); }
//初始化field对象
void addField(Field field){
if(this.field == null){
this.field = field;
}
}
//获取属性字段名
String getName(){
return this.name;
}
//获取字段的数据类型
ResolvableType getType(){
!= null){
MethodParameter methodParameter =new ,0);
return ResolvableType.forMethodParameter(methodParameter,this.declaringClassType);
}
MethodParameter methodParameter =new ,-1);
return ResolvableType.forMethodParameter(methodParameter,this.declaringClassType);
}
//获取字段上标注的注解
Annotation[]getAnnotations(){
try{
return(this.field != null)?DeclaredAnnotations(): null;
}
catch(Exception ex){
return null;
}
}
//获取字段对应的属性值
Supplier<Object>getValue(Supplier<?> instance){
== null){
return null;
}
return()->{
try{
());
}
catch(Exception ex){
throw new IllegalStateException("Unable to get value for property "+this.name, ex);
}
};
}
//判定属性字段是否可以设置值
boolean isSettable(){
!= null;
}
//设置属性的值
void tValue(Supplier<?> instance, Object value){
try{
//获取属性配置源对象,将指定的值设置为指定属性的值
<(), value);
}
catch(Exception ex){
throw new IllegalStateException("Unable to t value for property "+this.name, ex);
}
}
}
正在绑定的Bean类(是指⼀个JavaBean属性对象和属性值之间的绑定关系):
static class Bean<T>{
//Bean实例对象
private static Bean<?> cached;
//JavaBean的数据类型
private final ResolvableType type;
//javabean的class实例对象
private final Class<?> resolvedType;
//javabean的属性字段及绑定值之间的映射关系
private final Map<String, BeanProperty> properties =new LinkedHashMap<>();
Bean(ResolvableType type, Class<?> resolvedType){
//初始化type javabean的数据类型
//javabean的class实例对象
addProperties(resolvedType);
}
private void addProperties(Class<?> type){
while(type != null &&!Object.class.equals(type)){
//获取javabean的所有⽅法对象
Method[] declaredMethods = DeclaredMethods();
//获取javabean的所有属性字段对象
Field[] declaredFields = DeclaredFields();
addProperties(declaredMethods, declaredFields);
type = Superclass();
}
}
protected void addProperties(Method[] declaredMethods, Field[] declaredFields){
for(int i =0; i < declaredMethods.length; i++){
if(!isCandidate(declaredMethods[i])){
declaredMethods[i]= null;
}
}
for(Method method : declaredMethods){
addMethodIfPossible(method,"get",0, BeanProperty::addGetter);
addMethodIfPossible(method,"is",0, BeanProperty::addGetter);
}
蔬菜丸子的家常做法for(Method method : declaredMethods){
毕业生自我鉴定表addMethodIfPossible(method,"t",1, BeanProperty::addSetter);
}
for(Field field : declaredFields){
addField(field);
}
}
private boolean isCandidate(Method method){
int modifiers = Modifiers();
return!Modifier.isPrivate(modifiers)&&!Modifier.isProtected(modifiers)&&!Modifier.isAbstract(modifiers) &&!Modifier.isStatic(modifiers)&&!Object.class.DeclaringClass())
&&!Class.class.DeclaringClass())&& Name().indexOf('$')==-1;
}
private void addMethodIfPossible(Method method, String prefix,int parameterCount,
BiConsumer<BeanProperty, Method> consumer){
if(method != null && ParameterCount()== parameterCount && Name().startsWith(prefix) && Name().length()> prefix.length()){
String propertyName = Introspector.Name().substring(prefix.length()));
consumer.accept(puteIfAbnt(propertyName,this::getBeanProperty), method);
}
}
//获取BeanProperty实例对象,即指定属性及值的对应关系
private BeanProperty getBeanProperty(String name){
private BeanProperty getBeanProperty(String name){
return new BeanProperty(pe);
}
//添加指定字段的Filed属性对象值
private void addField(Field field){
BeanProperty property =(Name());
if(property != null){
property.addField(field);
}
}
//获取javabean属性字段及值的集合
Map<String, BeanProperty>getProperties(){
return this.properties;
}
//获取绑定值的实例对象
@SuppressWarnings("unchecked")
BeanSupplier<T>getSupplier(Bindable<T> target){
return new BeanSupplier<>(()->{
T instance = null;
Value()!= null){
instance = Value().get();
}
if(instance == null){
instance =(T) BeanUtils.solvedType);
}
王羲之兰亭序字帖return instance;
});
}
//获取当前类的实例对象
@SuppressWarnings("unchecked")
static<T> Bean<T>get(Bindable<T> bindable,boolean canCallGetValue){ ResolvableType type = Type();
Class<?> resolvedType = solve(Object.class);
Supplier<T> value = Value();
T instance = null;
if(canCallGetValue && value != null){
instance = ();
resolvedType =(instance != null)? Class(): resolvedType; }
if(instance == null &&!isInstantiable(resolvedType)){
return null;
}
Bean<?> bean = Bean.cached;
if(bean == null ||!bean.isOfType(type, resolvedType)){
bean =new Bean<>(type, resolvedType);
cached = bean;
}
return(Bean<T>) bean;
}
private static boolean isInstantiable(Class<?> type){
if(type.isInterface()){
return fal;护理求职信
}
try{
return true;
}
catch(Exception ex){
return fal;
}
}
private boolean isOfType(ResolvableType type, Class<?> resolvedType){ pe.hasGenerics()|| type.hasGenerics()){
pe.equals(type);