wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在启动时查询注册表时的ComponentLookupError大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我正在构建一个应用程序,使用COllective.lead(trunk)查询外部关系数据库中的某些数据.用户可以在自定义Plone控制面板工具中修改数据库连接设置(我按照Aspeli的Professional Plone Development书中的示例).以这种方式查询数据库设置. 我的产品base.zcml为数据库设置了一个实用程序: <include package="plone.app.r@H_944_5@
我正在构建一个应用程序,使用COllective.lead(trunk)查询外部关系数据库中的某些数据.用户可以在自定义Plone控制面板工具中修改数据库连接设置(我按照Aspeli的Professional Plone Development书中的示例).以这种方式查询数据库设置.

我的产品base.zcml为数据库设置了一个实用程序:

<include package="plone.app.registry" />
<include package="collective.lead" />

<i18n:registerTranslations directory="locales" />

<utility
  provides="collective.lead.interfaces.IDatabase"
  factory=".dbsetTings.CalculatorDatabase"
  name="test.calc.db"
  />

dbsetTings.py有:

from zope.component import getUtility
from plone.registry.interfaces import IRegistry

class CalculatorDatabase(DatabasE):

    @property
    def _url(self):
        registry = getUtility(IRegistry)
        setTings = registry.forInterface(IDatabaseSetTings)
        return URL(
            drivername=setTings.drivername,username=setTings.username,password=setTings.password,host=setTings.hostname,port=setTings.port,database=setTings.database,)

这会在运行时引发ComponentLookupError异常:

File "/home/zope/envs/test-web/src/test.calc/test/calc/dbsetTings.py",line 38,in _url
    registry = getUtility(IRegistry)
File "/home/zope/envs/test-web/eggs/zope.component-3.7.1-py2.6.egg/zope/component/_api.py",line 171,in getUtility
    raise ComponentLookupError(interface,Name)
zope.configuration.config.ConfigurationExecutionError: <class 'zope.component.interfaces.ComponentLookupError'>: (<InterfaceClass plone.registry.interfaces.IRegistry>,'')
in:
File "/home/zope/envs/test-web/src/test.calc/test/calc/configure.zcml",line 26.2-30.6
<utility
  provides="collective.lead.interfaces.IDatabase"
  factory=".dbsetTings.CalculatorDatabase"
  name="test.calc.db"
  />

为什么在运行时找不到注册表?我究竟做错了什么?

谢谢.

史蒂夫提到的是问题的根源,因此我只会针对您无法进行异常处理的情况添加解决方法.在类似的场景中,我需要注册一个依赖于存储在注册表中的设置的实用程序.没有这些设置就无法注册该实用程序,所以我绝对希望事情按顺序发生.

解决方法是不在zcml中注册该实用程序,而是在以下订阅者中执行此操作:

<subscriber
    for="Products.CMFPlone.interfaces.IPlonesiteRoot
         zope.app.publication.interfaces.IBeforeTraverseEvent"
    handler=".component.setupstuff"
    />

这将保证setupstuff可以访问所有本地实用程序.另请注意,在查询实用程序是否已存在之后,setupstuff也会快速返回,因为订阅者将在每个请求上触发.

大佬总结

以上是大佬教程为你收集整理的在启动时查询注册表时的ComponentLookupError全部内容,希望文章能够帮你解决在启动时查询注册表时的ComponentLookupError所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。