前端之家收集整理的这篇文章主要介绍了
get set 注解 以及public属性 依赖注入该怎么写,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
public static void diDaoFiled(Object object){
Field[] fields = object.getClass().getFields();
for(Field field : fields){
if(field.isAnnotationPresent(ShopDi.class)){
ShopDi sd = field.getAnnotation(ShopDi.class);
String mn = sd.value();
if(mn == null || "".equals(mn.trim())){
mn = field.getName();
}
Object o = DaoUtil.createDaoFactory().getDao(mn);
try {
field.set(object,o);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static void diDaoNew(Object object){
Method[] ms = object.getClass().getDeclaredMethods();
for(Method m : ms){
if(m.isAnnotationPresent(ShopDi.class)){
ShopDi sd = m.getAnnotation(ShopDi.class);
String mn = sd.value();
if(mn == null || "".equals(mn.trim())){
mn = m.getName().substring(3);
mn = mn.substring(0,1).toLowerCase()+mn.substring(1);
}
Object o = DaoUtil.createDaoFactory().getDao(mn);
try {
m.invoke(object,o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public static void diDao(Object object){
Method[] ms = object.getClass().getDeclaredMethods();
for(Method m :ms){
String mn = m.getName();
if(mn.startsWith("set")){
mn = mn.substring(3);
mn = mn.substring(0,1).toLowerCase()+mn.substring(1);
Object o = DaoUtil.createDaoFactory().getDao(mn);
try {
m.invoke(object,o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}