Guice是由Google大牛Bob lee开发的一款绝对轻量级的java IoC容器。其优点在于:java
Guice和spring各有所长,Guice更适合与嵌入式或者高性能但项目简单方案,如OSGI容器,spring更适合大型项目组织。git
在咱们谈到IOC框架,首先咱们的话题将是构造,属性以及函数注入方式,Guice的实现只须要在构造函数,字段,或者注入函数上标注@Inject,如:github
public class OrderServiceImpl implements OrderService { private ItemService itemService; private PriceService priceService; @Inject public OrderServiceImpl(ItemService itemService, PriceService priceService) { this.itemService = itemService; this.priceService = priceService; } ... }
public class OrderServiceImpl implements OrderService { private ItemService itemService; private PriceService priceService; @Inject public void init(ItemService itemService, PriceService priceService) { this.itemService = itemService; this.priceService = priceService; } ... }
public class OrderServiceImpl implements OrderService { private ItemService itemService; private PriceService priceService; @Inject public void setItemService(ItemService itemService) { this.itemService = itemService; } @Inject public void setPriceService(PriceService priceService) { this.priceService = priceService; } ... }
Guice提供依赖配置类,须要继承至AbstractModule,实现configure方法。在configure方法中咱们能够用Binder配置依赖。spring
Binder利用链式造成一套独具语义的DSL,如:api
对于上面的配置在注入的方式仅仅须要@Inject标注,但对于按名注入须要在参数前边加入@Named标注,如:app
public void configure() { final Binder binder = binder(); //TODO: bind named instance; binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class); binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class); } @Inject public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1, @Named("impl2") NamedService nameService2) { }
Guice也能够利用@Provides标注注入方法来运行时注入:如框架
@Provides public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1, @Named("impl2") NamedService nameService2) { final ArrayList<NamedService> list = new ArrayList<NamedService>(); list.add(nameService1); list.add(nameService2); return list; }
下面是一个Guice module的实例代码:包含大部分经常使用依赖配置方式。更多代码参见github .ide
package com.github.greengerong.app; /** * *************************************** * * * Auth: green gerong * * Date: 2014 * * blog: http://greengerong.github.io/ * * github: https://github.com/greengerong * * * * **************************************** */ public class AppModule extends AbstractModule { private static final Logger LOGGER = LoggerFactory.getLogger(AppModule.class); private final BundleContext bundleContext; public AppModule(BundleContext bundleContext) { this.bundleContext = bundleContext; LOGGER.info(String.format("enter app module with: %s", bundleContext)); } @Override public void configure() { final Binder binder = binder(); //TODO: bind interface binder.bind(ItemService.class).to(ItemServiceImpl.class).in(SINGLETON); binder.bind(OrderService.class).to(OrderServiceImpl.class).in(SINGLETON); //TODO: bind self class(without interface or base class) binder.bind(PriceService.class).in(Scopes.SINGLETON); //TODO: bind instance not class. binder.bind(RuntimeService.class).toInstance(new RuntimeService()); //TODO: bind named instance; binder.bind(NamedService.class).annotatedWith(Names.named("impl1")).to(NamedServiceImpl1.class); binder.bind(NamedService.class).annotatedWith(Names.named("impl2")).to(NamedServiceImpl2.class); } @Provides public List<NamedService> getAllItemServices(@Named("impl1") NamedService nameService1, @Named("impl2") NamedService nameService2) { final ArrayList<NamedService> list = new ArrayList<NamedService>(); list.add(nameService1); list.add(nameService2); return list; } }
对于Guice的使用则比较简单,利用利用Guice module初始化Guice建立其injector,如:函数
Injector injector = Guice.createInjector(new AppModule(bundleContext));
这里能够传入多个module,咱们能够利用module分离领域依赖。性能
Guice api方法:
public static Injector createInjector(Module... modules) public static Injector createInjector(Iterable<? extends Module> modules) public static Injector createInjector(Stage stage, Module... modules) public static Injector createInjector(Stage stage, Iterable<? extends Module> modules)
Guice同时也支持不一样Region配置,上面的State重载,state支持 TOOL,DEVELOPMENT,PRODUCTION选项;默认为DEVELOPMENT环境。
本文Guice更全的demo代码请参见github .
Guice还有不少的扩展如AOP,同一个服务多个实例注入set,map,OSGI,UOW等扩展,请参见Guice wiki.