2014-12-06 89 views
0

我正在嘗試使用Groovy配置我的Spring應用程序。我有幾個模塊,因此整個上下文被拆分爲幾個.groovy文件。Spring Groovy配置:導入ConfigSlurper屬性

我用suggested method(部分使用外部屬性)讀取使用ConfigSlurper,從外部文件屬性,以便在我的主要context.groovy有定義和使用props對象:

def props = new ConfigSlurper("dev").parse("app.properties")  
beans { 
    someBean(SomeBean) { 
     commonShinyProperty = props.common.shiny 
    } 
} 

其中app.properties是:

common { 
    shiny = true 
} 

我想要做的是重用相同的屬性源代碼E(props對象)在另一個上下文部分anotherContext.groovy - 是這樣的:

importBeans('classpath:context.groovy') 
beans { 
    anotherBean(AnotherBean) {   
     commonShinyProperty = props.common.shiny 
    } 
} 

此代碼不作爲props工作是不是可以在這裏找到,從context.groovy只有豆子。即使它被定義爲豆,應用程序無法啓動,如Cannot get property 'shiny' on null objectNo such property: for class...

請建議,如果這樣的配置是可能的。先謝謝你!

回答

1

屬性文件通過org.springframework.boot.context.config.ConfigFileApplicationListener加載,這發生在真正加載應用程序上下文之前。

我做了一個自定義GroovyPropertySource來加載類路徑上的application.groovy,所以當它需要配置時它可以通過相同的Environment.getProperty()使用到應用程序上下文。

退房https://github.com/davidiamyou/spring-groovy-config

你應該能夠做到像

beans { 
    anotherBean(AnotherBean) {   
     commonShinyProperty = '${common.shiny}' 
    } 
} 
+0

謝謝你,@davidiamyou。 – 2015-02-14 09:58:00