springboot,spring反射加载类踩坑⽂章⽬录
报错的地⽅
在使⽤springboot启动项⽬的时候,有通过反射加载类,当时⼤概使⽤的这个⽅法:
o = Class.forName("ample.demo.User",SystemClassLoader()).newInstance();
但是本地idea测试的时候是没有任何问题的,但是⼀打包就报错classNotFountEx类不到异常…
编写个测试项⽬,idea执⾏
就是简单的springboot项⽬,然后有个User和UserSpring类,UserSpring交给spring管理,然后在启动类地⽅打印信息:
结果是都没有问题:
测试项⽬,jar执⾏
将项⽬打包成jar然后在window上执⾏:
测试结果:
springboot反射加载类结论:
通过idea执⾏⽅法,使⽤的类加载器都是系统类加载器AppClassLoader,
通过执⾏jar⽂件,系统类加载器还是系统类加载器,但是加载jar⾥⾯class的时候使⽤的是org.springframework.boot.loader.LaunchedURLClassLoader,所以在使⽤系统类加载器去加载User的时候就会报错,不到这个类了!
猜想如果是spring容器的话呢?
测试,随便在Spring的⼀个类中写:
打印:
14:24:42.996 [http-apr-9001-exec-9] ntroller.TestController - aaa
14:24:43.335 [http-apr-9001-exec-9] INFO  ller.TestController - aaa
context:
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.URLClassLoader@64f6106c
User.class:WebappClassLoader
context:
delegate: false
repositories:
/
WEB-INF/classes/
----------> Parent Classloader:
java.URLClassLoader@64f6106c
User.class.parent:java.URLClassLoader@64f6106c
User.class.parent.parent:sun.misc.Launcher$AppClassLoader@18b4aac2
new User():WebappClassLoader
context:
delegate: false
repositories:
/WEB-INF/classes/
----------> Parent Classloader:
java.URLClassLoader@64f6106c
---------st.User@538b148
java.lang.ClassNotFoundException: st.User
at java.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
UserId(TestController.java:46)
flect.NativeMethodAccessorImpl.invoke0(Native Method)
flect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
flect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at flect.Method.invoke(Method.java:498)
at org.hod.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.hod.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.hod.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1039)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
spring启动流程面试回答at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at at.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.tor.CoyoteAdapter.service(CoyoteAdapter.java:442)
at http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
at AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
at at.util.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2517)
at at.util.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2506)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at at.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
spring容器使⽤反射加载类问题
spring容器通过的是WebappClassLoader类加载器去加载的,WebappClassLoader的⽗类加载器是URLClassLoader,如果直接使⽤系统类加载器也会出现如上问题.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。