{"id":520,"date":"2008-08-04T00:00:00","date_gmt":"2008-08-03T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=520"},"modified":"2011-07-15T09:22:33","modified_gmt":"2011-07-15T01:22:33","slug":"google-guice-example","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=520","title":{"rendered":"Google Guice Example"},"content":{"rendered":"<p><DIV>\u6700\u8fd1\u5728\u7814\u7a76Google \u7684 Guice\uff0c\u611f\u89c9\u7528\u8d77\u6765\u633a\u7b80\u5355\u7684\uff0c\u5165\u95e8\u5e94\u8be5\u6bd4Spring\u8981\u5bb9\u6613\u7684\u591a\uff0c\u8fd9\u4e5f\u8bb8\u662f\u56e0\u4e3aGuice\u521a\u521a\u51fa\u4e16\uff0c\u5f88\u529f\u80fd\u8fd8\u6ca1\u6709\u5b8c\u5584\u5427\uff0c\u4f46\u636e\u8bf4Google\u5185\u90e8\u5df2\u7ecf\u5728\u7528\u4e86\uff0c\u6211\u60f3\u518d\u8fc7\u4e00\u6bb5\u65f6\u95f4\u53d6Spring\u800c\u4ee3\u4e4b\u4e5f\u4e0d\u662f\u6ca1\u6709\u53ef\u80fd\uff0c\u81f3\u5c11\u4e5f\u80fd\u8ddfSpring\u5e73\u5206\u5929\u4e0b\u5427\u3002<\/DIV><br \/>\n<DIV>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u5b9e\u4f8b<\/DIV><br \/>\n<DIV>1.\u4e1a\u52a1\u903b\u8f91\u63a5\u53e3<\/DIV><br \/>\n<DIV>package com.hawkunion.guice;<\/DIV><br \/>\n<DIV>\/**<\/DIV><br \/>\n<DIV>*<\/DIV><br \/>\n<DIV>* @author \u675c\u5e86\u660e<\/DIV><br \/>\n<DIV>*\/<\/DIV><br \/>\n<DIV>public interface Work {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public void sayHello(String userName);<\/DIV><br \/>\n<DIV>}<\/DIV><br \/>\n<DIV>2.\u5b9e\u73b0\u63a5\u53e3\u7684\u4e1a\u52a1\u903b\u8f91<\/DIV><br \/>\n<DIV>package com.hawkunion.guice;<\/DIV><br \/>\n<DIV>\/**<\/DIV><br \/>\n<DIV>*<\/DIV><br \/>\n<DIV>* @author \u675c\u5e86\u660e<\/DIV><br \/>\n<DIV>*\/<\/DIV><br \/>\n<DIV>public class WorkImpl implements Work{<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public void sayHello(String userName) {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&#8220;Hello! \u6b22\u8fce\u4f60\uff1a&#8221;+userName);<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>}<\/DIV><br \/>\n<DIV>3.\u8981\u7528\u5230\u4e1a\u52a1\u903b\u8f91\u7684\u7c7b<\/DIV><br \/>\n<DIV>package com.hawkunion.guice;<\/DIV><br \/>\n<DIV>import com.google.inject.Inject;<\/DIV><br \/>\n<DIV>\/**<\/DIV><br \/>\n<DIV>*<\/DIV><br \/>\n<DIV>* @author \u675c\u5e86\u660e<\/DIV><br \/>\n<DIV>*\/<\/DIV><br \/>\n<DIV>public class RunWorkClass {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; private Work work = null;<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; @Inject<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public void setWork(Work workimpl){<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.work = workimpl;<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public void runWork(){<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; work.sayHello(&#8220;\u963f\u675c&#8221;);<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>}<\/DIV><br \/>\n<DIV>4.Module<\/DIV><br \/>\n<DIV>package com.hawkunion.guice;<\/DIV><br \/>\n<DIV>import com.google.inject.Binder;<\/DIV><br \/>\n<DIV>import com.google.inject.Module;<\/DIV><br \/>\n<DIV>\/**<\/DIV><br \/>\n<DIV>*<\/DIV><br \/>\n<DIV>* @author \u675c\u5e86\u660e<\/DIV><br \/>\n<DIV>*\/<\/DIV><br \/>\n<DIV>public class MyModule implements Module {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public void configure(Binder binder) {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; binder.bind(Work.class).to(WorkImpl.class);<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }<\/DIV><br \/>\n<DIV>}<\/DIV><br \/>\n<DIV>5.\u6d4b\u8bd5<\/DIV><br \/>\n<DIV>package com.hawkunion.guice;<\/DIV><br \/>\n<DIV>import com.google.inject.Guice;<\/DIV><br \/>\n<DIV>import com.google.inject.Injector;<\/DIV><br \/>\n<DIV>import com.google.inject.Module;<\/DIV><br \/>\n<DIV>\/**<\/DIV><br \/>\n<DIV>*<\/DIV><br \/>\n<DIV>* @author \u675c\u5e86\u660e<\/DIV><br \/>\n<DIV>*\/<\/DIV><br \/>\n<DIV>public class TestWork {<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; public static void main(String args[]){<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RunWorkClass runWorkClass = new RunWorkClass();<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Module module = new MyModule();<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Injector in = Guice.createInjector(module);\/\/\u628amodule\u7ed9Guice<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in.injectMembers(runWorkClass);\/\/\u8ba9Guice\u628a\u9700\u8981\u6ce8\u5165\u7684\u4e1a\u52a1\u903b\u8f91\u6ce8\u5165\u7ed9\u5bf9\u8c61<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; runWorkClass.runWork();\/\/&nbsp;&nbsp; \u8fd0\u884cwork<\/DIV><br \/>\n<DIV>&nbsp;&nbsp;&nbsp;&nbsp; }<\/DIV><br \/>\n<DIV>}<\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u7814\u7a76Google \u7684 Guice\uff0c\u611f\u89c9\u7528\u8d77\u6765\u633a\u7b80\u5355\u7684\uff0c\u5165\u95e8\u5e94\u8be5\u6bd4Spring\u8981\u5bb9\u6613\u7684\u591a\uff0c\u8fd9\u4e5f\u8bb8\u662f\u56e0\u4e3aGuice\u521a\u521a\u51fa\u4e16\uff0c\u5f88\u529f\u80fd\u8fd8\u6ca1\u6709\u5b8c\u5584\u5427\uff0c\u4f46\u636e\u8bf4Google\u5185\u90e8\u5df2\u7ecf\u5728\u7528\u4e86\uff0c\u6211\u60f3\u518d\u8fc7\u4e00\u6bb5\u65f6\u95f4\u53d6Spring\u800c\u4ee3\u4e4b\u4e5f\u4e0d\u662f\u6ca1\u6709\u53ef\u80fd\uff0c\u81f3\u5c11\u4e5f\u80fd\u8ddfSpring\u5e73\u5206\u5929\u4e0b\u5427\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u5b9e\u4f8b 1.\u4e1a\u52a1\u903b\u8f91\u63a5\u53e3 package com.hawkunion.guice; \/** * * @author \u675c\u5e86\u660e *\/ public interface Work { &nbsp;&nbsp;&nbsp;&nbsp; public void sayHello(String userName); } 2.\u5b9e\u73b0\u63a5\u53e3\u7684\u4e1a\u52a1\u903b\u8f91 package com.hawkunion.guice; \/** * * @author \u675c\u5e86\u660e *\/ public class WorkImpl implements Work{ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; public void sayHello(String userName) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&#8220;Hello! \u6b22\u8fce\u4f60\uff1a&#8221;+userName); &nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } 3.\u8981\u7528\u5230\u4e1a\u52a1\u903b\u8f91\u7684\u7c7b package com.hawkunion.guice; import &hellip; <a href=\"https:\/\/www.strongd.net\/?p=520\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Google Guice Example<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[74],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-java","tag-google-guice"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=520"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":850,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/520\/revisions\/850"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}