改變值動態第一種方式:
grailsApplication.config.grails.mail.put("port",your database fetch value of port)
grailsApplication.config.grails.mail.put("username","your database fetch value of username")
grailsApplication.config.grails.mail.put("password","your database fetch value of username")
grailsApplication.config.grails.mail.put("props","your database fetch value of username")
第二種方式改變值動態:
org.codehaus.groovy.grails.commons.DefaultGrailsApplication
DefaultGrailsApplication類提供所有有關的配置信息。您可以使用以下代碼獲取電子郵件信息並將其更改爲動態。
def defaultGrailsApplication= new DefaultGrailsApplication()
// to get mails information from Config.groovy
defaultGrailsApplication?.config?.grails.mail?.get("host")
defaultGrailsApplication?.config?.grails.mail?.get("port")
defaultGrailsApplication?.config?.grails.mail?.get("username")
defaultGrailsApplication?.config?.grails.mail?.get("password")
defaultGrailsApplication?.config?.grails.mail?.get("props")
// to change mails information of Config.groovy
defaultGrailsApplication?.config?.grails.mail?.put("host","your database fetch value of host")
defaultGrailsApplication?.config?.grails.mail?.put("port",your database fetch value of port)
defaultGrailsApplication?.config?.grails.mail?.put("username","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("password","your database fetch value of username")
defaultGrailsApplication?.config?.grails.mail?.put("props","your database fetch value of username")
現在你需要指定這個「defaultGrailsApplication」實例注入的應用價值。只是假設,我們有grailsApplication注入像一個一流的服務Temp.service:
Temp.service
class Temp{
def grailsApplication
def your method(){
please add above code for set value
grailsApplication = defaultGrailsApplication
}
}
[數據庫屬性](可能重複http://stackoverflow.com/questions/15947021/properties在數據庫中) –