Spring4D字段注入在TForm实例中不起作用



我想使用Spring4D1.1版的Inject属性,就像下面的示例代码一样。Inject属性似乎没有效果,因为按钮单击处理程序方法中的fMyResource字段值是NIL。在我的原始代码中,类型注册在Application.CreateForm(TForm1, Form1);之前的dpr文件中。我只是修改它以使代码更加简洁。我应该怎么做才能使现场注射工作?

unit FieldInjectionTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Spring.Container.Common;
type
IMyResource = interface
['{6BD6421E-F57F-41BD-A6E4-347B2BE20A3C}']
procedure foo;
end;
TMyResource = class ( TInterfacedObject, IMyResource )
public
procedure foo; virtual;
end;
TForm1 = class ( TForm )
button1 : TButton;
procedure Button1Click( sender : TObject );
private
[Inject]
fMyResource : IMyResource;
end;
implementation
uses
Spring.Container;
procedure TMyResource.foo;
begin
//...
end;
procedure TForm1.Button1Click( sender : TObject );
begin
// fMyResource is NIL
fMyResource.foo;
end;
initialization
globalContainer.registerType<TMyResource>.implements<IMyResource>;
globalContainer.build;
end.

@whirsdaddy的注释帮助我理解:[Inject]属性只适用于容器管理的对象。当我重新思考时,这并不奇怪。

最新更新