java反射简单例子 冷不防 2022-06-05 08:11 222阅读 0赞 作者:meepo 链接:https://www.zhihu.com/question/24304289/answer/150310001 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 今天又被学员问到这个问题了,索性专门做个小知识点来讲解反射是怎么起作用的,发现还是可以在不学习Spring等框架的基础上,举一些例子来辅助理解反射如何起作用。 鉴于手机知乎上看代码格式会乱掉,所以代码都截图 1. 首先准备两个很简单的业务类 \<img src="https://pic2.zhimg.com/50/v2-aacd60547555dd41116a3288ca549785\_hd.png" data-rawwidth="495" data-rawheight="206" class="origin\_image zh-lightbox-thumb" width="495" data-original="https://pic2.zhimg.com/v2-aacd60547555dd41116a3288ca549785\_r.png"\> ![v2-aacd60547555dd41116a3288ca549785_hd.jpg][] \<img src="https://pic1.zhimg.com/50/v2-4410be8aedf7a91db4350ca4e9b89e6c\_hd.png" data-rawwidth="496" data-rawheight="202" class="origin\_image zh-lightbox-thumb" width="496" data-original="https://pic1.zhimg.com/v2-4410be8aedf7a91db4350ca4e9b89e6c\_r.png"\> ![v2-4410be8aedf7a91db4350ca4e9b89e6c_hd.jpg][] 2. 非反射方式切换不同的业务方法调用 \<img src="https://pic2.zhimg.com/50/v2-6d07614e59386775c738db0e573ca9a5\_hd.png" data-rawwidth="538" data-rawheight="207" class="origin\_image zh-lightbox-thumb" width="538" data-original="https://pic2.zhimg.com/v2-6d07614e59386775c738db0e573ca9a5\_r.png"\> ![v2-6d07614e59386775c738db0e573ca9a5_hd.jpg][] 当需要从第一个业务方法切换到第二个业务方法的时候,使用非反射方式,必须修改代码,并且重新编译运行,才可以达到效果 3. 反射方式 使用反射方式,首先准备一个配置文件,就叫做spring.txt吧, 放在src目录下。 里面存放的是类的名称,和要调用的方法名。 在测试类Test中,首先取出类名称和方法名,然后通过反射去调用这个方法。 当需要从调用第一个业务方法,切换到调用第二个业务方法的时候,不需要修改一行代码,也不需要重新编译,只需要修改配置文件spring.txt,再运行即可。 \<img src="https://pic2.zhimg.com/50/v2-77ffdf1131e3cf1cfea3c48aac79f951\_hd.png" data-rawwidth="346" data-rawheight="144" class="content\_image" width="346"\> ![v2-77ffdf1131e3cf1cfea3c48aac79f951_hd.jpg][] \<img src="https://pic3.zhimg.com/50/v2-80cd1a6134c5e5c051807ddb949e9282\_hd.png" data-rawwidth="884" data-rawheight="678" class="origin\_image zh-lightbox-thumb" width="884" data-original="https://pic3.zhimg.com/v2-80cd1a6134c5e5c051807ddb949e9282\_r.png"\> ![v2-80cd1a6134c5e5c051807ddb949e9282_hd.jpg][] 使用这个例子,可以较好得理解反射的一个应用场景。 这也是[Spring框架][Spring]的最基本的原理,只是它做的更丰富,安全,健壮。 这个例子如果有兴趣自己照着做一遍,我放在这里: [http://how2j.cn/k/reflection/reflection-usage/1111.html][http_how2j.cn_k_reflection_reflection-usage_1111.html] ## 二、反射的主要用途 ## 很多人都认为反射在实际的Java开发应用中并不广泛,其实不然。 当我们在使用IDE(如Eclipse,IDEA)时,当我们输入一个对象或类并想调用它的属性或方法时,一按点号,编译器就会自动列出它的属性或方法,这里就会用到反射。 反射最重要的用途就是开发各种通用框架。 很多框架(比如Spring)都是配置化的(比如通过XML文件配置JavaBean,Action之类的),为了保证框架的通用性,它们可能需要根据配置文件加载不同的对象或类,调用不同的方法,这个时候就必须用到反射——运行时动态加载需要加载的对象。 举一个例子,在运用Struts 2框架的开发中我们一般会在struts.xml里去配置Action,比如: <table style="margin:0px; padding:0px; border:none; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:16px; vertical-align:middle; border-collapse:collapse; border-spacing:0px; width:auto; max-width:65em; empty-cells:show"> <tbody style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline"> <tr style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; height:35px"> <td style="margin:0px; padding:0px; border:none; outline:0px; font-style:inherit; font-family:inherit; font-size:16px; vertical-align:middle; height:35px; white-space:nowrap"> <pre style="margin-top:0px; margin-bottom:0px; padding:0px 1em 0px 0px; border:none; outline:0px; font-weight:inherit; font-style:inherit; font-family:Menlo,Consolas,'Source Code Pro',Inconsolata,Monaco,monospace,'Microsoft YaHei','PingFang SC','Hiragino Sans GB','WenQuanYi Micro Hei'; font-size:0.85rem; vertical-align:baseline; overflow:auto; color:rgb(211,211,211); line-height:1.3rem; text-align:right; background:none"> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 1 </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 2 </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 3 </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 4 </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 5 </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; font-size:13.6px; vertical-align:baseline; line-height:1.3rem"> 6 </div></pre> </td> <td style="margin:0px; padding:0px; border:none; outline:0px; font-style:inherit; font-family:inherit; font-size:16px; vertical-align:middle; height:35px"> <pre style="margin-top:0px; margin-bottom:0px; padding:0px; border:none; outline:0px; font-weight:inherit; font-style:inherit; font-family:Menlo,Consolas,'Source Code Pro',Inconsolata,Monaco,monospace,'Microsoft YaHei','PingFang SC','Hiragino Sans GB','WenQuanYi Micro Hei'; font-size:1em; vertical-align:baseline; overflow:auto; color:rgb(77,77,76); line-height:1.3rem; background:none"> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"><<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">action</span> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">name</span>=<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(113,140,0)">"login"</span></span> </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">class</span>= <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(113,140,0)">"org.ScZyhSoft.test.action.SimpleLoginAction"</span> </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">method</span>= <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(113,140,0)">"execute"</span>> </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"><<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">result</span>></span>/shop/shop-index.jsp <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"></<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">result</span>></span> </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"><<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">result</span> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">name</span>=<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(113,140,0)">"error"</span>></span>login.jsp <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"></<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">result</span>></span> </div> <div style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; line-height:1.3rem"> <span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline; color:rgb(200,40,41)"></<span style="margin:0px; padding:0px; border:0px; outline:0px; font-weight:inherit; font-style:inherit; font-family:inherit; vertical-align:baseline">action</span>></span> </div></pre> </td> </tr> </tbody> </table> 配置文件与Action建立了一种映射关系,当View层发出请求时,请求会被StrutsPrepareAndExecuteFilter拦截,然后StrutsPrepareAndExecuteFilter会去动态地创建Action实例。 ——比如我们请求login.action,那么StrutsPrepareAndExecuteFilter就会去解析struts.xml文件,检索action中name为login的Action,并根据class属性创建SimpleLoginAction实例,并用invoke方法来调用execute方法,这个过程离不开反射。 对与框架开发人员来说,反射虽小但作用非常大,它是各种容器实现的核心。而对于一般的开发者来说,不深入框架开发则用反射用的就会少一点,不过了解一下框架的底层机制有助于丰富自己的编程思想,也是很有益的。 ## 四、反射的一些注意事项(待补充) ## 由于反射会额外消耗一定的系统资源,因此如果不需要动态地创建一个对象,那么就不需要用反射。 另外,反射调用方法时可以忽略权限检查,因此可能会破坏封装性而导致安全问题 [v2-aacd60547555dd41116a3288ca549785_hd.jpg]: /images/20220605/831376dd62bd45079bf8eda621b0bb82.png [v2-4410be8aedf7a91db4350ca4e9b89e6c_hd.jpg]: /images/20220605/feb28f72fa004d6fb392bde6506846d8.png [v2-6d07614e59386775c738db0e573ca9a5_hd.jpg]: /images/20220605/32985872cbd7414aabd6fd6e4953eaa2.png [v2-77ffdf1131e3cf1cfea3c48aac79f951_hd.jpg]: /images/20220605/e0802d8f65234ac5a13169bbd5238bfa.png [v2-80cd1a6134c5e5c051807ddb949e9282_hd.jpg]: /images/20220605/cbff0090c3774acfa10222391dec7862.png [Spring]: https://link.zhihu.com/?target=http%3A//how2j.cn/k/spring/spring-ioc-di/87.html [http_how2j.cn_k_reflection_reflection-usage_1111.html]: https://link.zhihu.com/?target=http%3A//how2j.cn/k/reflection/reflection-usage/1111.html
还没有评论,来说两句吧...