codecamp

Hasor 基本IoC

首先开始一个项目中会有各种类,类与类之间还有复杂的依赖关系。如果没有 IoC 框架的情况下,我们需要手工的创建它们。通常在你脑海里会呈现一张庞大的对象图,你必须要保证它们依赖没有错误。但这是十分繁琐而且是很容易出错的。

  

付款服务以及 ItemService 商品服务。

public class TradeService {
    @Inject
    private PayService      payService;
    @Inject
    private ItemService     itemService;

    public boolean payItem(long itemId , CreditCard creditCard){
        ...
    }
}


接下来我们创建 AppContext 并通过它将 TradeService 所需要的服务对象注入进去即可。接下来您只需要通过下面这个代码完成 TradeService 对象的创建,Hasor 会自动完成依赖注入。

AppContext appContext = Hasor.createAppContext();
TradeService tradeService = appContext.getInstance(TradeService.class);
....
tradeService.payItem(......);


如果 PayService 为一个接口而非具体的实现类,那么您可以在 PayService 接口上通过 @ImplBy 注释标记出它的实现类即可。

@ImplBy(PayServiceImpl.class)
public interface PayService {
    ...
}


如果我们需要 TradeService 是一个单列那么在类上标记 @Singleton 注解即可。

@Singleton
public class TradeService {
    ...
}


接下来如果我们希望 TradeService 在创建时做一些初始化工作,您只需要在初始化方法上加上 @Init 注解即可。

@Singleton
public class TradeService {
    @Init
    public void initMethod(){
        ...
    }
}


同样的例子,我们还可以通过 Module 的方式进行实现 无侵入 的注入。

AppContext appContext = Hasor.createAppContext(new Module() {
    public void loadModule(ApiBinder apiBinder) throws Throwable {
        apiBinder.bindType(TradeService.class)
                .inject("payService", apiBinder.bindType(PayService.class).toInfo())
                .inject("itemService", apiBinder.bindType(ItemService.class).toInfo())
                .initMethod("initMethod")
                .asEagerSingleton();
    }
});


Hasor 使用模板引擎
Hasor Aop拦截方法调用
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }