'Holders.config is null during grails run-war

Recently did a log4j upgrade on grails 2.5.4 application. Following dependencies pertaining to v2.17.1 have been added:

  • log4j-api
  • log4j-core
  • log4j-1.2-api
  • log4j-slf4j-impl

I've excluded the following dependencies from the global level:

  • log4j
  • grails-plugin-log4j
  • jcl-over-slf4j

The application boots up without any issues on doing grails run-app. But when I try to run the war by grails run-war or deploy the war using tomcat, I keep getting:

Error creating bean with name 'grailsApplication' defined in ServletContext
...
Caused by: java.lang.ExceptionInInitializerError
...
Caused by: java.lang.NullPointerException: Cannot get property 'xyz' on null object

This null object is Holders.config

This issue occurs during static property initialization. The class looks like:

class Test {
    public static Logger log = LoggerFactory.getLogger(this)
    private static final String SOURCE_URL = Holders.config.xyz.abc
    ...
}

Another class had the same issue during the initialization of variable using Holders.config in static block.

Strangely, this issue is only for classes under grails-app/utils folder because I've another class under src/groovy where its variables are defined in a similar way but there's no issue with it during the war file execution.

There's no change in code except for the dependencies added/removed as mentioned above. I also tried using applicationContext.getBean('grailsApplication).config.xyz.abc but it said Could not find ApplicationContext, configure Grails correctly first.

Can someone please suggest what could be the root cause or guide how I can fix or maybe initialize these static values.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source