Grails 3 - 在Bootstrap.groovy

时间:2015-12-01 10:59:58

标签: grails grails-3.0 grails-3.0.9

我正在将Grails 2.5应用程序迁移到Grails 3.0.9并且在grails-app/init/BootStrap.groovy中读取类路径资源时遇到一些问题。

在Grails 2.5.x中,当将资源文件放入与ClassPathResource

相同的文件夹时,可以使用Spring BootStrap.groovy访问资源(例如文本文件)
def file1 = new ClassPathResource('my-textfile.txt').file

在Grails 3中,当我使用grails run-app启动应用程序时,它可以正常工作。所有资源均来自src/main/resources。但是如果我使用IntelliJ中的Spring Boot Application(位于/grails-app/init/a/b/Application.groovy)类启动应用程序,BootStrap.groovy就会再找到资源。

使用BootStrap.groovy启动应用时,是否有人知道如何在Grails 3.0.9中的Application.groovy内访问这些文件?

2 个答案:

答案 0 :(得分:1)

根据Gradle惯例,我将资源放在src/main/resource下,因为它们需要在运行时可访问。

由于某些原因,我的grails-app/init/a/b/Application.groovy无法找到这些资源,因此我必须将符号链接放到grails-app/init/a/b。之后一切都按预期工作。

答案 1 :(得分:0)

class BootStrap {

     def init = { servletContext ->

        def config = new ConfigSlurper().parse(new File("${System.properties['user.dir']}/grails-app/conf/test.groovy").toURL())
        println config
        println config.hfy.test
    }
    def destroy = {
    }
}

$ {System.properties ['user.dir']}:项目目录

相关问题