从Java类访问Config.groovy

时间:2012-10-31 12:27:29

标签: java grails configuration groovy

当我的Grails应用程序启动时,我也会在后台开始Spring Integration和Batch过程。我想在Config.groovy文件中存储一些数据库连接属性,但是如何从集成/批处理过程中使用的Java类访问它们?

我找到了这个帖子:

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

建议使用:

private Map config = ConfigurationHolder.getFlatConfig();

后跟类似的内容:

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

这实际上工作正常(从Config.groovy正确加载了属性)但问题是ConfigurationHolder在被弃用之后。我发现处理该问题的任何线程似乎都是Grails特定的并建议使用依赖注入,就像在这个线程中一样:

How to access Grails configuration in Grails 2.0?

是否有一种不推荐的方法可以从Java类文件访问Config.groovy属性?

3 个答案:

答案 0 :(得分:4)

只是要注册,在Grails 2.x中,有一个Holders class替换了这个已弃用的持有者。您可以使用它来访问静态上下文中的grailsApplication

答案 1 :(得分:3)

刚刚检查了一些现有代码,我使用了this method described by Burt Beckwith

创建新文件: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()
  }
}

然后,修改grails-app/config/spring/resources.groovy以包含:

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

然后,在src/javasrc/groovy内的文件中,您可以致电:

GrailsApplication app = ApplicationContextHolder.getGrailsApplication() ;
ConfigObject config = app.getConfig() ;

答案 2 :(得分:1)

我无法理解为什么这不起作用,但我可以完全建议另一种方法。 Grails设置PropertyPlaceholderConfigurer,其值来自grailsApplication.config,因此您可以声明

public void setDriver(String driver) { ... }

在你班上,然后说

<bean class="com.example.MyClass" id="exampleBean">
  <property name="driver" value="${jdbc.driver}" />
</bean>

如果您正在使用bean DSL,这也适用于resources.groovy,但您必须记住使用单引号而不是加倍:

exampleBean(MyClass) {
  driver = '${jdbc.driver}'
}

使用"${jdbc.driver}"不起作用,因为Groovy将其解释为GString并且在处理resources.groovy时(未能)解析,而您需要的是放置文字{{1}表达式作为稍后由占位符配置器解析的属性值。

相关问题