属性文件中的Grails域类属性

Don*_*onX 1 grails properties grails-domain-class

在我的grails应用程序中,我想从属性文件中读取一些值,并在启动时将其设置为Grails Domain类的静态属性.

Class A{

  static myValues="1,2";
} 

 class B{
   static myValues="2,3";
  }
Run Code Online (Sandbox Code Playgroud)

在上面的例子中我直接给出了输入..而不是我想从一个config.properties文件中读取它,它将具有以下内容

A = 1,2-

B = 2,3

是否有可能在grails中这样做.请帮助我.

Bur*_*ith 6

如果你把config.properties放在grails-app/conf中,那么它将在classpath中,grails-app/conf/BootStrap.groovy中的这段代码将加载属性并设置值:

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
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

显然,您需要检查属性文件是否可用,类,是否存在等.