标签: wirebox

如何使构造函数方法可以使用WireBox注入的依赖项?

在这个例子中,我有一个叫做模型对象test.cfc具有相关性testService.cfc.

testWireBox testService通过属性声明注入.该对象如下所示:

component {

     property name="testService" inject="testService";

     /**
     *  Constructor
     */
     function init() {

         // do something in the test service
         testService.doSomething();

         return this;

     }

 }
Run Code Online (Sandbox Code Playgroud)

作为参考,testService有一个叫做doSomething()转储出一些文本的方法:

component
     singleton
{

     /**
     *  Constructor
     */
     function init() {

         return this;

     }


     /**
     *  Do Something
     */
     function doSomething() {

         writeDump( "something" );

     }

 }
Run Code Online (Sandbox Code Playgroud)

问题是,WireBox似乎testService在构造函数init()方法触发之后才会注入.所以,如果我在我的处理程序中运行它:

prc.test = wirebox.getInstance(
     name = "test"
);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息: Error building: test …

coldfusion dependency-injection coldbox cfml wirebox

4
推荐指数
1
解决办法
152
查看次数