Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Config.groovy from Java class

When my Grails app starts up, I also begin a Spring Integration and Batch process in the background. I want to have some DB connection properties stored in the Config.groovy file, but how do I access them from a Java class used in teh Integration/Batch process?

I found this thread:

Converting Java -> Grails ... How do I load these properties?

Which suggests using:

private Map config = ConfigurationHolder.getFlatConfig();

followed by something like:

String driver = (String) config.get("jdbc.driver");

This actually works fine (teh properties are loaded correctly from Config.groovy) but the problem is that ConfigurationHolder is after being deprecated. And any thread I've found dealing with the issue seems to be Grails-specific and suggest using dependancy injection, like in this thread:

How to access Grails configuration in Grails 2.0?

So is there a non-deprecated way to get access to the Config.groovy properties from a Java class file?

like image 261
Illu Avatar asked Oct 31 '12 12:10

Illu


People also ask

How do I import a Java class into groovy?

The same code in Java needs an import statement to Date class like this: import java. util. Date. Groovy by default imports these classes for you.

What is groovy config?

groovy, is for settings that are used when running Grails commands, such as compile, doc, etc. The second file, Config. groovy, is for settings that are used when your application is running. This means that Config. groovy is packaged with your application, but BuildConfig.


1 Answers

Just checked in some of my existing code, and I use this method described by Burt Beckwith

Create a new file: src/groovy/ctx/ApplicationContextHolder.groovy

package ctx

import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import javax.servlet.ServletContext

import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware

@Singleton
class ApplicationContextHolder implements ApplicationContextAware {
  private ApplicationContext ctx

  private static final Map<String, Object> TEST_BEANS = [:]

  void setApplicationContext(ApplicationContext applicationContext) {
    ctx = applicationContext
  }

  static ApplicationContext getApplicationContext() {
    getInstance().ctx
  }

  static Object getBean(String name) {
    TEST_BEANS[name] ?: getApplicationContext().getBean(name)
  }

  static GrailsApplication getGrailsApplication() {
    getBean('grailsApplication')
  }

  static ConfigObject getConfig() {
    getGrailsApplication().config
  }

  static ServletContext getServletContext() {
    getBean('servletContext')
  }

  static GrailsPluginManager getPluginManager() {
    getBean('pluginManager')
  }

  // For testing
  static void registerTestBean(String name, bean) {
    TEST_BEANS[name] = bean
  }

  // For testing
  static void unregisterTestBeans() {
     TEST_BEANS.clear()
  }
}

Then, edit grails-app/config/spring/resources.groovy to include:

  applicationContextHolder(ctx.ApplicationContextHolder) { bean ->
    bean.factoryMethod = 'getInstance'
  }

Then, in your files inside src/java or src/groovy, you can call:

GrailsApplication app = ApplicationContextHolder.getGrailsApplication() ;
ConfigObject config = app.getConfig() ;
like image 125
tim_yates Avatar answered Oct 10 '22 08:10

tim_yates