下面介绍几个工具类会在项目中用到
① 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;
- public class BeanUtils {
-
- 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;
- }
-
- public static <T> T instanceClass(Constructor<T> ctor, Object... args) throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException{
- makeAccessible(ctor);
- return ctor.newInstance(args);
- }
-
- 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);
- }
- }
- 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;
- 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;
- 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