如何获得温莎城堡自动注入资产? [英] How can I get Castle Windsor to automatically inject a property?

查看:69
本文介绍了如何获得温莎城堡自动注入资产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类中有一个用于日志记录服务的属性。

I have a property on my classes for logging service.

private ILogger logger = NullLogger.Instance;
public ILogger Logger
{
    get { return logger; }
    set { logger = value; }
}

我在组件注册中有此内容:

And I have this in my component registration:

container.AddFacility<LoggingFacility>(x => new LoggingFacility(LoggerImplementation.Log4net));

但是,温莎似乎并未注入Logger-我是否缺少某些东西?

However, Windsor doesn't seem to inject the Logger - am I missing something?

推荐答案

AddFacility的lambda参数实际上是创建回调(在创建设施时调用),而不是工厂。

The lambda parameter for AddFacility is actually a creation callback (it gets called when the facility is created), not a factory.

使用以下替代方法:

container.AddFacility("logging", new LoggingFacility(LoggerImplementation.Log4net, "path_to_log4net.config"));

BTW温莎将自动注入属性依赖项。

BTW Windsor automatically injects property dependencies whenever it can.

这篇关于如何获得温莎城堡自动注入资产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆