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.