![](/img/trans.png)
[英]How to access jdbc properties file (for Spring Security configuration) which is outside WEB-INF folder?
[英]Create configuration properties file outside WEB-INF in war file
我已經創建了一個小型grails應用程序,現在我需要處理配置文件中的動態參數,我已經完成了數據源的工作,最后我將其放置在datasource.groovy文件中的代碼下方
grails.config.locations = ["classpath:config.properties"]
並在grails-app / config內創建了一個config.properties文件,它的工作正常,但問題是一旦我生成了戰爭文件,該文件的屬性文件位於WEB-INF / classs內,但是我需要該文件不在WEB-INF之外,eaxmple結構喜歡 :
myApp:
我該如何解決這個問題?
我最喜歡的選項:在應用服務器中指定其他類路徑,並將屬性文件放置在此路徑中。
例如在Tomcat中,它將是$ CATALINA_HOME / conf / catalina.properties (或$ CATALINA_BASE / conf / catalina.properties,如果存在)中的'common.loader'屬性。 (如果使用類似tomcat7-行家-插件其< 配置 >標記有“<additionalClasspathDirs>”元素,用戶可以指定附加的類路徑)。
有關此主題的更多討論,請參見此博客 。
對於Grails <3.0-在配置文件中,您可以放置或取消注釋以下代碼。 將其放在config.groovy文件的末尾,這樣它將覆蓋預定義的配置屬性:
// keep it at the end of the file - so you can override with external config
grails.config.locations = [ "classpath:${appName}-config.properties",
"classpath:${appName}-config.groovy",
"file:${userHome}/.grails/${appName}-config.properties",
"file:${userHome}/.grails/${appName}-config.groovy"]
if (System.properties["${appName}.config.location"]) {
grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}
然后在home .grails文件夾中創建myapp-config.properties或更好的myapp-config.groovy格式文件(“ myapp”是您的應用程序名稱),以便在開發環境中為每個Grails應用程序提供一個外部配置文件。 對於生產而言,它必須位於Tomcat類路徑中-所以一個好地方是將其放在tomcat lib文件夾中。
假設您的應用名為myapp,並且將配置文件myapp-config.groovy放置在用戶主目錄下名為myapp的文件夾中:
def loadExternalConfiguration() {
def contextPath = grailsApplication.mainContext.servletContext.contextPath.substring(1)
def userHome = System.properties['user.home']
def userConfigDir = new File("${userHome}/${contextPath}")
if (userConfigDir.exists()) {
File configFile = new File("${userConfigDir}/${contextPath}-config.groovy")
if (configFile.exists() && configFile.isFile()) {
def configSlurper = new ConfigSlurper()
def configPart = configSlurper.parse(configFile.toURI().toURL())
grailsApplication.config.merge(configPart)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.