2

I'm trying to create an external configuration for a non controller or taglib class because the values will need to change without a recompile. Now that ConfigurationHolder and ApplicationHolder are deprecated what are my options for this?

I've done about 3 hours of Googling and it seems that the only thing someone has come up with since those classes have been deprecated is just using DI. However, I need this configuration to be external of the WAR file somehow so I don't know if that would help me unless I'm missing something?

Thanks

  • 1
    Just want to clarify what you mean by "configuration" - are you referring to system wide settings? For example if you were creating a banking app you would need to be able to set/modify a default interest rate. If so I would look at the Settings plugin. – Kelly Jun 12 '12 at 00:13
  • Correct. I'll take a look to see what that plugin offers, thanks! –  Jun 12 '12 at 13:26

2 Answers2

1

Take a look at the first lines of your default config file...

If you uncomment this code, you will get an external configuration which is read when the application starts.

So you will not have to recompile but to restart your application if the configuration changes. Since many admins restart the application server automatically over night, this shouldn't be a big problem, should it?

// locations to search for config files that get merged into the main config
// config files can either be Java properties files or ConfigSlurper scripts

// grails.config.locations = [ "classpath:${appName}-config.properties",
//                             "classpath:${appName}-config.groovy",
//                             "file:${userHome}/.grails/${appName}-config.properties",
//                             "file:${userHome}/.grails/${appName}-config.groovy"]

// if(System.properties["${appName}.config.location"]) {
//    grails.config.locations << "file:" + System.properties["${appName}.config.location"]
// }

In order to access the config from within /src/groovy take a look at this question: How to access Grails configuration in Grails 2.0?

btw: yes, the configurationHolder is deprecated, but it still works :-)

Community
  • 1
  • 1
rdmueller
  • 10,742
  • 10
  • 69
  • 126
  • This is the route I would like to go, but there is only access to the values in those external locations through the GrailsApplication class which is only made available to controllers and taglibs (and I need the values to be available to a regular class in src/groovy) –  Jun 12 '12 at 13:24
0

Based on the response I got from js3v I would recommend looking at the Settings plugin. It gives you a very nice way to enter these types of items and a basic CRUD interface to manage them. You can have String, Integer, Date and BigDecimal types. You can create names to segment them (like admin.email.defaultGreeting, or banking.defaultInterestRate) and access them in gsp's or controllers/services/domain classes like this:

//gsp
<g:setting valueFor="admin.email.defaultGreeting" default="Hello!" encodeAs="HTML"/>

//Other
import org.grails.plugins.settings.*
Setting.valueFor("admin.email.defaultGreeting", "Hello!")
Kelly
  • 3,709
  • 4
  • 20
  • 31
  • I have been experimenting and you can manage classes with spring in the conf/spring/resources.groovy class and then you can set the properties of those beans externally. That is probably what I will opt for, but your solution also solves my problem without using deprecated stuff so you get the accept. Cheers. –  Jun 12 '12 at 20:56