Guice:轻量级IOC/DI 容器
2016-04-27 18:08
525 查看
Guice 简介
Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter方法)进行注入。Guice采用Java加注解的方式进行托管对象的配置,充分利用IDE编译器的类型安全检查功能和自动重构功能,使得配置的更改也是类型安全的。Guice提供模块对应的抽象module,使得架构和设计的模块概念产物与代码中的module类一一对应,更加便利的组织和梳理模块依赖关系,利于整体应用内部的依赖关系维护,而其他IOC框架是没有对应物的。此外,借助privateModule的功能,可以实现模块接口的明确导出和实现封装,使得支持多数据源这类需求实现起来异常简单。示例代码
package com.conquer.comutils.core.guice.business; public interface HelloWorld { String sayHello(); }
package com.conquer.comutils.core.guice.business; import javax.inject.Singleton; @Singleton// 被 di 容器 只实例化 一次 public class HelloWorldImpl implements HelloWorld { public HelloWorldImpl() { System.out.println("HelloWorldImpl constructs"); } @Override public String sayHello() { return "Hello, world!"; } }
package com.conquer.comutils.core.guice.business; import javax.inject.Inject;// 使用 java 规范的 inject 注解,避免依赖 guice import javax.inject.Named; public class MyBusiness { @Inject // @Named("driver")// 多个实例的注入选择,也可以通过 继承 Qualifier实现不同的分类器 来实现区分 private HelloWorld helloWorld; // @Inject // public HelloResource(HelloWorld helloWorld) { // this.helloWorld = helloWorld; // } public String test() { return helloWorld.sayHello(); } }以上三个类是我们的业务代码,可以看到他们没有任何的 guice 代码侵入,仅仅是依赖了一些 javax inject 规范而已
Guice 的 Module,架构设计中的模块的对应物,起到外置配置类结构关系的作用
package com.conquer.comutils.core.guice; import com.conquer.comutils.core.guice.business.HelloWorld; import com.conquer.comutils.core.guice.business.HelloWorldImpl; import com.conquer.comutils.core.guice.business.MyBusiness; import com.google.inject.Binder; import com.google.inject.Module; // 在自定义的Module类中进行绑定 public class MyGuiceModule implements Module { public void configure(Binder binder) { binder.bind(MyBusiness.class); binder.bind(HelloWorld.class).to(HelloWorldImpl.class) // 使用 javax.inject.Singleton() 标注 实现类 实现单例 // .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建 ; // try { // 默认使用无参构造方法创建实例,也可以通过这种方式指定创建时候使用的构造方法 // binder.bind(HelloWorld.class).toConstructor(HelloWorldImpl.class.getConstructor()); // } catch (NoSuchMethodException e) { // e.printStackTrace(); // } } }测试类:
package com.conquer.comutils.core.guice; import com.conquer.comutils.core.guice.business.HelloWorld; import com.conquer.comutils.core.guice.business.HelloWorldImpl; import com.conquer.comutils.core.guice.business.MyBusiness; import com.google.inject.Binder; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Module; public class TestGuice { public static void main(String[] args) { Injector inj = Guice.createInjector(new Module() { @Override public void configure(Binder binder) { binder.bind(HelloWorld.class).to(HelloWorldImpl.class) // 使用 javax.inject.Singleton() 标注 实现类 实现单例 // .in(com.google.inject.Scopes.SINGLETON)// 这里设置单例,默认不是单例的而是每次创建 ; } }); for (int i = 0; i < 2; i++) { HelloWorld hw = inj.getInstance(HelloWorld.class); System.out.println(hw);// 设置 com.google.inject.Scopes.SINGLETON 这里打印同一个对象 System.out.println(hw.sayHello()); } System.out.println("=========================="); // test business Injector injector = Guice.createInjector(new MyGuiceModule()); MyBusiness helloResource = injector.getInstance(MyBusiness.class); String test = helloResource.test(); System.out.println("test: " + test); } }输出:
HelloWorldImpl constructs com.conquer.comutils.core.guice.business.HelloWorldImpl@3b58fa97 Hello, world! com.conquer.comutils.core.guice.business.HelloWorldImpl@3b58fa97 Hello, world! ========================== HelloWorldImpl constructs test: Hello, world!
相关文章推荐
- 四种方法实现UITableView的cell高度自动计算
- UINavigationController下view及子view的坐标及高度
- Android相关属性的介绍:android:exported = true
- ios集成环信 easeUI SDK 3.0 SDK 2.0 出错遇到的所有问题
- Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Must specify unique android
- SSH RSA host key for has changed and you have requeste
- 自定义UITabbarItem的title字体颜色
- Android Studio在build的时候出现transformClassesWithDexForDebug
- 303. Range Sum Query - Immutable
- Arduino ESP8266编程深入要点
- Arduino ESP8266编程深入要点
- android studio 启动项目时“Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled”
- Java 7 关于break和continue后的标签
- 5.0systemUI启动流程
- UItabBar上添加红点
- iOS开发--UIImage转化成NSData
- boundingRectWithSize 的使用, 计算UILable高度, 包含Emoji及多属性string.
- git Pull Request 是什么意思?
- Tiled GPU perf. warning: Backbuffer was not cleared/discarded, doing Render.OpaqueGeometry/RenderFor
- Java常见错误-“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误