新手最近在学习一个手写mvc框架的学习过程中,在通过全限定名反射生成实例的时候,出现了ClassNotFoundException,但是在本地测试的时候用同样的方法却没有报错,麻烦大佬看一下是什么原因造成的?谢谢!
以下是DispatcherServlet初始化的代码,className可以正确打印
public void init() throws ServletException {
//bean标签中的class属性所指向的类加载出来,并将对象也创建出来(通过反射)
//1.加载读取smart_mvc.xml
InputStream is = getClass().getClassLoader().getResourceAsStream("smart_mvc.xml");
//2.加载其中的数据(树状的结果),借助dom4j相关类和接口
SAXReader saxReader = new SAXReader();
try {
//返回的是一个document对象,是一个树状的结构
Document document = saxReader.read(is);
//获取xml文件的根路径
Element root = document.getRootElement();
//获取根节点中的所有子节点
List<Element> elements = root.elements();
List<Object> beans = new ArrayList<>();
//读取元素的目的是将class属性所指向的路径对应的类加载成对象
for (Element element : elements) {
//获取这个元素对象的属性,Element相当于一个bean标签
String className = element.attributeValue("class");
System.out.println("className=" + className);
//根据这个类的路径创建这个类的对象
Object bean = Class.forName(className).getDeclaredConstructor().newInstance();
System.out.println("bean: " + bean);
beans.add(bean);
}
System.out.println("after==>" + beans);
} catch (Exception e) {
e.printStackTrace();
}
}
报错内容:
java.lang.ClassNotFoundException: org.example.smart_mvc.controller.HelloController
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1407)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1215)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:340)
at org.example.smart_mvc.web.servlet.DispatcherServlet.init(DispatcherServlet.java:49)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1164)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1117)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:788)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:128)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:382)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:895)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1722)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:832)
测试代码可以正确执行
@Test
public void test1() throws Exception {
String s = "org.example.smart_mvc.controller.HelloController";
Object o = Class.forName(s).getDeclaredConstructor().newInstance();
System.out.println(o);
}