package com.myframework.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URL; public final class ClassUtil { private static final Logger LOGGER = LoggerFactory.getLogger(ClassUtil.class); /** * 获取类加载器 * * @return */ public static ClassLoader getClassLoader() { return Thread.currentThread().getContextClassLoader(); } public static String getClassPath() { String classpath = ""; URL resource = getClassLoader().getResource(""); if (resource != null) { classpath = resource.getPath(); } return classpath; } public static Class<?> loadClass(String className) { return loadClass(className, true); } public static Class<?> loadClass(String className, boolean isInitiatlized) { Class<?> cls; try { cls = Class.forName(className, isInitiatlized, getClassLoader()); } catch (ClassNotFoundException e) { LOGGER.error("加载类出错!", e); throw new RuntimeException(e); } return cls; } }