{"id":2053,"date":"2017-10-13T15:30:49","date_gmt":"2017-10-13T11:18:16","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2056"},"modified":"2017-10-13T15:36:25","modified_gmt":"2017-10-13T07:36:25","slug":"%e9%9d%a0%e6%8e%92%e7%85%a7%e7%89%87%e5%92%8c%e6%8b%8d%e8%a7%86%e9%a2%91%e5%81%9a%e5%88%b0%e6%9c%88%e5%85%a510%e4%b8%87-12","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2053","title":{"rendered":"\u8be6\u89e3Spring\u4e2d\u7684ApplicationListener\u548cContextRefreshedEvent"},"content":{"rendered":"<p>\n\tApplicationListener\u548cContextRefreshedEvent\u4e00\u822c\u90fd\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u3002\u6700\u8fd1\u5728\u9762\u8bd5\u4e2d\u95ee\u5230\u4e86\u88ab\u9762\u8bd5\u8005\u5bf9\u4e8e\u8fd9\u4e24\u4e2a\u7684\u7528\u6cd5\uff0c\u9762\u8bd5\u8005\u5927\u591a\u6570\u88ab\u95ee\u61f5\u4e86\u3002\u53ef\u89c1\u57fa\u7840\u77e5\u8bc6\u7684\u638c\u63e1\u7a0b\u5ea6\u3002\u57fa\u4e8e\u6b64\u672c\u6587\u5c06\u4ecb\u7ecd\u5b83\u4eec\u7684\u7528\u6cd5\u3002\n<\/p>\n<p>\n\t\u4e8b\u4ef6\u673a\u5236\u4f5c\u4e3a\u4e00\u79cd\u7f16\u7a0b\u673a\u5236\uff0c\u5728\u8bb8\u591a\u8bed\u8a00\u4e2d\u90fd\u63d0\u4f9b\u4e86\u652f\u6301\u3002<a href=\"https:\/\/www.xttblog.com\/?tag=java\" name=\"java\" target=\"_blank\" title=\"java\">JAVA<\/a>\u8bed\u8a00\u4e5f\u4e0d\u4f8b\u5916\uff0c<a href=\"https:\/\/www.xttblog.com\/?tag=java\" name=\"java\" target=\"_blank\" title=\"java\">java<\/a>\u4e2d\u7684\u4e8b\u4ef6\u673a\u5236\u7684\u53c2\u4e0e\u8005\u67093\u79cd\u89d2\u8272:\n<\/p>\n<ol>\n<li>\n\t\tevent object\n\t<\/li>\n<li>\n\t\tevent source\n\t<\/li>\n<li>\n\t\tevent listener\n\t<\/li>\n<\/ol>\n<p>\n\t\u8fd9\u4e09\u4e2a\u89d2\u8272\u7684\u542b\u4e49\u5b57\u9762\u4e0a\u5f88\u597d\u89e3\uff0c\u5b83\u4eec\u5c31\u5b9a\u4e49\u4e86\u4e8b\u4ef6\u673a\u5236\u7684\u4e00\u4e2a\u57fa\u672c\u6a21\u578b\u3002\u4f5c\u4e3a\u4e00\u79cd\u5e38\u7528\u7684\u7f16\u7a0b\u8bbe\u8ba1\u673a\u5236\uff0c\u8bb8\u591a\u5f00\u6e90\u6846\u67b6\u7684\u8bbe\u8ba1\u4e2d\u90fd\u4f7f\u7528\u4e86\u4e8b\u4ef6\u673a\u5236\u3002SpringFramework\u4e5f\u4e0d\u4f8b\u5916\u3002\n<\/p>\n<p>\n\t\u5728IOC\u7684\u5bb9\u5668\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u5f53\u6240\u6709\u7684bean\u90fd\u5df2\u7ecf\u5904\u7406\u5b8c\u6210\u4e4b\u540e\uff0c<a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">spring<\/a> ioc\u5bb9\u5668\u4f1a\u6709\u4e00\u4e2a\u53d1\u5e03\u4e8b\u4ef6\u7684\u52a8\u4f5c\u3002\u4ece AbstractApplicationContext \u7684\u6e90\u7801\u4e2d\u5c31\u53ef\u4ee5\u770b\u51fa\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nprotected void finishRefresh() {\r\n\t\/\/ Initialize lifecycle processor for this context.\r\n\tinitLifecycleProcessor();\r\n\t\/\/ Propagate refresh to lifecycle processor first.\r\n\tgetLifecycleProcessor().onRefresh();\r\n\t\/\/ Publish the final event.\r\n\tpublishEvent(new ContextRefreshedEvent(this));\r\n\t\/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n\t\/\/ Participate in LiveBeansView MBean, if active.\r\n\tLiveBeansView.registerApplicationContext(this);\r\n}<\/pre>\n<p>\n\t\u8fd9\u6837\uff0c\u5f53ioc\u5bb9\u5668\u52a0\u8f7d\u5904\u7406\u5b8c\u76f8\u5e94\u7684bean\u4e4b\u540e\uff0c\u4e5f\u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u673a\u4f1a\uff08\u5148\u6709InitializingBean\uff0c\u540e\u6709ApplicationListener&lt;ContextRefreshedEvent&gt;\uff09\uff0c\u53ef\u4ee5\u53bb\u505a\u4e00\u4e9b\u81ea\u5df1\u60f3\u505a\u7684\u4e8b\u3002\u5176\u5b9e\u8fd9\u4e5f\u5c31\u662fspring ioc\u5bb9\u5668\u7ed9\u63d0\u4f9b\u7684\u4e00\u4e2a\u6269\u5c55\u7684\u5730\u65b9\u3002\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u8fd9\u4e2a\u6269\u5c55\u673a\u5236\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\norg.springframework.context.ApplicationEvent \r\norg.springframework.context.ApplicationListener<\/pre>\n<p>\n\t\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u5c31\u662f\uff0c\u8ba9\u6211\u4eec\u7684bean\u5b9e\u73b0ApplicationListener\u63a5\u53e3\uff0c\u8fd9\u6837\u5f53\u53d1\u5e03\u4e8b\u4ef6\u65f6\uff0c<a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">spring<\/a>\u7684ioc\u5bb9\u5668\u5c31\u4f1a\u4ee5\u5bb9\u5668\u7684\u5b9e\u4f8b\u5bf9\u8c61\u4f5c\u4e3a\u4e8b\u4ef6\u6e90\u7c7b\uff0c\u5e76\u4ece\u4e2d\u627e\u5230\u4e8b\u4ef6\u7684\u76d1\u542c\u8005\uff0c\u6b64\u65f6ApplicationListener\u63a5\u53e3\u5b9e\u4f8b\u4e2d\u7684onApplicationEvent(E event)\u65b9\u6cd5\u5c31\u4f1a\u88ab\u8c03\u7528\uff0c\u6211\u4eec\u7684\u903b\u8f91\u4ee3\u7801\u5c31\u4f1a\u5199\u5728\u6b64\u5904\u3002\u8fd9\u6837\u6211\u4eec\u7684\u76ee\u7684\u5c31\u8fbe\u5230\u4e86\u3002\u4f46\u8fd9\u4e5f\u5e26\u6765\u4e00\u4e2a\u601d\u8003\uff0c\u6709\u4eba\u53ef\u80fd\u4f1a\u60f3\uff0c\u8fd9\u6837\u7684\u4ee3\u7801\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">spring<\/a>\u7684InitializingBean\u63a5\u53e3\u6765\u5b9e\u73b0\u554a\uff0c\u4e5f\u4f1a\u88ab<a href=\"https:\/\/www.xttblog.com\/?tag=Spring\" name=\"Spring\" target=\"_blank\" title=\"Spring\">spring<\/a>\u5bb9\u5668\u53bb\u81ea\u52a8\u8c03\u7528\uff0c\u4f46\u662f\u5927\u5bb6\u5e94\u8be5\u60f3\u5230\uff0c\u5982\u679c\u6211\u4eec\u73b0\u5728\u60f3\u505a\u7684\u4e8b\uff0c\u662f\u5fc5\u987b\u8981\u7b49\u5230\u6240\u6709\u7684bean\u90fd\u88ab\u5904\u7406\u5b8c\u6210\u4e4b\u540e\u518d\u8fdb\u884c\uff0c\u6b64\u65f6InitializingBean\u63a5\u53e3\u7684\u5b9e\u73b0\u5c31\u4e0d\u5408\u9002\u4e86\uff0c\u6240\u4ee5\u9700\u8981\u6df1\u523b\u7406\u89e3\u4e8b\u4ef6\u673a\u5236\u7684\u5e94\u7528\u573a\u5408\u3002\n<\/p>\n<p>\n\t\u66fe\u7ecf\u6709\u4e00\u4f4d\u540c\u4e8b\u5229\u7528&nbsp;ApplicationListener\uff0c\u91cd\u590d\u52a0\u8f7d\u4e86\u597d\u51e0\u6b21 xml \u914d\u7f6e\u6587\u4ef6\u3002\u6240\u4ee5\u57fa\u7840\u77e5\u8bc6\u4e00\u5b9a\u8981\u638c\u63e1\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class ApplicationContextListener implements ApplicationListener&lt;ContextRefreshedEvent&gt; {\r\n    private static Logger _log = LoggerFactory.getLogger(ApplicationContextListener.class);\r\n    @Override\r\n    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {\r\n        \/\/ root application context\r\n        if(null == contextRefreshedEvent.getApplicationContext().getParent()) {\r\n            _log.debug(&quot;&gt;&gt;&gt;&gt;&gt; spring\u521d\u59cb\u5316\u5b8c\u6bd5 &lt;&lt;&lt;&lt;&lt;&quot;);\r\n            \/\/ spring\u521d\u59cb\u5316\u5b8c\u6bd5\u540e\uff0c\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u6240\u6709\u4f7f\u7528BaseService\u6ce8\u89e3\u7684initMapper\u65b9\u6cd5\r\n            Map&lt;String, Object&gt; baseServices = \r\n\t\t\tcontextRefreshedEvent.getApplicationContext().getBeansWithAnnotation(BaseService.class);\r\n            for(Object service : baseServices.values()) {\r\n                _log.debug(&quot;&gt;&gt;&gt;&gt;&gt; {}.initMapper()&quot;, service.getClass().getName());\r\n                try {\r\n                    Method initMapper = service.getClass().getMethod(&quot;initMapper&quot;);\r\n                    initMapper.invoke(service);\r\n                } catch (Exception e) {\r\n                    _log.error(&quot;\u521d\u59cb\u5316BaseService\u7684initMapper\u65b9\u6cd5\u5f02\u5e38&quot;, e);\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n            \/\/ \u7cfb\u7edf\u5165\u53e3\u521d\u59cb\u5316\uff0c\u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n            Map&lt;String, BaseInterface&gt; baseInterfaceBeans = \r\n\t\t\tcontextRefreshedEvent.getApplicationContext().getBeansOfType(BaseInterface.class);\r\n            for(Object service : baseInterfaceBeans.values()) {\r\n                _log.debug(&quot;&gt;&gt;&gt;&gt;&gt; {}.init()&quot;, service.getClass().getName());\r\n                try {\r\n                    Method init = service.getClass().getMethod(&quot;init&quot;);\r\n                    init.invoke(service);\r\n                } catch (Exception e) {\r\n                    _log.error(&quot;\u521d\u59cb\u5316BaseInterface\u7684init\u65b9\u6cd5\u5f02\u5e38&quot;, e);\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u4ee5\u4e0a\u5c31\u662fApplicationListener\u548cContextRefreshedEvent\u7684\u76f8\u5173\u7528\u6cd5\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2053\">\u8be6\u89e3Spring\u4e2d\u7684ApplicationListener\u548cContextRefreshedEvent<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>ApplicationListener\u548cContextRefreshedEvent\u4e00\u822c\u90fd\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u3002\u6700\u8fd1\u5728\u9762 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1528,69,551,1529],"class_list":["post-2053","post","type-post","status-publish","format-standard","hentry","category-java","tag-ioc","tag-java","tag-spring","tag-1529"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2053"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2053\/revisions"}],"predecessor-version":[{"id":2152,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2053\/revisions\/2152"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}