Accessing grails application config from a quartz

2019-05-27 15:12发布

Using grails 2.4.2, quartz:1.0.2, I'm trying to gain access to configuration properties

class MyJob {
  def grailsApplication
  int propA
  def MyJob() {
    propA = grailsApplication.config.foo.bar.propAVal
  }
  ...
}

grailsApplication, however, doesn't get injected, and is null.

Can't access any bean from Quartz Job in Grails supposedly relates to this, but I don't really see how the marked answer resolves the OP's question :-/

help? 10x

3条回答
看我几分像从前
2楼-- · 2019-05-27 15:39

For those using grails 3.x the type of grailsApplication now has to be specified explicitly. E.g. instead of:

def grailsApplication

you need to use:

import grails.core.GrailsApplication
...
GrailsApplication grailsApplication


If you don't you'll receive the following compilation error:

The return type of java.lang.Object getGrailsApplication() in namespace.classnameJob is incompatible with grails.core.GrailsApplication in grails.plugins.quartz.QuartzJob

查看更多
狗以群分
3楼-- · 2019-05-27 15:43

The problem is probably that you are accessing grailsApplication in constructor, where it's not injected yet.

I recommend to dump useless class property int propA and do it this way:

  def grailsApplication

  def execute() {
    def propA = grailsApplication.config.foo.bar.propAVal         
    ... 
    //use propA for something
  }
查看更多
男人必须洒脱
4楼-- · 2019-05-27 15:46
import grails.util.Holders

class MyJob {
    def grailsApplication = Holders.getGrailsApplication()
    int propA
    def MyJob() {
        propA = grailsApplication.config.foo.bar.propAVal
    }
    ...
}

In this way you can access 'grailsApplication' from a Quartz job or even from any groovy file inside "/src" folder.

查看更多
登录 后发表回答