国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

java輕量級IOC框架Guice

2019-11-14 22:20:46
字體:
來源:轉載
供稿:網友
java輕量級IOC框架Guice 2014-12-26 00:28 by 破狼, ... 閱讀, ... 評論, 收藏, 編輯

Guice是由Google大牛Bob lee開發的一款絕對輕量級的java IoC容器。其優勢在于:

  1. 速度快,號稱比sPRing快100倍。
  2. 無外部配置(如需要使用外部可以可以選用Guice的擴展包),完全基于annotation特性,支持重構,代碼靜態檢查。
  3. 簡單,快速,基本沒有學習成本。

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,如:

  • 基本配置:binder.bind(serviceClass).to(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
  • 無base類、接口配置:binder.bind(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
  • service實例配置:binder.bind(serviceClass).toInstance(servieInstance).in(Scopes.[SINGLETON | NO_SCOPE]);
  • 多個實例按名注入:binder.bind(serviceClass).annotatedWith(Names.named(“name”)).to(implClass).in(Scopes.[SINGLETON | NO_SCOPE]);
  • 運行時注入:利用@Provides標注注入方法,相當于spring的@Bean。
  • @ImplementedBy:或者在實現接口之上標注@ImplementedBy指定其實現類。這種方式有點反OO設計,抽象不該知道其實現類。

對于上面的配置在注入的方式僅僅需要@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.


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广灵县| 邯郸县| 徐水县| 怀安县| 皋兰县| 吉林市| 天气| 高安市| 循化| 五大连池市| 南川市| 绥中县| 武夷山市| 玉田县| 四子王旗| 寿阳县| 榕江县| 六安市| 巍山| 镇康县| 大埔县| 车险| 遂川县| 缙云县| 苍溪县| 普安县| 平山县| 新干县| 梁山县| 和硕县| 林口县| 三台县| 登封市| 益阳市| 景宁| 陇川县| 巴东县| 勃利县| 东至县| 龙游县| 即墨市|