boraas
boraas

Reputation: 942

GrailsApplication in Domain Class

I would like to write the following:

import grails.core.GrailsApplication

class MyDomainClass{
    GrailsApplication grailsApplication
    String otherName
    
    String getName(){
        String prop =  grailsApplication.config.getProperty('my.property.from.application.yml')
        return prop + otherName
    }
    static mapping = {
        autowire true
    }
}

However, if I have grailsApplication in the domain class the grails command grails run-app does not start the webserver and waits forever. It outputs the following warnings, but no error:

> IDLE
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/Users/martin/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.14/f0a005fb21e7bd9b7ebf04cd2ecda0fc8f3be59d/groovy-2.5.14.jar) to method java.util.AbstractMap.clone()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/Users/martin/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy/2.5.14/f0a005fb21e7bd9b7ebf04cd2ecda0fc8f3be59d/groovy-2.5.14.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass

Is this some kind of bad practice or a bug? How can I address this issue? The most obvious choice would be in my opinion a call to a service.

I am using the following Grails version:

| Grails Version: 4.0.11
| JVM Version: 11.0.11

Upvotes: 1

Views: 306

Answers (1)

Joe
Joe

Reputation: 1219

Add the WebAttribute Trait and then you get the grailsApplication included.

9.1.1 WebAttributes Trait Example

WebAttributes is one of the traits provided by the framework. Any Groovy class may implement this trait to inherit all of the properties and behaviors provided by the trait.

src/main/groovy/demo/Helper.groovy:

package demo

import grails.web.api.WebAttributes

class Helper implements WebAttributes {

    List<String> getControllerNames() {
        // There is no need to pass grailsApplication as an argument
        // or otherwise inject the grailsApplication property.  The
        // WebAttributes trait provides access to grailsApplication.
        grailsApplication.getArtefacts('Controller')*.name
    }
}

Upvotes: 2

Related Questions