差異處
這裏顯示兩個版本的差異處。
下次修改 | 前次修改 | ||
java:guice:helloworld [2013/11/08 17:28] tony 建立 |
java:guice:helloworld [2023/06/25 09:48] (目前版本) |
||
---|---|---|---|
行 1: | 行 1: | ||
{{tag>java guice}} | {{tag>java guice}} | ||
- | ====== Hello World! ====== | + | ====== Guice - Hello World! ====== |
這個範例主要了解Guice如何實做Dependency Injection(DI)。 | 這個範例主要了解Guice如何實做Dependency Injection(DI)。 | ||
===== 準備 ===== | ===== 準備 ===== | ||
行 57: | 行 57: | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
- | System.setProperty("printer.mode", "console"); | ||
- | |||
Injector injector = Guice.createInjector(new PrinterModule()); | Injector injector = Guice.createInjector(new PrinterModule()); | ||
PrinterClient printerClient = injector.getInstance(PrinterClient.class); | PrinterClient printerClient = injector.getInstance(PrinterClient.class); | ||
行 76: | 行 74: | ||
@Override | @Override | ||
public void configure(Binder binder) { | public void configure(Binder binder) { | ||
- | String mode = System.getProperty("printer.mode"); | + | binder.bind(IPrintService.class).to(ConsolePrinter.class); |
- | if( mode.equals("console") ){ | + | |
- | binder.bind(IPrintService.class).to(ConsolePrinter.class); | + | |
- | }else { | + | |
- | throw new RuntimeException("Can't find suitable printer mode."); | + | |
- | } | + | |
} | } | ||
} | } | ||
行 89: | 行 82: | ||
\\ | \\ | ||
友藏內心獨白: 照這樣看來Guice是蠻簡單的! | 友藏內心獨白: 照這樣看來Guice是蠻簡單的! | ||
+ | ===== ===== | ||
+ | ---- | ||
+ | \\ | ||
+ | ~~DISQUS~~ |