你可以做这样的事情在你的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) {}
}
}
那么正确的服务将自动春天注入到你的对象。
谢谢@ataylor,这就是我也想到的。加上我没有处理的bean初始化方式。另外,阅读somehwre我们可以有类似的东西,如果我们重写DefaultGrailsApplication。但文档有更多关于该插件的信息。而且我对GnG真的很陌生。独自一人。想知道是否有一种方式,否则将与if ... n其他。 – 2010-10-26 05:01:30