gpt4 book ai didi

delphi - 使用 Spring4D 进行构造函数注入(inject)时出现 "Unsatisfied constructor"

转载 作者:行者123 更新时间:2023-12-03 15:49:49 24 4
gpt4 key购买 nike

我正在努力解决 Spring4D 的构造函数注入(inject)问题。在某个类中,我想将接口(interface)的特定实现(按名称)注入(inject)到构造函数中。

看看这个:

IListFactory = interface
['{40...29}']
function GetList : IListOfSomething;
end;

ICiderPress = interface
['{50...10}']
procedure Press;
end;

TAppleListFactory = class(TInterfacedObject, IListFactory)
function GetList : IListOfSomething;
end;

TCiderPress = class(TInterfacedObject, ICiderPress)
private
FListFactory : IListFactory;
public
constructor Create(const ListFactory : IListFactory);

procedure Press;
end;

implementation

function TCiderPress.Create(const ListFactory : IListFactory);
begin
FListFactory := ListFactory;
end;

procedure TCiderPress.Press;
begin
// Do somtihing with FListFactory
end;

initialization
GlobalContainer.RegisterType<TAppleListFactory>.Implements<IListFactory>('apple');

GlobalContainer.RegisterType<TCiderPress>.Implements<ICiderPress>;
end.

现在我使用 ServiceLocator 获取打印机的实例:

CiderPress := ServiceLocator.GetService<ICiderPress>;
CiderPress.Press;

而且效果很好。

现在我添加第二个 ListFactory:

TOrangeListFactory = class(TInterfacedObject, IListFactory)
function GetList : IListOfSomething;
end;

并添加注册

GlobalContainer.RegisterType<TOrangeListFactory>.Implements<IListFactory>('orange');

并将我的 cidre press 类更改为

TCiderPress = class(TInterfacedObject, ICiderPress)
private
FListFactory : IListFactory;
public
[Inject]
constructor Create([Inject('apple')]const ListFactory : IListFactory);

procedure Press;
end;

问题是,TCiderPress 的 ctor 没有被调用。

如果我添加

GlobalContainer.AddExtension<TActivatorContainerExtension>;

我收到 EActivatorException:类型不满足的构造函数:TCiderPress

出了什么问题?

编辑:

如果我像这样委托(delegate)构造,它就会起作用:

GlobalContainer.RegisterType<TCiderPress>.Implements<ICiderPress>
.DelegateTo(function : TCiderPress
begin
Result := TCiderPress.Create(ServiceLocator.GetService<IListFactory>('apple');
end
);

编辑2:

我发现我的错误了!我必须在接口(interface) uses 子句中包含 Spring.Container.Common

我使用的是 Delphi XE3 和 Spring4D 1.1.3。

最佳答案

这对我有用:

unit Unit2;

interface

uses
Spring.Container,
Spring.Container.Common;

type
IListOfSomething = interface
['{ACCEF350-5FDE-4D60-BAE0-17F029A669ED}']
end;


IListFactory = interface
['{039DE93A-1235-4D75-A8E2-7265765F6E90}']
function GetList : IListOfSomething;
end;

ICiderPress = interface
['{64C4F565-BB8C-42C0-9584-4F4A21779F52}']
procedure Press;
end;

TAppleListFactory = class(TInterfacedObject, IListFactory)
public
function GetList: IListOfSomething;
end;

TOrangeListFactory = class(TInterfacedObject, IListFactory)
public
function GetList: IListOfSomething;
end;

TCiderPress = class(TInterfacedObject, ICiderPress)
private
FListFactory: IListFactory;
public
[Inject]
constructor Create([Inject('apple')] const ListFactory: IListFactory);
procedure Press;
end;

implementation

constructor TCiderPress.Create(const ListFactory: IListFactory);
begin
FListFactory := ListFactory;
end;

procedure TCiderPress.Press;
begin
WriteLn(TObject(FListFactory).ClassName);
end;

{ TAppleListFactory }

function TAppleListFactory.GetList: IListOfSomething;
begin
Result := nil;
end;

{ TOrangeListFactory }

function TOrangeListFactory.GetList: IListOfSomething;
begin
Result := nil;
end;

initialization
GlobalContainer.RegisterType<TAppleListFactory>.Implements<IListFactory>('apple');
GlobalContainer.RegisterType<TOrangeListFactory>.Implements<IListFactory>('orange');
GlobalContainer.RegisterType<TCiderPress>.Implements<ICiderPress>;
GlobalContainer.Build();
end.

和消费一样

o := GlobalContainer.Resolve<ICiderPress>;
o.Press();

关于delphi - 使用 Spring4D 进行构造函数注入(inject)时出现 "Unsatisfied constructor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35922238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com