我正在尝试使用反射在SpringBootApplication中创建类的实例。为了实现相同的目的,我试图使用Class.forName(String ClassName)方法获取“Class”对象,但它会抛出一个带有嵌套ClassNotFoundException的ClassCastException。我有一个微弱的想法,Spring Boot有自己的类加载器,但它无法找到我的Java文件,尽管它位于主src文件夹中。有什么方法可以解决这个问题吗?
public static List<Object> createExcelBeans(List<LinkedList<String>> excelData, List<String> excelFields,
String className)
throws NumberFormatException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IOException {
List<Object> pojoList = new LinkedList<Object>();
/*
* getting the class instance of the specified name
*/
Class<?> pojoClass = Class.forName(className);
堆栈追踪:
java.lang.ClassNotFoundException: ExcelBean
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.Excel.ExcelService.createExcelBeans(ExcelService.java:39)
at com.Excel.ExcelReader.excelReaderService(ExcelReader.java:137)
at com.Excel.ExcelReader.excelToPojo(ExcelReader.java:43)