开发者

Using External Configuration File

I've added the code below to my Config.groovy file, however, in spite of having it I'm n开发者_JAVA百科ot able to access the external configuration properties in the Config.groovy file.

Does anyone how I can access the properties of the external configuration file within the Config.groovy file?

if (System.getProperty("CONFIG")) { 
  grails.config.locations << "file:" + System.getProperty("CONFIG")
} else { 
  grails.config.locations << "file:./${appName}-config.properties"
}

Note: I've tried using ${...} like I would in Spring configuration files, ConfigurationHolder.config, and grailsApplication to access the properties but none of these approaches work.

Thanks.


I think this would cause confusion due to the order the config files are loaded. I'm pretty sure at the time the Config.groovy is loaded, the external one hasn't been loaded yet.

So your "CONFIG" property is set in the external file, which is the name of the file that you want to load?

How I usually do this is just list all the files that I could use.

grails.config.locations = [
        "file:../app-config/myapp-dataSource.groovy",
        "file:../app-config/myapp-config.groovy"
]

environments {
  development {
    grails.config.locations = [
            "file:../myapp-config/myapp-dataSource.groovy",
            "file:../myapp-config/myapp-config.groovy",
            "file:${userHome}/myapp-config/myapp-dataSource.groovy",
            "file:${userHome}/myapp-config/myapp-config.groovy"
    ]   
  }
...
}

If the files do not exist they are just skipped. Files I believe are loaded in order, so anything in the ${userHome} dir, will override the previously set values. This is nice for development, as you can have machine local ways of changing settings, and not have to worry about these config changes being checked in.


@Nick Larson, What you mentioned about CONFIG not loaded is not true. If CONFIG is a JVM parameter, set with -DCONFIG=xxxx, then it is set before config.groovy kicks in.

@Kin1, You are using file: protocol for accessing the property file. Are you trying to access this in a WAR or EAR file or is it a file based system. In a WAR or EAR file you need to use classpath: for the file, file: does not work. Also, you have to make sure to actually copy the Groovy file(not compiled class file) in the classpath. We do it on WAR create event and the build process copy the config file into one of the classpath location.

Hope this helps.


Add the below line in config.groovy

grails.config.locations = [ "classpath:grails-app-config.properties"]

environments {
    development {   
        grails.logging.jul.usebridge = true
        grails.config.locations = ["file:C:\\conf\\externalfile.groovy"]
    }
    production {
        grails.logging.jul.usebridge = false
        grails.config.locations = ["file:/opt/config/externalfile.groovy"]
        // TODO: grails.serverURL = "http://www.changeme.com"
    }
}

If you want to access any property from external configuration(config.groovy) then just declare the property like 

property = property value eg:(ImagePath = "C:\\Users\\Saved Pictures")

access it like grailsApplication.config."property"

eg:(grailsApplication.config.ImagePath)

NOTE: dont use def just a property and its value.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜