2012-10-31 41 views
6

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

我发现这个线程:

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

这表明使用:

private Map config = ConfigurationHolder.getFlatConfig(); 

其次是这样的:

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

这实际工作正常(德属性装从Config.groovy正确)但问题是t帽子ConfigurationHolder被弃用后。我发现处理这个问题的任何线索似乎是具体的Grails和在这个线程使用扶养注入,比如建议:

How to access Grails configuration in Grails 2.0?

那么,有没有一种非过时的方式来获得访问来自Java类文件的Config.groovy属性?

+0

[接受问题的接受答案](http://stackoverflow.com/a/7136095/6509)中的第二种方法有什么问题? –

+0

你好,蒂姆,欢呼回答。我将我的java类作为一个bean放入资源文件(具有grailsApplication的属性和grailsApplication的参数),并将以下代码行添加到我的类中:private GrailsApplication grailsApplication;与getter和setter。但是当我尝试通过ConfigObject config = grailsApplication.getConfig()来访问它时,它似乎是零和错误了。任何想法我做错了什么?谢谢 – Illu

+0

你在哪里试图访问它?你不能在构造函数中使用依赖注入的bean,例如,你需要声明一个用'@ PostConstruct'注解的方法,并把需要'grailsApplication'的逻辑代替,或者使用构造函数参数注入来代替二传手注射。 –

回答

1

我不知道为什么这不起作用,但我可以完全建议一种替代方法。 Grails的建立一个PropertyPlaceholderConfigurergrailsApplication.config需要它的价值,所以你可以在你的类声明

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

,然后说

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

,如果你使用的咖啡豆这也适用于resources.groovy DSL,但你必须记住使用单引号而非双:

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

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

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() ; 
4

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

+0

这似乎取代了Burt的[ApplicationContextHolder](http://burtbeckwith.com/blog/?p=1017),因为它提供了来自该解决方案的大部分内容,也解决了静态分配问题。 – raffian