1、StringUtil.java
package mvc.framework.util; import org.apache.commons.lang3.StringUtils; // 字符串工具类 public final class StringUtil { // 分割固定格式的字符串 public static String[] splitString(String str, String separator) { return StringUtils.splitByWholeSeparator(str, separator); } // 测试 public static void main(String[] args) { String para = "usercode=9001&username=jzh"; String[] arr = StringUtil.splitString(para, "&"); for (String temp : arr) { System.out.println(temp); } } }
2、BeanHelper.java。
package mvc.framework.helper; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; import java.util.Set; // Bean 助手类 public final class BeanHelper { // 测试 public static void main(String[] args) { // Bean容器 Map<Class<?>, Object> beanClassSet = new HashMap<Class<?>, Object>(); Class<?> beanClass = null; // 存放Bean类 try { beanClass = Class.forName("mvc.framework.util.StringUtil", true, Thread.currentThread().getContextClassLoader()); Object obj = beanClass.newInstance(); beanClassSet.put(beanClass, obj); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); } // 调用Bean类 try { // 获取实例 Object object = beanClassSet.get(beanClass); // 返回方法名为“splitString”的一个 Method对象,指定参数类型。 Method method = object.getClass().getMethod("splitString", new Class[] { String.class, String.class }); // 执行该方法 String[] arr = (String[]) method.invoke(object, new Object[] { new String("usercode=9001&username=jzh"), new String("&") }); for (String temp : arr) { System.out.println(temp); } } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } }