我想在Grails中添加基于linux或基于windows的系统属性,因为我的应用程序需要在两者中运行。我知道我们可以添加在配置中指定的grails.config.locations位置。真棒。
但我需要if和esle条件才能选择文件。问题是config.grrovy有userHome,grailsHome,appName,appVersion,我需要像osName这样的东西。我可以继续使用syetm.properties,或者如果soembody可以告诉我这些(仅)属性如何在Config.groovy中可用(通过DefaultGrailsApplication或其他方式)。那就太好了。
此外,如果我需要这些属性,我会将我的服务作为用户定义的spring-bean,这也是更优雅的。这是正确和可行的方法吗?如果是,举个例子
发布于 2010-10-26 00:15:35
你可以在你的Config.groovy中这样做
environments {
development {
if (System.properties["os.name"] == "Linux") {
grails.config.locations = [ "file:$basedir/grails-app/conf/linux.properties" ]
} else {
grails.config.locations = [ "file:$basedir/grails-app/conf/windows.properties" ]
}
}
...
}或者,对于基于服务的方法,您可以将所有操作系统特定的行为捆绑到服务接口的实现中。例如:
// OsPrinterService.groovy
interface OsPrinterService {
void printOs();
}
// LinuxOsPrinterService.groovy
class LinuxOsPrinterService implements OsPrinterService {
void printOs() { println "Linux" }
}
// WindowsOsPrinterService.groovy
class WindowsOsPrinterService implements OsPrinterService {
void printOs() { println "Windows" }
}然后在grails-app/conf/spring/resources.groovy中实例化正确的,如下所示:
beans = {
if (System.properties["os.name"] == "Linux") {
osPrinterService(LinuxOsPrinterService) {}
} else {
osPrinterService(WindowsOsPrinterService) {}
}
}然后spring会自动将正确的服务注入到你的对象中。
发布于 2010-10-26 00:11:33
为Windows和Linux创建自定义环境。如果放在config.groovy中,下面这样的代码应该可以工作
environments {
productionWindows {
filePath=c:\path
}
productionLinux {
filePath=/var/dir
}
}然后,您应该能够使用grails配置对象来获取filePath的值,无论您是在Windows上还是在Linux上。有关这方面的更多详细信息,请参阅http://www.grails.org/doc/1.0.x/guide/3.%20Configuration.html的第3.2节。如果您想要创建一个war文件以在Linux上运行,您可以执行以下命令。
grails -Dgrails.env=productionLinux war然后获取您在其中运行的特定环境的config.groovy中存储的文件路径。
def fileToOpen=Conf.config.filePath根据当前运行的环境,fileToOpen将包含您在config.groovy中分配给filePath的值,因此当使用productionLinux作为环境运行时,它将包含值/var/dir
https://stackoverflow.com/questions/4015727
复制相似问题