mini spring learning
https://www.pexels.com/zh-cn/photo/768089/
http://www.implements.fun:8080/tag/minispring
package com.minis.beans.factory;
import com.minis.beans.BeansException;
public interface BeanFactory {
Object getBean(String name) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name);
boolean isPrototype(String name);
Class<?> getType(String name);
}
package com.minis.beans;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BeanWrapperImpl extends AbstractPropertyAccessor {
Object wrappedObject;
Class<?> clz;
public BeanWrapperImpl(Object object) {
super();
this.wrappedObject = object;
this.clz = object.getClass();
}
@Override
public void setPropertyValue(PropertyValue pv) {
BeanPropertyHandler propertyHandler = new BeanPropertyHandler(pv.getName());
PropertyEditor pe = this.getCustomEditor(propertyHandler.getPropertyClz());
if (pe == null) {
pe = this.getDefaultEditor(propertyHandler.getPropertyClz());
}
if (pe != null) {
pe.setAsText((String) pv.getValue());
propertyHandler.setValue(pe.getValue());
}
else {
propertyHandler.setValue(pv.getValue());
}
}
class BeanPropertyHandler {
Method writeMethod = null;
Method readMethod = null;
Class<?> propertyClz = null;
public Class<?> getPropertyClz() {
return propertyClz;
}
public BeanPropertyHandler(String propertyName) {
try {
Field field = clz.getDeclaredField(propertyName);
propertyClz = field.getType();
this.writeMethod = clz.getDeclaredMethod("set"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1),propertyClz);
this.readMethod = clz.getDeclaredMethod("get"+propertyName.substring(0,1).toUpperCase()+propertyName.substring(1));
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object getValue() {
Object result = null;
writeMethod.setAccessible(true);
try {
result = readMethod.invoke(wrappedObject);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
public void setValue(Object value) {
writeMethod.setAccessible(true);
try {
writeMethod.invoke(wrappedObject, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
作者:ukyo--碳水化合物
出处:https://www.cnblogs.com/ukzq/p/17278582.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2019-04-01 [JSONObject/JSONArray] - 定制的JSON格式返回
2019-04-01 在新获取git中项目时出现的问题汇总