views:

55

answers:

1

In my grails application I want to read some values from properties file and set it to Grails Domain class static property at startup.

Example

Class A{

  static myValues="1,2";
} 

 class B{
   static myValues="2,3";
  }

In the above example I have directly given the inputs..Instead of that I want to read it from one config.properties file which will have the following

A=1,2

B=2,3

Is it possible to do that in grails.Help me please.

+5  A: 

If you put config.properties in grails-app/conf then it'll be in the classpath and this code in grails-app/conf/BootStrap.groovy will load the properties and set the values:

class BootStrap {

   def init = { servletContext ->
      def props = new Properties()
      def cl = Thread.currentThread().contextClassLoader
      props.load cl.getResourceAsStream('config.properties')
      props.each { key, value ->
         def clazz = Class.forName(key, true, cl)
         clazz.myValues = value
      }
   }
}

Obviously you'll need to check that the properties file is available, that the classes, exist, etc.

Burt Beckwith