下面介绍几个工具类会在项目中用到
① BeanUtils.java 对java反射中操作的一些封装
package com.ajunframework.beans.utils; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /** * @author ajun * @http://blog.csdn.net/ajun_studio */ public class BeanUtils { /** * 实例化一个class * @param <T> * @param clazz Person.class * @return */ public static <T> T instanceClass(Class<T> clazz){ if(!clazz.isInterface()){ try { return clazz.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return null; } /** * 通过构造函数实例化 * @param <T> * @param ctor * @param args * @return * @throws IllegalArgumentException * @throws InstantiationException * @throws IllegalAccessException * @throws InvocationTargetException */ public static <T> T instanceClass(Constructor<T> ctor, Object... args) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{ makeAccessible(ctor); return ctor.newInstance(args);//调用构造方法实例化 } /** * 查找某个class的方法 * @param clazz * @param methodName * @param paramTypes * @return * @throws SecurityException * @throws NoSuchMethodException */ public static Method findMethod(Class<?> clazz, String methodName, Class<?>... paramTypes){ try { return clazz.getMethod(methodName, paramTypes); } catch (NoSuchMethodException e) { return findDeclaredMethod(clazz, methodName, paramTypes);//返回共有的方法 } } public static Method findDeclaredMethod(Class<?> clazz, String methodName, Class<?>[] paramTypes){ try { return clazz.getDeclaredMethod(methodName, paramTypes); } catch (NoSuchMethodException ex) { if (clazz.getSuperclass() != null) { return findDeclaredMethod(clazz.getSuperclass(), methodName, paramTypes); } return null; } } public static Method [] findDeclaredMethods(Class<?> clazz){ return clazz.getDeclaredMethods(); } public static void makeAccessible(Constructor<?> ctor) { if ((!Modifier.isPublic(ctor.getModifiers()) || !Modifier.isPublic(ctor.getDeclaringClass().getModifiers())) && !ctor.isAccessible()) { ctor.setAccessible(true);//如果是私有的 设置为true 使其可以访问 } } public static Field[] findDeclaredFields(Class<?> clazz){ return clazz.getDeclaredFields(); } }
②BeanWrapper.java 用于设置bean中的属性值
package com.ajunframework.beans.utils; import java.beans.IntrospectionException; import java.beans.PropertyDescriptor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * @author ajun * @http://blog.csdn.net/ajun_studio */ public class BeanWrapper { private Object object; public BeanWrapper(Object object) { super(); this.object = object; } public void setPropertyValue(String name,Object value){ try { PropertyDescriptor pd = new PropertyDescriptor(name,this.object.getClass()); Method setMethod = pd.getWriteMethod(); setMethod.invoke(object, value); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }catch (IntrospectionException e) { e.printStackTrace(); } } public Object getPropertyValue(String name){ Object value=null; try { PropertyDescriptor pd = new PropertyDescriptor(name,this.object.getClass()); Method getMethod = pd.getReadMethod(); value = getMethod.invoke(object); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IntrospectionException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return value; } }
③PubConstant.java 读取constant.properties文件
package com.ajunframework.beans.utils; import java.io.IOException; import java.util.Properties; /** * @author ajun * @http://blog.csdn.net/ajun_studio **/ public class PubConstant { private static Properties properties= new Properties(); static{ try { properties.load(PubConstant.class.getClassLoader().getResourceAsStream("constant.properties")); } catch (IOException e) { e.printStackTrace(); } } public static String getValue(String key){ String value = (String)properties.get(key); return value.trim(); } }
下一节我们会做个测试 对现在的ioc