如何将外部属性文件中的映射包含到Application.cfc中? [英] How can I include mappings into Application.cfc from external property file?

查看:140
本文介绍了如何将外部属性文件中的映射包含到Application.cfc中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Application.cfc中设置映射有困难
我们有diverent Server(dev,QS,prod)
每个都有一点不同的Pathes。
我想通过配置文件设置服务器特定的修补和变量。
在ApplicationStart上,您读取ini文件并设置系统。
http:// www .raymondcamden.com / index.cfm / 2005/8/26 / ColdFusion-101-Config-Files-AGoGo
这个工作正常。



Normaly您在Applcation.cfc中设置映射,如下所示:

  <!--- in Application.cfc ---> 
< cfset this.mappings ['/ components'] =D:\Inetpub\wwwroot\myApp\components>

在正常cfm文件中的某处我通过以下方式添加了一个名为test的cfc:

 < cfset t = createObject(component,components.test)> 

我想在 onApplicationsStart

 < cffunction 
name =OnApplicationStart
access =public
returntype =boolean
output =false
hint =首次创建应用程序时触发。>

<!--- create structure to hold配置设置--->
< cfset ini = structNew()>
< cfset ini.iniFile = expandPath(./ ApplicationProperties.ini)>
< cfset application.ini = ini>

<!--- read ini file --->
< cfset sections = getProfileSections(application.ini.iniFile)>

< cfloop index =keylist =#sections.mappings#>
< cfset this.mappings [key] = getProfileString(application.ini.iniFile,mappings,key)>
< / cfloop>

但这不起作用,因为this.mappings为空,下一个请求。 :(



将此设置为OnRequestStart

  - 读取ini文件---> 
< cfset sections = getProfileSections(application.ini.iniFile)>

< cfloop index =keylist = mappings#>
< cfset this.mappings [key] = getProfileString(application.ini.iniFile,mappings,key)>
< / cfloop>

我得到一个无法找到组件的错误。
这很奇怪。



将结构体放入应用程序范围

 < cfloop index =keylist =#sections.mappings#> 
< cfset APPLICATION.mappings [key] = getProfileString(application.ini.iniFile,mappings,key)>
< / cfloop>

如何调用我的组件?

 < cfset t = createObject(component,application.components.test)> 

不起作用。



所以我有3个目标。


  1. 从ini文件中读取所有剪辑和映射

  2. 在ApplicationStart读取一次

  3. 源代码。


解决方案

不能在onApplicationStart()中设置映射,在Application.cfc的伪构造函数中,并且它们必须在每个请求上设置。



还需要注意的是,此时应用程序范围不可用,因此如果需要缓存任何内容,则需要使用服务器范围。您可以将映射结构体缓存到服务器范围,并将其设置为每个请求的this.mappings。

 < cfcomponent> 
< cfset this.name =myapp/>

<!--- not cached so create mappings --->
< cfif NOT structKeyExists(server,#this.name#_mappings)>
< cfset iniFile = getDirectoryFromPath(getCurrentTemplatePath())& /ApplicationProperties.ini/>
< cfset sections = getProfileSections(iniFile)/>
< cfset mappings = structnew()/>
< cfloop index =keylist =#sections.mappings#>
< cfset mappings [key] = getProfileString(iniFile,mappings,key)>
< / cfloop>
< cfset server [#this.name#_mappings] = mappings />
< / cfif>

<!---分配来自服务器范围中的缓存结构的映射 - >
< cfset this.mappings = server [#this.name#_mappings] />

< cffunction name =onApplicationStart>
<!--- other stuff here --->
< / cffunction>

< / cfcomponent>

如果您打算在webroot中保留ini文件,应该将其设为.cfm模板,使用< cfabort>启动它。








 < cfabort> 
[mappings]
/ foo = c:/ bar / foo


I have trouble with setting mappings in Application.cfc We have diverent Server (dev,QS,prod) Each with a little different Pathes. I want to set serverspecific pathes and variables via configuration file. On ApplicationStart you read the ini file and setup your system. http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo This works fine.

Normaly you set mappings in Applcation.cfc like this:

<!--- in Application.cfc --->
<cfset this.mappings['/components'] = "D:\Inetpub\wwwroot\myApp\components">

Somewhere in a normal cfm File I instatiate a cfc named test via:

<cfset t = createObject("component", "components.test")>

I want to set the mappings only once at onApplicationsStart

<cffunction
    name="OnApplicationStart"
    access="public"
    returntype="boolean"
    output="false"
    hint="Fires when the application is first created.">

    <!---create structure to hold configuration settings--->
    <cfset ini = structNew()>
    <cfset ini.iniFile = expandPath("./ApplicationProperties.ini")>
    <cfset application.ini = ini>

    <!--- read ini file --->
    <cfset sections = getProfileSections(application.ini.iniFile)>

    <cfloop index="key" list="#sections.mappings#">
       <cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
    </cfloop>

But this don't work because this.mappings is empty and next request. :(

Putting this to OnRequestStart

<!--- read ini file --->
    <cfset sections = getProfileSections(application.ini.iniFile)>

    <cfloop index="key" list="#sections.mappings#">
       <cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
    </cfloop>

I get an error that the component can't be found. This is strange.

Putting the struct into Application scope

    <cfloop index="key" list="#sections.mappings#">
       <cfset APPLICATION.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
    </cfloop>

How to call my Component?

<cfset t = createObject("component", "application.components.test")>

Doesn't work.

So I have 3 targets.

  1. reading all pathes and mappings from ini file
  2. reading them once at ApplicationStart
  3. easy usage in sourcecode.

解决方案

Mappings can't be set in onApplicationStart(), they must be set in the pseudo constructor of Application.cfc, and they must be set on every request.

It's also important to note that the application scope is not available at this point, therefore if you need to cache anything you'll need to use the server scope. You can cache your mapping struct to the server scope and just set it into this.mappings each request.

<cfcomponent>
  <cfset this.name = "myapp" />

  <!--- not cached so create mappings --->
  <cfif NOT structKeyExists(server, "#this.name#_mappings")>
    <cfset iniFile = getDirectoryFromPath(getCurrentTemplatePath()) & "/ApplicationProperties.ini" />
    <cfset sections = getProfileSections(iniFile) />
    <cfset mappings = structnew() />
    <cfloop index="key" list="#sections.mappings#">
      <cfset mappings[key] = getProfileString(iniFile, "mappings", key)>
    </cfloop>
    <cfset server["#this.name#_mappings"] = mappings />
  </cfif>

  <!--- assign mappings from cached struct in server scope --->
  <cfset this.mappings = server["#this.name#_mappings"] />

  <cffunction name="onApplicationStart">
  <!--- other stuff here --->
  </cffunction>

</cfcomponent>

If you intend to keep you ini file in the webroot, you should make it a .cfm template and start it with a <cfabort>. It will work just the same but will not be readable

ApplicationProperties.ini.cfm

<cfabort>
[mappings]
/foo=c:/bar/foo

这篇关于如何将外部属性文件中的映射包含到Application.cfc中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆