Guice是由Google大牛Bob lee開發的一款絕對輕量級的java IoC容器。其優勢在于:
Guice和spring各有所長,Guice更適合與嵌入式或者高性能但項目簡單方案,如OSGI容器,spring更適合大型項目組織。
注入方式在我們談到IOC框架,首先我們的話題將是構造,屬性以及函數注入方式,Guice的實現只需要在構造函數,字段,或者注入函數上標注@Inject,如:
構造注入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; } ...}函數(setter)注入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; } ...}Module依賴注冊Guice提供依賴配置類,需要繼承至AbstractModule,實現configure方法。在configure方法中我們可以用Binder配置依賴。
Binder利用鏈式形成一套獨具語義的DSL,如:
對于上面的配置在注入的方式僅僅需要@Inject標注,但對于按名注入需要在參數前邊加入@Named標注,如:
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);}@Injectpublic 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實例下面是一個Guice module的實例代碼:包含大部分常用依賴配置方式。更多代碼參見github .
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的使用則比較簡單,利用利用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.
新聞熱點
疑難解答