为什么依赖注入在我的CF工厂对象中失败?

时间:2021-01-03 09:58:11

I get this error

我收到这个错误

Element INSTANCE is undefined in VARIABLES.

元素INSTANCE在VARIABLES中未定义。

I do not see the reason for the error!

我没有看到错误的原因!

This is my factory

这是我的工厂

<cfcomponent output="true" displayname="ObjectFactory">

 <cffunction name="init" access="public" output="true" returntype="ObjectFactory">
  <cfset variables.instance = structNew() />
  <cfreturn this />
 </cffunction>

 <cffunction name="createObj" access="public" output="false" returntype="any">
  <cfargument name="objName" type="string" required="true" />
  <cfswitch expression="#arguments.objName#">
   <cfcase value="abstractCollection">
    <cfreturn createObject('component',"AbstractCollection").init() />
    <cfbreak />
   </cfcase>
   <cfcase value="assignmentCollection">
    <cfreturn createObject('component',"AssignmentCollection").init() />
    <cfbreak />
   </cfcase>
   <cfcase value="salesmanBean">
    <cfreturn createObject('component',"SalesmanBean").init(
     salesmanHasThisDecorations = this.getInstance("assignmentCollection")) />
    <cfbreak />
   </cfcase>
  </cfswitch>
 </cffunction>

 <cffunction name="getInstance" access="public" output="false" returntype="any">
  <cfargument name="objName" type="string" required="true" />
 <!--- Error occurs in the line below --->
  <cfif not structKeyExists(variables.instance, arguments.objName)>
   <cfset variables.instance[arguments.objName] = this.createObj(arguments.objName) />
  </cfif>
  <cfreturn variables.instance[arguments.objName] />
 </cffunction>
</cfcomponent>

2 个解决方案

#1


Make sure you call init() when you instantiate ObjectFactory:

确保在实例化ObjectFactory时调用init():

<cfset objectFactory = CreateObject("component","ObjectFactory").init()>

FYI, init() and <cfcomponent> should have output='false'

FYI,init()和 应该有output ='false'

FYI, you should call your own function without "this.", because if for some reason the function is later declared as private, it won't find it in 'this' scope.

仅供参考,您应该在没有“this。”的情况下调用自己的函数,因为如果由于某种原因该函数稍后被声明为私有,它将无法在“this”范围内找到它。

#2


Agree that you are likely not calling .init() so are not creating the variable before accessing it.

同意您可能不会调用.init(),因此在访问变量之前不会创建变量。

You also may want to initialize (create) the VARIABLES scoped variables outside of init(). The init() should be used more for passing in values to your internal CFC-scope (VARIABLES scope) than for creating variables within it.

您还可能希望在init()之外初始化(创建)VARIABLES范围的变量。 init()应该更多地用于将值传递到内部CFC范围(VARIABLES范围),而不是在其中创建变量。

<cfcomponent displayname="ObjectFactory">
<cfset variables.instance = structNew() />

 <cffunction name="init" access="public" returntype="ObjectFactory">
  <cfargument name="name" required="yes" type="string">
  <cfset variables.instance.name = arguments.name>
  <cfreturn this />
 </cffunction>

...

#1


Make sure you call init() when you instantiate ObjectFactory:

确保在实例化ObjectFactory时调用init():

<cfset objectFactory = CreateObject("component","ObjectFactory").init()>

FYI, init() and <cfcomponent> should have output='false'

FYI,init()和 应该有output ='false'

FYI, you should call your own function without "this.", because if for some reason the function is later declared as private, it won't find it in 'this' scope.

仅供参考,您应该在没有“this。”的情况下调用自己的函数,因为如果由于某种原因该函数稍后被声明为私有,它将无法在“this”范围内找到它。

#2


Agree that you are likely not calling .init() so are not creating the variable before accessing it.

同意您可能不会调用.init(),因此在访问变量之前不会创建变量。

You also may want to initialize (create) the VARIABLES scoped variables outside of init(). The init() should be used more for passing in values to your internal CFC-scope (VARIABLES scope) than for creating variables within it.

您还可能希望在init()之外初始化(创建)VARIABLES范围的变量。 init()应该更多地用于将值传递到内部CFC范围(VARIABLES范围),而不是在其中创建变量。

<cfcomponent displayname="ObjectFactory">
<cfset variables.instance = structNew() />

 <cffunction name="init" access="public" returntype="ObjectFactory">
  <cfargument name="name" required="yes" type="string">
  <cfset variables.instance.name = arguments.name>
  <cfreturn this />
 </cffunction>

...