在AEM中,我如何将项目环境获取到我的Sling模型中。

huangapple go评论88阅读模式
英文:

In AEM how can I get project environment into my sling model

问题

我需要在我的 Sling 模型中使用环境(开发|生产等,而不是像 author|publish 这样的 runmode)。我该如何将其注入到我的核心模型中?

是否有相应的服务可供使用?

英文:

I need to use the environment in my sling model (dev|prod etc, not runmode such as author|publish). How can I inject it into my core model?

Is there any service for this ?

答案1

得分: 2

dev|prod等,而不是诸如author|publish之类的run mode

authorpublish 是固定的运行模式之一,但一般情况下,运行模式可以用来区分 devprod(或类似的环境)。

通常情况下,当需要区分AEM环境,比如开发与生产环境时,可以通过自定义运行模式来实现。虽然AEM作为Cloud Service对自定义程度有一定限制,但你提到的情况仍然可以通过开箱即用的方式来实现。除其他用途外,它还可以用来管理特定于环境的OSGi配置

对于本地部署/托管部署,你将拥有更多的灵活性。我一直都是为这种目的使用自定义的运行模式。

需要注意的一点是,你需要在Sling Model中编程方式检查运行模式,这确实让人感到疑惑。我不确定你正在实现什么,但如果某个功能取决于环境,我更倾向于通过分配给特定运行模式的替代OSGi配置来处理它。我认为通过在需要时添加另一个sling:OsgiConfig节点作为配置会更容易,而不是在仅识别预定环境集的Java类中调整条件逻辑。

假设你有一组如此的运行模式,你可以将 SlingSettingsService 注入到你的模型中,并以这种方式读取运行模式。或者你可以编写一个OSGi服务来封装你需要的任何逻辑。这样的服务会使用与给定环境相关的配置启动,你可以直接将其注入到你的Sling模型中,知道它返回的值就是你需要的值。

英文:

> dev|prod etc, not runmode such as author|publish

author and publish are among the fixed run modes but run modes in general can be used to tell dev from prod (or similar kinds of environments) as well.

Usually, when AEM environments need to be told apart, e.g. dev vs prod, it is realised through custom run modes. While AEM as a Cloud Service places some limitations on just how much you can customise, the case you mention is still covered OOTB. Among other things, it can be used to manage environment-specific OSGi config.

An on-premise/hosted deployment gives you even more flexibility. I've always used customized run modes for this kind of purpose.

One thing to note is that it does raise an eyebrow that you need to programmatically check the run mode in a Sling Model. I'm not sure what you're implementing but if a piece of functionality is dependent on the environment, I'd rather handle it via alternative OSGi configuration assigned to a given run mode. I think it's generally easier to add another configuration as a sling:OsgiConfig node when required, as opposed to adjusting conditional logic in a Java class that only recognises a predetermined set of environments.

Provided that you have a set of run modes like this, you could inject SlingSettingsService into your model and read the run modes this way. Or you could write an OSGi service to encapsulate whatever logic you need. Such a service would start up with the configuration relevant to a given environment and you could inject it directly into your Sling model, knowing the values it returns are the ones you need.

huangapple
  • 本文由 发表于 2020年10月16日 01:09:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64376581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定