构造函数注入问题
创建 MainClass 时,将调用 public MyWindsorClass(IMyInterface obj) {} ;创建 MainClass2 时,当我期望调用 public MyWindsorClass() {} 时,也会调用 public MyWindsorClass(IMyInterface obj) {} 。我究竟做错了什么?我在构造函数注入方面遇到问题。这是一个示例:
class Program
{
static void Main(string[] args)
{
var container = new WindsorContainer(new XmlInterpreter());
var objs = container.ResolveAll<IMyWindsorInterface>();
}
}
public interface IMyWindsorInterface { }
public class MyWindsorClass : IMyWindsorInterface
{
public MyWindsorClass() {}
public MyWindsorClass(IMyInterface obj) {}
}
public interface IMyInterface { }
public class MyInjectedClass : IMyInterface{ }
<castle>
<components>
<component id="MainClass"
service="CastleTest.IMyWindsorInterface, CastleTest"
type="CastleTest.MyWindsorClass, CastleTest"
inspectionBehavior="None"
lifestyle="Transient">
<parameters>
<obj>${blah}</obj>
</parameters>
</component>
<component id="MainClass2"
service="CastleTest.IMyWindsorInterface, CastleTest"
type="CastleTest.MyWindsorClass, CastleTest"
inspectionBehavior="None"
lifestyle="Transient" />
<component id="blah"
service="CastleTest.IMyInterface, CastleTest"
type="CastleTest.MyInjectedClass, CastleTest"
inspectionBehavior="None"/>
</components>
</castle>
创建 MainClass 时,将调用 public MyWindsorClass(IMyInterface obj) {} ;创建 MainClass2 时,当我期望调用 public MyWindsorClass() {} 时,也会调用 public MyWindsorClass(IMyInterface obj) {} 。我到底做错了什么?感谢任何可以提供帮助的人!
When MainClass is created public MyWindsorClass(IMyInterface obj) {} is called and when MainClass2 is created public MyWindsorClass(IMyInterface obj) {} is also called when I'm expecting public MyWindsorClass() {} to be called. What exactly am I doing wrong?I'm having an issue with constructor injection. Here is an example:
class Program
{
static void Main(string[] args)
{
var container = new WindsorContainer(new XmlInterpreter());
var objs = container.ResolveAll<IMyWindsorInterface>();
}
}
public interface IMyWindsorInterface { }
public class MyWindsorClass : IMyWindsorInterface
{
public MyWindsorClass() {}
public MyWindsorClass(IMyInterface obj) {}
}
public interface IMyInterface { }
public class MyInjectedClass : IMyInterface{ }
<castle>
<components>
<component id="MainClass"
service="CastleTest.IMyWindsorInterface, CastleTest"
type="CastleTest.MyWindsorClass, CastleTest"
inspectionBehavior="None"
lifestyle="Transient">
<parameters>
<obj>${blah}</obj>
</parameters>
</component>
<component id="MainClass2"
service="CastleTest.IMyWindsorInterface, CastleTest"
type="CastleTest.MyWindsorClass, CastleTest"
inspectionBehavior="None"
lifestyle="Transient" />
<component id="blah"
service="CastleTest.IMyInterface, CastleTest"
type="CastleTest.MyInjectedClass, CastleTest"
inspectionBehavior="None"/>
</components>
</castle>
When MainClass is created public MyWindsorClass(IMyInterface obj) {} is called and when MainClass2 is created public MyWindsorClass(IMyInterface obj) {} is also called when I'm expecting public MyWindsorClass() {} to be called. What exactly am I doing wrong? Thanks to anyone that can help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,Windsor 将自动装配所有可用的依赖项。在您的情况下,
IMyInterface
可作为组件使用,因此 Windsor 将选择可以满足最多依赖项的构造函数。您可以通过从组件模型中删除构造函数来临时更改此行为,以便 Windsor 不再“看到”它:
Windsor will by default autowire all available dependencies. In your case,
IMyInterface
is available as a component so Windsor will choose the constructor that can satisfy the most dependencies.You may change this behavior ad-hoc by removing the constructor from the component model so Windsor doesn't 'see' it any more: