繁体   English   中英

如何访问Grails插件的application.properties中的值?

[英]How can I access the values in application.properties of a Grails plugin?

我可以使用grailsApplication.metadata[key]从应用程序的application.properties获取值,但是对于加载的Grails插件,我很难获得相同的值。

我已经尝试过grailsApplication.mainContext.getBean('pluginManager').allPlugins但是该列表中的实例不包含插件的applications.properties文件中的值。

我也尝试过GrailsPluginUtils.getMetadataForPlugin(pluginName) ,但这也不会从Applications.properties返回值。 (实际上,我通常为空。)

有没有办法在运行时获取这些值?

@Michael的回答确实可以变得更好。 在需要的地方添加一个def grailsApplication ,然后使用${grailsApplication.metadata.'app.myprp'}类的属性访问属性app.myprp

我不喜欢这个作为答案,所以我希望有人仍会指出正确的方向。

因为我找不到获取插件元数据的方法,所以我正在手动读取插件application.properties文件(以下https://stackoverflow.com/a/20872275/489597 ):

import org.codehaus.groovy.grails.plugins.GrailsPluginUtils

GrailsPluginUtils.getPluginDirectories().collect {
    Properties properties = new Properties()
    File propertiesFile = new File("${it.getPath()}/application.properties")
    propertiesFile.withInputStream {
        properties.load(it)
    }
    properties
}

已经向我指出,这在打包的WAR中将不起作用,因为除非将它们复制到WEB-INF中,否则application.properties文件将不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM